Skip to content

Commit f026eff

Browse files
Completely refactored the way menus are displayed
1 parent 69fd030 commit f026eff

File tree

11 files changed

+160
-61
lines changed

11 files changed

+160
-61
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"NUMPADTWO",
4040
"NUMPADZERO",
4141
"openeditors",
42+
"rougelite",
4243
"stringfromjava",
4344
"Tiltshift"
4445
],

project.hxp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,14 @@ class Project extends HXProject
169169
this.meta.version = VERSION;
170170
this.meta.packageName = PACKAGE_NAME;
171171
this.meta.company = COMPANY;
172-
this.meta.description = 'A space themed, open world survival sandbox game.';
172+
this.meta.description = 'A space themed, open world survival rougelite game.';
173173
this.meta.companyId = COMPANY;
174174
this.meta.companyUrl = COMPANY;
175175

176176
//
177177
// CODE
178178
// =================================
179-
this.sources.push(SOURCE_DIR); // Source code location (more than one can be added)
179+
this.sources.push(SOURCE_DIR); // Source code location (more than one can be added).
180180

181181
//
182182
// PREBUILD & POSTBUILD
@@ -203,7 +203,7 @@ class Project extends HXProject
203203
this.window.width = (!isMobile()) ? 960 : 0;
204204
this.window.height = (!isMobile()) ? 720 : 0;
205205
this.window.background = 0xFF000000;
206-
this.window.fullscreen = false;
206+
this.window.fullscreen = true;
207207
this.window.resizable = isWeb();
208208
this.window.orientation = (isDesktop() || isMobile()) ? Orientation.LANDSCAPE : Orientation.AUTO;
209209
this.window.hardware = true;

source/InitState.hx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
package;
22

3+
import flixel.FlxG;
34
import flixel.FlxState;
45
import flixel.math.FlxMath;
56
import flixel.system.FlxAssets;
7+
import flixel.tweens.FlxTween;
68
import lime.app.Application;
79
import openfl.Lib;
810
import openfl.display.StageQuality;
911
import openfl.display.StageScaleMode;
12+
import starcore.StarcoreG;
1013
import starcore.backend.api.DiscordClient;
1114
import starcore.backend.data.ClientPrefs;
1215
import starcore.backend.util.AudioUtil;
16+
import starcore.backend.util.LoggerUtil.info;
1317
import starcore.backend.util.LoggerUtil;
1418
import starcore.backend.util.PathUtil;
1519
#if debug
16-
import starcore.menus.MainMenuState;
20+
import starcore.menus.main.MainMenuState;
1721
#else
1822
import starcore.menus.PSXStartupMenuState;
1923
#end
@@ -34,7 +38,7 @@ class InitState extends FlxState
3438
/**
3539
* The volume used when the window is out of focus.
3640
*/
37-
static final MINIMIZED_VOLUME:Float = 0.015;
41+
static final MINIMIZED_VOLUME:Float = 0.02;
3842

3943
/**
4044
* The duration of the volume tweening.
@@ -103,7 +107,7 @@ class InitState extends FlxState
103107
DiscordClient.initialize();
104108

105109
// Switch to the main menu state after everything has loaded.
106-
LoggerUtil.log('Setup complete! Switching to main menu');
110+
info('Setup complete! Starting game');
107111
FlxG.switchState(() -> #if !debug new PSXStartupMenuState() #else new MainMenuState() #end);
108112
}
109113

@@ -184,7 +188,7 @@ class InitState extends FlxState
184188
volumeTween = null;
185189
}
186190

187-
// Smoothly tween from current (minimized) volume back to lastVolume
191+
// Smoothly tween from current (minimized) volume back to lastVolume.
188192
volumeTween = FlxTween.num(FlxG.sound.volume, lastVolume, TWEEN_DURATION, null, (v:Float) ->
189193
{
190194
FlxG.sound.volume = v;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package starcore.graphics.states;
2+
3+
import flixel.group.FlxSpriteGroup;
4+
5+
/**
6+
* Special sprite group for display a menu UI over an `OpenableMenuState`.
7+
*/
8+
abstract class MenuDisplay extends FlxSpriteGroup
9+
{
10+
/**
11+
* The `OpenableMenuState` that `this` menu is currently opened in.
12+
*/
13+
public var parentState:OpenableMenuState;
14+
15+
public function new(parentState:OpenableMenuState)
16+
{
17+
super();
18+
this.parentState = parentState;
19+
}
20+
21+
/**
22+
* Gets called when it is first created an opened.
23+
*/
24+
public abstract function create():Void;
25+
26+
/**
27+
* Gets called when the menu is closed.
28+
*/
29+
public abstract function close():Void;
30+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package starcore.graphics.states;
2+
3+
import flixel.FlxState;
4+
5+
/**
6+
* Special state that allows you to switch `MenuSubState`s with ease.
7+
*/
8+
abstract class OpenableMenuState extends FlxState
9+
{
10+
/**
11+
* The current menu being displayed.
12+
*/
13+
var currentMenu:MenuDisplay = null;
14+
15+
/**
16+
* Switches the current menu to a different one.
17+
*
18+
* @param newMenu The new `MenuDisplay` to open.
19+
*/
20+
public function switchMenu(newMenu:MenuDisplay):Void
21+
{
22+
if (currentMenu != null)
23+
{
24+
currentMenu.close();
25+
remove(currentMenu);
26+
}
27+
currentMenu = newMenu;
28+
currentMenu.create();
29+
add(currentMenu);
30+
}
31+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
// don't have to import them in every file!
55
// ===============================================================================================
66

7-
package;
7+
package starcore;
88

99
//
1010
// FLIXEL IMPORTS
1111
// =============================
1212

1313
import flixel.FlxG;
1414
import flixel.FlxSprite;
15+
import flixel.text.FlxText;
1516
import flixel.tweens.FlxTween;
1617

1718
//

source/starcore/menus/PSXStartupMenuState.hx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package starcore.menus;
22

3-
import starcore.backend.util.EaseUtil;
4-
import flixel.util.FlxTimer;
53
import flixel.FlxState;
64
import flixel.graphics.frames.FlxAtlasFrames;
5+
import flixel.util.FlxTimer;
76
import starcore.audio.StarcoreSound;
7+
import starcore.backend.util.EaseUtil;
88
import starcore.backend.util.PathUtil;
9+
import starcore.menus.main.MainMenuState;
910

1011
/**
1112
* The startup menu state that mimics the classic PlayStation startup sequence.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package starcore.menus.main;
2+
3+
import flixel.group.FlxGroup.FlxTypedGroup;
4+
import starcore.backend.util.WorldUtil;
5+
import starcore.background.BackgroundPlanet;
6+
import starcore.background.BackgroundStar;
7+
import starcore.graphics.states.OpenableMenuState;
8+
9+
/**
10+
* State that represents the main menu of the game.
11+
* This is where the player can start a new game, load a game, or quit the game.
12+
*/
13+
class MainMenuState extends OpenableMenuState
14+
{
15+
var stars:FlxTypedGroup<BackgroundStar>;
16+
var planets:FlxTypedGroup<BackgroundPlanet>;
17+
18+
override function create():Void
19+
{
20+
super.create();
21+
22+
StarcoreG.playMenuMusic();
23+
24+
// Add the background elements.
25+
planets = WorldUtil.generatePlanets();
26+
add(planets);
27+
28+
stars = WorldUtil.generateStars();
29+
add(stars);
30+
31+
switchMenu(new TitleMenuDisplay(this));
32+
}
33+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package starcore.menus.main;
2+
3+
import starcore.graphics.states.MenuDisplay;
4+
5+
/**
6+
* Menu display for showing a list of all save files the user has.
7+
*/
8+
class SavesMenuDisplay extends MenuDisplay
9+
{
10+
override function update(elapsed:Float):Void
11+
{
12+
super.update(elapsed);
13+
14+
if (FlxG.keys.justPressed.BACKSPACE)
15+
{
16+
parentState.switchMenu(new TitleMenuDisplay(parentState));
17+
}
18+
}
19+
20+
function create():Void
21+
{
22+
add(new FlxText('bruh moment', 64));
23+
}
24+
25+
function close():Void {}
26+
}
Lines changed: 22 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,39 @@
1-
package starcore.menus;
1+
package starcore.menus.main;
22

3-
import flixel.FlxG;
4-
import flixel.FlxSprite;
5-
import flixel.addons.transition.FlxTransitionableState;
6-
import flixel.group.FlxGroup.FlxTypedGroup;
7-
import flixel.text.FlxText;
3+
import flixel.group.FlxSpriteGroup;
84
import flixel.util.FlxColor;
95
import flixel.util.FlxSpriteUtil;
10-
import flixel.util.FlxTimer;
116
import starcore.backend.Controls;
127
import starcore.backend.util.PathUtil;
13-
import starcore.backend.util.WorldUtil;
14-
import starcore.background.BackgroundPlanet;
15-
import starcore.background.BackgroundStar;
16-
import starcore.play.PlayState;
8+
import starcore.graphics.states.MenuDisplay;
179
import starcore.ui.UIClickableSprite;
1810

1911
/**
20-
* State that represents the main menu of the game.
21-
* This is where the player can start a new game, load a game, or quit the game.
12+
* The menu for displaying the title of the game.
2213
*/
23-
class MainMenuState extends FlxTransitionableState
14+
class TitleMenuDisplay extends MenuDisplay
2415
{
2516
var logo:FlxText;
2617

2718
var buttons:Array<String> = ['play', 'quit'];
28-
var buttonsGroup:FlxTypedGroup<FlxSprite>;
19+
var buttonsGroup:FlxSpriteGroup;
2920
var buttonClickFunctions:Map<String, Void->Void>;
3021
var buttonWasClicked:Bool = false;
3122

32-
var stars:FlxTypedGroup<BackgroundStar>;
33-
var planets:FlxTypedGroup<BackgroundPlanet>;
34-
var starChangeAlphaTimer:FlxTimer;
35-
36-
override function create():Void
23+
override function update(elapsed:Float):Void
3724
{
38-
super.create();
39-
40-
// Play menu music
41-
StarcoreG.playMenuMusic();
42-
43-
// Add the planets in the background
44-
planets = WorldUtil.generatePlanets();
45-
add(planets);
25+
super.update(elapsed);
4626

47-
// Add the stars in the background
48-
stars = WorldUtil.generateStars();
49-
add(stars);
27+
if (Controls.getBinds().UI_BACK_JUST_PRESSED)
28+
{
29+
StarcoreG.closeGame();
30+
}
31+
}
5032

51-
// Setup the logo
33+
function create():Void
34+
{
35+
// Setup the logo that says "STARCORE".
36+
// TODO: Replace with a better logo.
5237
logo = new FlxText();
5338
logo.text = 'STARCORE';
5439
logo.size = 165;
@@ -58,14 +43,14 @@ class MainMenuState extends FlxTransitionableState
5843
logo.setPosition((FlxG.width / 2) - (logo.width / 2), 0);
5944
add(logo);
6045

61-
// Setup the main menu buttons
62-
buttonsGroup = new FlxTypedGroup<FlxSprite>();
46+
// Setup the main menu buttons.
47+
buttonsGroup = new FlxSpriteGroup();
6348
add(buttonsGroup);
6449

6550
buttonClickFunctions = [
6651
'play' => () ->
6752
{
68-
FlxG.switchState(() -> new PlayState());
53+
parentState.switchMenu(new SavesMenuDisplay(parentState));
6954
},
7055
'quit' => () ->
7156
{
@@ -80,12 +65,7 @@ class MainMenuState extends FlxTransitionableState
8065
coolSwaggerButton.loadGraphic(PathUtil.ofSharedImage('menus/main/$btn-button'));
8166
coolSwaggerButton.scale.set(4, 4);
8267
coolSwaggerButton.updateHitbox();
83-
coolSwaggerButton.behavior.updateHoverBounds(
84-
coolSwaggerButton.x,
85-
coolSwaggerButton.y,
86-
coolSwaggerButton.width,
87-
coolSwaggerButton.height
88-
);
68+
coolSwaggerButton.behavior.updateHoverBounds(coolSwaggerButton.x, coolSwaggerButton.y, coolSwaggerButton.width, coolSwaggerButton.height);
8969
coolSwaggerButton.setPosition(0, newY);
9070
coolSwaggerButton.behavior.onClick = buttonClickFunctions.get(btn);
9171
coolSwaggerButton.behavior.onHover = () ->
@@ -105,13 +85,5 @@ class MainMenuState extends FlxTransitionableState
10585
}
10686
}
10787

108-
override function update(elapsed:Float):Void
109-
{
110-
super.update(elapsed);
111-
112-
if (Controls.getBinds().UI_BACK_JUST_PRESSED)
113-
{
114-
StarcoreG.closeGame();
115-
}
116-
}
88+
function close():Void {}
11789
}

0 commit comments

Comments
 (0)