Skip to content

Commit 9207bdc

Browse files
committed
Added Substates
1 parent e1c6992 commit 9207bdc

File tree

7 files changed

+256
-18
lines changed

7 files changed

+256
-18
lines changed

HxCU_Meta.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
2-
"libVersion": "1.5.5",
2+
"libVersion": "1.5.7",
3+
"customUDPPort": "",
34
"haxeLibs": [
45
"hxu_sdl2",
56
"hxu_jansson",

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ A code probably similar to one from [HaxeFlixel](https://haxeflixel.com) right?
7878
- [x] buttons (Partially broken)
7979
- [x] Audio support with precise control (Only one music or sound at the same time)
8080
- [x] States
81-
- [ ] Substates
81+
- [x] Substates
8282
- [x] Collisions
8383
- [x] Sprites basic physics
8484
- [x] Tweens and easing functions

leafy/LfEngine.hx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class LfEngine {
4343
/**
4444
* The current version of the engine
4545
*/
46-
public static final VERSION:String = "1.5.6";
46+
public static final VERSION:String = "1.5.8";
4747

4848
/**
4949
* Function to be called when the engine exits
@@ -78,14 +78,13 @@ class LfEngine {
7878
/**
7979
* Initialize the engine, and start the state
8080
*
81+
* Example:
82+
*
83+
* LfEngine.initEngine("LEAFY_GAME", LfRenderType.DRC, new MyGameState());
84+
*
8185
* @param gamePath The path to the game folder ("LEAFY_GAME" by default)
8286
* @param renderMode The render mode of the engine
8387
* @param state The initial state
84-
*
85-
* Example:
86-
* ```
87-
* LfEngine.initEngine("LEAFY_GAME", LfRenderType.DRC, new MyGameState());
88-
* ```
8988
*/
9089
public static function initEngine(gamePath:String, renderMode:LfRenderType = LfRenderType.DRC, state:LfState):Void {
9190
#if !disableHxCrashHandler

leafy/backend/LfJson.hx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ class LfJson {
1919
/**
2020
* Pointer to the JSON object
2121
*/
22-
private var jsonPtr:Ptr<Json_t>;
22+
public var jsonPtr:Ptr<Json_t>;
2323

2424
/**
2525
* JSON error object
2626
*/
27-
private var jsonError:Json_error_t;
27+
public var jsonError:Json_error_t;
2828

2929
/**
3030
* Constructor for LfJson

leafy/objects/LfObject.hx

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ package leafy.objects;
88
import Std;
99

1010
import sdl2.SDL_Render.SDL_Texture;
11-
import sdl2.SDL_Pixels.SDL_Color;
1211
import sdl2.SDL_Render;
12+
import sdl2.SDL_Render.SDL_RendererFlip;
13+
import sdl2.SDL_Pixels.SDL_Color;
1314
import sdl2.SDL_Rect;
1415
import sdl2.SDL_Surface.SDL_Surface;
1516
import sdl2.SDL_Image;
@@ -18,7 +19,6 @@ import leafy.backend.sdl.LfWindow;
1819
import leafy.utils.LfUtils.LfVector2D;
1920
import leafy.utils.LfUtils;
2021

21-
2222
/**
2323
* Type of the object
2424
*/
@@ -37,12 +37,24 @@ enum CenterMode {
3737
CENTER_XY;
3838
}
3939

40+
/**
41+
* Scale mode of the object
42+
*/
4043
enum ScaleMode {
4144
NEAREST;
4245
LINEAR;
4346
BEST;
4447
}
4548

49+
/**
50+
* Flip mode of the object
51+
*/
52+
enum FlipMode {
53+
NONE;
54+
HORIZONTAL;
55+
VERTICAL;
56+
}
57+
4658
/**
4759
* Base class for all objects
4860
*
@@ -183,6 +195,11 @@ class LfObject extends LfBase {
183195
*/
184196
public var scaleMode:ScaleMode = null;
185197

198+
/**
199+
* Flip mode of the object
200+
*/
201+
public var flipMode:FlipMode = FlipMode.NONE;
202+
186203
////////////////////////////////
187204

188205
override public function update(elapsed:Float):Void {
@@ -241,7 +258,7 @@ class LfObject extends LfBase {
241258
// }
242259

243260
SDL_Render.SDL_SetTextureAlphaMod(this.sdlTexturePtr, Std.int(this.alpha * 255));
244-
SDL_Render.SDL_RenderCopyEx(LfWindow.currentRenderer, this.sdlTexturePtr, null, this.sdlRect, this.angle, null, SDL_FLIP_NONE);
261+
SDL_Render.SDL_RenderCopyEx(LfWindow.currentRenderer, this.sdlTexturePtr, null, this.sdlRect, this.angle, null, convertFlipModeToSDL(this.flipMode));
245262
}
246263

247264
/**
@@ -373,6 +390,18 @@ class LfObject extends LfBase {
373390
this.scaleMode = scaleMode;
374391
}
375392

393+
/**
394+
* Set the flip mode of the object texture
395+
* @param flipMode
396+
*/
397+
public function setFlipMode(flipMode:FlipMode):Void {
398+
if (this.sdlTexturePtr == null) {
399+
LeafyDebug.log("Failed to set flip mode for object: [" + this.name + "] - Texture is null (" + SDL_Image.IMG_GetError().toString() + ")", ERROR);
400+
return;
401+
}
402+
this.flipMode = flipMode;
403+
}
404+
376405
/**
377406
* Convert a Leafy object scale mode to an SDL scale mode
378407
* @param scaleMode
@@ -391,6 +420,24 @@ class LfObject extends LfBase {
391420
}
392421
}
393422

423+
/**
424+
* Convert a Leafy object flip mode to an SDL flip mode
425+
* @param flipMode
426+
* @return SDL_Render.SDL_RendererFlip
427+
*/
428+
private function convertFlipModeToSDL(flipMode:FlipMode):SDL_RendererFlip {
429+
switch (flipMode) {
430+
case FlipMode.NONE:
431+
return SDL_RendererFlip.SDL_FLIP_NONE;
432+
case FlipMode.HORIZONTAL:
433+
return SDL_RendererFlip.SDL_FLIP_HORIZONTAL;
434+
case FlipMode.VERTICAL:
435+
return SDL_RendererFlip.SDL_FLIP_VERTICAL;
436+
default:
437+
return SDL_RendererFlip.SDL_FLIP_NONE;
438+
}
439+
}
440+
394441
/**
395442
* Update the SDL rect of the object
396443
*/

leafy/states/LfState.hx

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
package leafy.states;
77

8-
import haxe.ds.List;
8+
import haxe.Unserializer.TypeResolver;
99
import Std;
10+
1011
import leafy.objects.LfObject;
11-
import leafy.gamepad.LfGamepad;
1212

1313
/**
1414
* Base class for Leafy states
@@ -21,6 +21,21 @@ class LfState extends LfBase {
2121
*/
2222
public var stateObjects:Array<LfObject> = new Array<LfObject>();
2323

24+
/**
25+
* The sub-state of the state
26+
*/
27+
public var subState:LfSubState = null;
28+
29+
// /**
30+
// * Whether the state should keep updating
31+
// */
32+
// public var keepUpdating:Bool = true;
33+
34+
// /**
35+
// * Whether the state should keep rendering
36+
// */
37+
// public var keepRendering:Bool = true;
38+
2439
public function new() {
2540
super();
2641
}
@@ -41,16 +56,27 @@ class LfState extends LfBase {
4156
* @param elapsed The elapsed time
4257
*/
4358
override public function update(elapsed:Float):Void {
59+
// if (!keepUpdating) {
60+
// return;
61+
// }
62+
4463
for (object in this.stateObjects) {
4564
object.update(elapsed);
4665
}
47-
}
4866

67+
if (this.subState != null) {
68+
this.subState.update(elapsed);
69+
}
70+
}
4971

5072
/**
51-
* /* Function called when the state is rendered
73+
* Function called when the state is rendered
5274
*/
5375
override public function render():Void {
76+
// if (!keepRendering) {
77+
// return;
78+
// }
79+
5480
if (this.stateObjects == []) {
5581
return;
5682
}
@@ -60,6 +86,10 @@ class LfState extends LfBase {
6086
obj.render();
6187
}
6288
}
89+
90+
if (this.subState != null) {
91+
this.subState.render();
92+
}
6393
}
6494

6595
/**
@@ -78,6 +108,10 @@ class LfState extends LfBase {
78108
object.destroy();
79109
}
80110

111+
if (this.subState != null) {
112+
this.subState.destroy();
113+
}
114+
81115
this.stateObjects.pop();
82116
}
83117

@@ -109,7 +143,30 @@ for (size_t i = 0; i < stateObjects->size(); i++) {
109143
}
110144
}
111145
");
146+
}
147+
148+
/**
149+
* Function to set the sub-state of the state
150+
* @param newSubState The new sub-state to set
151+
*/
152+
public function setSubState(newSubState:LfSubState):Void {
153+
if (newSubState == null) {
154+
return;
155+
}
112156

113-
157+
if (this.subState != null) {
158+
this.subState.destroy();
159+
}
160+
161+
this.subState = newSubState;
162+
this.subState.create();
163+
}
164+
165+
/**
166+
* Function to get the sub-state of the state
167+
* @return The current sub-state
168+
*/
169+
public function getSubState():LfSubState {
170+
return this.subState;
114171
}
115172
}

0 commit comments

Comments
 (0)