Skip to content

Commit

Permalink
Better title screen
Browse files Browse the repository at this point in the history
  • Loading branch information
Xeeynamo committed May 23, 2020
1 parent 7214c29 commit 3852e4e
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 9 deletions.
23 changes: 23 additions & 0 deletions OpenKh.Game/Infrastructure/InputManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace OpenKh.Game.Infrastructure
{
public class InputManager
{
private GamePadState pad;
private KeyboardState keyboard;
private KeyboardState prevKeyboard;

public bool IsExit => pad.Buttons.Back == ButtonState.Pressed || keyboard.IsKeyDown(Keys.Escape);
public bool IsDown => keyboard.IsKeyDown(Keys.Down) && !prevKeyboard.IsKeyDown(Keys.Down);
public bool IsUp => keyboard.IsKeyDown(Keys.Up) && !prevKeyboard.IsKeyDown(Keys.Up);

public void Update()
{
pad = GamePad.GetState(PlayerIndex.One);
prevKeyboard = keyboard;
keyboard = Keyboard.GetState();
}
}
}
1 change: 1 addition & 0 deletions OpenKh.Game/Infrastructure/StateInitDesc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class StateInitDesc
{
public IDataContent DataContent { get; set; }
public ArchiveManager ArchiveManager { get; set; }
public InputManager InputManager { get; set; }
public GraphicsDeviceManager GraphicsDevice { get; set; }
}
}
15 changes: 11 additions & 4 deletions OpenKh.Game/OpenKhGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,23 @@ public class OpenKhGame : Microsoft.Xna.Framework.Game

private readonly IDataContent dataContent;
private readonly ArchiveManager archiveManager;
private readonly InputManager inputManager;
private IState state;

public OpenKhGame()
{
graphics = new GraphicsDeviceManager(this);
graphics = new GraphicsDeviceManager(this)
{
PreferredBackBufferWidth = 512,
PreferredBackBufferHeight = 448
};

Content.RootDirectory = "Content";
IsMouseVisible = true;

dataContent = new StandardDataContent();
archiveManager = new ArchiveManager(dataContent);
inputManager = new InputManager();
}

protected override void Initialize()
Expand All @@ -35,6 +42,7 @@ protected override void Initialize()
{
DataContent = dataContent,
ArchiveManager = archiveManager,
InputManager = inputManager,
GraphicsDevice = graphics
});
base.Initialize();
Expand All @@ -50,11 +58,10 @@ protected override void LoadContent()

protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
inputManager.Update();
if (inputManager.IsExit)
Exit();

// TODO: Add your update logic here

state?.Update(new DeltaTimes
{

Expand Down
78 changes: 73 additions & 5 deletions OpenKh.Game/States/TitleState.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using OpenKh.Engine;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using OpenKh.Engine;
using OpenKh.Engine.Renderers;
using OpenKh.Game.Infrastructure;
using OpenKh.Kh2;
Expand All @@ -10,21 +12,37 @@ namespace OpenKh.Game.States
{
public class TitleState : IState
{
private const int BackgroundScreen = 14;
private const int Menu1 = 0;
private const int Menu2 = 2;
private const int Menu1OptionsCount = 2;
private const int Menu2OptionsCount = 3;
private const bool TheaterModeUnlocked = true;

private ArchiveManager archiveManager;
private InputManager inputManager;
private MonoDrawing drawing;
private Layout titleLayout;
private IEnumerable<ISurface> titleImages;
private LayoutRenderer layoutRenderer;
private LayoutRenderer layoutRendererFg;
private LayoutRenderer layoutRendererBg;
private int optionSelected;

public void Initialize(StateInitDesc initDesc)
{
archiveManager = initDesc.ArchiveManager;
inputManager = initDesc.InputManager;
drawing = new MonoDrawing(initDesc.GraphicsDevice.GraphicsDevice);

archiveManager.LoadArchive("menu/fm/title.2ld");
titleLayout = archiveManager.Get<Layout>("titl");
titleImages = archiveManager.Get<Imgz>("titl")?.Images?.Select(x => drawing.CreateSurface(x));
layoutRenderer = new LayoutRenderer(titleLayout, drawing, titleImages);

layoutRendererBg = new LayoutRenderer(titleLayout, drawing, titleImages);
layoutRendererBg.SelectedSequenceGroupIndex = BackgroundScreen;

layoutRendererFg = new LayoutRenderer(titleLayout, drawing, titleImages);
layoutRendererFg.SelectedSequenceGroupIndex = TheaterModeUnlocked ? Menu2 : Menu1;
}

public void Destroy()
Expand All @@ -34,12 +52,62 @@ public void Destroy()

public void Update(DeltaTimes deltaTimes)
{
layoutRenderer.FrameIndex++;
CheckLoop(layoutRendererBg);
CheckLoop(layoutRendererFg);

var pad = GamePad.GetState(PlayerIndex.One);
var keyboard = Keyboard.GetState();

var currentOption = optionSelected;
if (inputManager.IsUp)
{
currentOption--;
if (currentOption < 0)
currentOption = GetMaxOptionsCount() - 1;
}
else if (inputManager.IsDown)
{
currentOption++;
if (currentOption >= GetMaxOptionsCount())
currentOption = 0;
}

if (currentOption != optionSelected)
SetOption(currentOption);
}

public void Draw(DeltaTimes deltaTimes)
{
layoutRenderer.Draw();
layoutRendererBg.Draw();
layoutRendererFg.Draw();
}

private int GetMaxOptionsCount() => TheaterModeUnlocked ? Menu2OptionsCount : Menu1OptionsCount;

private void SetOption(int option)
{
optionSelected = option;
layoutRendererFg.SelectedSequenceGroupIndex = optionSelected + (TheaterModeUnlocked ? Menu2 : Menu1);
}

private void CheckLoop(LayoutRenderer layout)
{
switch (layout.SelectedSequenceGroupIndex)
{
case Menu1:
case Menu1 + 1:
case Menu2:
case Menu2 + 1:
case Menu2 + 2:
if (layout.FrameIndex > 178)
layout.FrameIndex = 70;
layout.FrameIndex++;
break;
case BackgroundScreen:
if (layout.FrameIndex < 119)
layout.FrameIndex++;
break;
}
}
}
}

0 comments on commit 3852e4e

Please sign in to comment.