Skip to content

Commit

Permalink
Merge pull request #12 from chosencharacters/eric/preloader
Browse files Browse the repository at this point in the history
Copy the preloader from 2022
  • Loading branch information
chosencharacters authored Nov 30, 2024
2 parents 506712f + 14eca6f commit 13023eb
Show file tree
Hide file tree
Showing 11 changed files with 279 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<!--The flixel preloader is not accurate in Chrome. You can use it regularly if you embed the swf into a html file
or you can set the actual size of your file manually at "FlxPreloaderBase-onUpdate-bytesTotal"-->
<app preloader="flixel.system.FlxPreloader" />
<app preloader="Preloader" />

<!--Minimum without FLX_NO_GAMEPAD: 11.8, without FLX_NO_NATIVE_CURSOR: 11.2-->
<set name="SWF_VERSION" value="11.8" />
Expand Down
Binary file added assets/preloader/bg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/preloader/cane.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/preloader/caneAnim.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/preloader/caneMask.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/preloader/loading.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/preloader/start.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/preloader/stripes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/preloader/xmasTank.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 19 additions & 8 deletions minigames/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,22 @@
2. Add the following to the main `Project.xml` in the Minigames section. Make sure to use the correct minigame ID!

```xml
<section unless="exclude_bunnymark">

<classpath path="minigames/bunnymark/source"/>
<library name="bunnymark" preload="false" />
<assets path="minigames/bunnymark/assets" rename="assets/minigames/bunnymark" library="bunnymark" exclude="*.ase|*.wav"/>

</section>
```
<section unless="exclude_bunnymark">
<classpath path="minigames/bunnymark/source"/>
<library name="bunnymark" preload="false" />
<assets path="minigames/bunnymark/assets" rename="assets/minigames/bunnymark" library="bunnymark" exclude="*.ase|*.wav"/>
</section>
```

3. Edit `assets/data/entries/minigames.json` and add an entry to the `minigames` list.
4. Add a Minigame entity in the LDTK project whose `minigame_id` value is the ID of the associated minigame.
5. Edit `source/minigames/MinigameHandler.hx` and edit `initalize()` to add the minigame's constructor to the `constructors` list.

Note that `external` minigames don't need code, just an entry in `minigames.json` and a corresponding Minigame entity in the LDTK project.

## Minigame Types

There are three types of minigames:
- `overlay`: This displays the minigame as a substate. This is the primary mode to use.
- `state`: This fully switches states to play the minigame. Currently not implemented, but should only be needed if the minigame truely doesn't support being a substate for some reason.
- `external`: Displays a prompt to access an external URL. This is good for things like linking the previous Tankmas events!
259 changes: 259 additions & 0 deletions source/Preloader.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
//SHOUTOUTS TO GAMEPOPPER FOR THE BALLIN TUTORIAL
//https://gamepopper.co.uk/2014/08/26/haxeflixel-making-a-custom-preloader/
package;

import flixel.tweens.FlxEase;
import openfl.Lib;
import openfl.display.Bitmap;
import openfl.display.BitmapData;
import openfl.display.Shape;
import openfl.display.Sprite;
import openfl.events.Event;
import openfl.events.MouseEvent;
import openfl.geom.Rectangle;
import openfl.text.TextField;
import openfl.text.TextFormat;
import openfl.text.TextFormatAlign;

@:bitmap("assets/preloader/cane.png" ) class Cane extends BitmapData { }
@:bitmap("assets/preloader/caneMask.png") class CaneMask extends BitmapData { }
@:bitmap("assets/preloader/stripes.png" ) class Stripes extends BitmapData { }
@:bitmap("assets/preloader/loading.png" ) class Loading extends BitmapData { }
@:bitmap("assets/preloader/start.png" ) class Start extends BitmapData { }
@:bitmap("assets/preloader/caneAnim.png") class CaneAnim extends BitmapData { }
@:bitmap("assets/preloader/xmasTank.png") class XmasTank extends BitmapData { }
@:bitmap("assets/preloader/bg.png" ) class BG extends BitmapData { }

class Preloader extends flixel.system.FlxBasePreloader
{
inline static var STRIPE_LOOP = 149;
inline static var CANE_THICKNESS = 60;
inline static var STRIPE_MAX = 326;
inline static var LOOP_TIME = 1.0;
inline static var TEXT_CHANGE_TIME = 1.0;
inline static var TEXT_WIPE_TIME = TEXT_CHANGE_TIME / 3;

override public function new(MinDisplayTime:Float = 1, ?AllowedURLs:Array<String>)
{
super(MinDisplayTime, AllowedURLs);
}

var cane:Bitmap;
var caneAnim:SpriteSheet;
var stripes:Bitmap;
var maskShape:Shape;
var outroStarted = false;
var outroStartTime:Float = 0;
var outroCompleted = false;
var loadingText:Bitmap;
var startText:Bitmap;
var clicked = false;

override private function create():Void
{
this._width = Lib.current.stage.stageWidth;
this._height = Lib.current.stage.stageHeight;

//bg
var BG = new Bitmap(new BG(960, 540));
BG.smoothing = false;
//bg.scaleX /= 2;
//bg.scaleY /= 2;
addChild(BG);

//tank image
var tank = new Bitmap(new XmasTank(318, 318));
tank.smoothing = false;
tank.scaleX /= 2;
tank.scaleY /= 2;
tank.x = (this._width - 318 * (tank.scaleX*2)) / 2;
tank.y = 25;
addChild(tank);

//cane loading base
cane = new Bitmap(new Cane(400, 150));
var caneMask = new Bitmap(new CaneMask(0, 0));
addChild(cane);
addChild(stripes = new Bitmap(new Stripes(0, 0)));
addChild(caneMask);
cane.smoothing = false;
cane.x = (this._width - 400) / 2;
cane.y = (this._height - 150) / 2 + 175;
caneMask.smoothing = false;
// caneMask.transform.colorTransform.color = Lib.current.stage.color;
caneMask.x = cane.x;
caneMask.y = cane.y + 150 - CANE_THICKNESS + 2;
stripes.smoothing = false;
stripes.x = caneMask.x;
stripes.y = caneMask.y;

maskShape = new Shape();
maskShape.graphics.beginFill(0xFFFFFF);
maskShape.graphics.drawRect(0, 0, STRIPE_MAX, CANE_THICKNESS);
maskShape.graphics.endFill();
maskShape.x = caneMask.x;
maskShape.y = caneMask.y;
stripes.mask = maskShape;

//text
loadingText = new Bitmap(new Loading(0, 0));
loadingText.smoothing = false;
loadingText.x = cane.x + 30;
loadingText.y = cane.y + 18;
loadingText.scrollRect = new Rectangle(0, 0, 194, 48);
addChild(loadingText);

startText = new Bitmap(new Start(0, 0));
startText.smoothing = false;
startText.x = loadingText.x;
startText.y = loadingText.y - 24;
startText.scrollRect = new Rectangle(0, 0, 0, 63);
addChild(startText);

//pull version from project.xml
var versionText = new TextField();
var format = new TextFormat("_sans", null, 0x999999);
versionText.defaultTextFormat = format;
versionText.selectable = false;
versionText.text = 'Version: ' + lime.app.Application.current.meta.get('version');
versionText.y = _height - versionText.textHeight - 2;
addChild(versionText);

stage.addEventListener(MouseEvent.CLICK, onClick);

super.create();
}

function onClick(e:MouseEvent)
{
if (outroCompleted)
{
stage.removeEventListener(MouseEvent.CLICK, onClick);
clicked = true;
}
}

override private function destroy():Void
{
stripes = null;
mask = null;
loadingText = null;
startText = null;

super.destroy();
}

override function onEnterFrame(e:Event)
{
var time = Date.now().getTime() - _startTime;
var min = minDisplayTime * 1000;

var textOutroComplete = false;
if (outroStarted)
{
var outroTime = (time - outroStartTime) / 1000;
//trace(outroTime);
if (outroTime < TEXT_WIPE_TIME)
{
final rect = loadingText.scrollRect;
final t = outroTime / TEXT_WIPE_TIME;
rect.width = FlxEase.circIn(1 - t) * loadingText.width;
loadingText.scrollRect = rect;
}
else if (outroTime > TEXT_CHANGE_TIME)
{
startText.scrollRect = null;
}
else if (outroTime > TEXT_CHANGE_TIME - TEXT_WIPE_TIME)
{
final rect = startText.scrollRect;
final t = (TEXT_CHANGE_TIME - TEXT_WIPE_TIME - outroTime) / TEXT_WIPE_TIME;
rect.width = FlxEase.circIn(t) * startText.width;
startText.scrollRect = rect;
}

if (outroTime > TEXT_WIPE_TIME)
{
loadingText.visible = false;
loadingText.scrollRect = null;
}
textOutroComplete = outroTime > TEXT_CHANGE_TIME;
}

if (caneAnim != null)
{
outroCompleted = caneAnim.finished && textOutroComplete;
caneAnim.update();
}

if (_loaded && (min <= 0 || time / min >= 1) && !outroCompleted)
{
_loaded = false;
outroStarted = true;
outroStartTime = time;
}
else if (outroCompleted && clicked)
_loaded = true;

super.onEnterFrame(e);

if (stripes != null)
{
var oldX = stripes.x;
stripes.x = cane.x
+ Math.round(-STRIPE_LOOP * (time / 1000.0 / LOOP_TIME)) % STRIPE_LOOP;
stripes.x = Math.floor(stripes.x / 4) * 4;

if (oldX < stripes.x && outroStarted)
{
stripes.x = cane.x;// - STRIPE_LOOP;
addChild(caneAnim = new SpriteSheet(new CaneAnim(0, 0), 168, 150, [0,0,0,0,1,2,3,4,5,6,7,7,7,7,7,7]));
caneAnim.x = cane.x + cane.width - caneAnim.width;
caneAnim.y = cane.y;
stripes = null;
}
}
}

override public function update(percent:Float):Void
{
super.update(percent);

maskShape.width = STRIPE_MAX * percent;
if (caneAnim != null)
caneAnim.update();
}
}

class SpriteSheet extends Sprite
{
public var finished(get, never):Bool;
inline function get_finished() return time > frames.length * frameTime;

var frames:Array<Int>;
var frameTime = 0.0;
var time = 0.0;

override function get_width():Float return scrollRect.width;
override function get_height():Float return scrollRect.height;

public function new(bitmapData:BitmapData, width:Int, height:Int, frames:Array<Int>, frameRate = 15)
{
this.frameTime = 1 / frameRate;
this.frames = frames;
super();
addChild(new Bitmap(bitmapData));
scrollRect = new Rectangle(0, 0, width, height);
}

inline public function update():Void
{
time += 1 / Lib.current.stage.frameRate;
var frame = Math.floor(time / frameTime);
if (frame >= frames.length)
frame = frames.length -1;
var rect = scrollRect;
rect.x = rect.width * frames[frame];
scrollRect = rect;
}
}

0 comments on commit 13023eb

Please sign in to comment.