Skip to content

Commit

Permalink
Use Apos.Input
Browse files Browse the repository at this point in the history
  • Loading branch information
Apostolique committed Dec 14, 2023
1 parent 3281bbc commit 766d22b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 80 deletions.
60 changes: 30 additions & 30 deletions Game/GameRoot.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Apos.Input;
using FontStashSharp;
using GameProject.Towers;
using GameProject.UI;
Expand All @@ -23,15 +24,15 @@ protected override void Initialize() {
Utility.Window = Window;
Utility.Board = new Board(15, 10);
Utility.TowerList = [];
_input = new Input();
_hud = new HUD(_input);
_hud = new HUD();
_sidebarUI = new Sidebar(new Vector2(190, 10));
_difficultyCooldown = new CoolDownTimer(30f);
_latestHoveredOverTower = null;
ResetPlayingState();
base.Initialize();
}
protected override void LoadContent() {
InputHelper.Setup(this);
_s = new SpriteBatch(GraphicsDevice);
_renderTarget01 = new RenderTarget2D(GraphicsDevice, Utility.Board.GridSize * Utility.Board.Width, Utility.Board.GridSize * Utility.Board.Height);
Utility.AssetManager = new AssetManager(Content);
Expand All @@ -52,6 +53,7 @@ void Window_ClientSizeChanged(object? sender, EventArgs e) {
_renderTarget01 = new RenderTarget2D(GraphicsDevice, w, h);
}
protected override void Update(GameTime gameTime) {
InputHelper.UpdateSetup();
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
if(Utility.CurrentGameState == Utility.GameState.Playing) {
Expand All @@ -64,15 +66,15 @@ protected override void Update(GameTime gameTime) {
}
}
else if(Utility.CurrentGameState == Utility.GameState.GameOver) {
_input.Update();
if (_input.KeyPressed(Keys.R)) {
if (KeyboardCondition.Pressed(Keys.R)) {
Utility.CurrentGameState = Utility.GameState.Playing;
MediaPlayer.Stop();
Utility.AssetManager.PlayMusic("break_space", 0.3F);
ResetPlayingState();
}
}

InputHelper.UpdateCleanup();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime) {
Expand Down Expand Up @@ -116,10 +118,9 @@ private void UpdatePlayingState(GameTime gameTime) {
TowerHotkeyHandler();
TowerSwitchInput();
_sidebarUI.Update(gameTime);
_input.Update();
if(_input.MouseRightButtonPressed && IsWithinDimensions()) {
if(MouseCondition.Pressed(MouseButton.RightButton) && IsWithinDimensions()) {
if (Utility.MaxTowers > Utility.TowerList.Count)
GameRoot.AddTower(_input.MouseToGameGrid().X, _input.MouseToGameGrid().Y, Utility.TowerType.FlameThrower, Utility.TowerList.Count + 1);
AddTower(Utility.MouseToGameGrid().X, Utility.MouseToGameGrid().Y, Utility.TowerType.FlameThrower, Utility.TowerList.Count + 1);
}
for (int i = Utility.TowerList.Count - 1; i >= 0; i--) {
Tile currenttile = Utility.Board.GetTile(new Point(Utility.TowerList[i].X, Utility.TowerList[i].Y));
Expand Down Expand Up @@ -152,7 +153,7 @@ private void DrawPlayingState(SpriteBatch spriteBatch) {
// Highlight needs to be drawn before the actual towers
if (_previewTower != null) {
if (IsWithinDimensions())
_hud.DrawPlacementIndicator(spriteBatch, _previewTower, GameRoot.IsValidTileForTower(_input.MouseToGameGrid().X, _input.MouseToGameGrid().Y));
HUD.DrawPlacementIndicator(spriteBatch, _previewTower, IsValidTileForTower(Utility.MouseToGameGrid().X, Utility.MouseToGameGrid().Y));
_selectedTower!.DrawSelectionHighlight(spriteBatch);
}
foreach (Tower t in Utility.TowerList) t.Draw(spriteBatch);
Expand All @@ -179,9 +180,9 @@ private void DrawPlayingState(SpriteBatch spriteBatch) {
}

public bool IsWithinDimensions() {
if (_input.MouseToGameGrid().X >= Utility.Board.FullWidth - 1 || _input.MouseToGameGrid().X <= 0)
if (Utility.MouseToGameGrid().X >= Utility.Board.FullWidth - 1 || Utility.MouseToGameGrid().X <= 0)
return false;
else if (_input.MouseToGameGrid().Y >= Utility.Board.FullHeight - 1 || _input.MouseToGameGrid().Y <= 0)
else if (Utility.MouseToGameGrid().Y >= Utility.Board.FullHeight - 1 || Utility.MouseToGameGrid().Y <= 0)
return false;
else
return true;
Expand All @@ -190,33 +191,33 @@ private void TowerHotkeyHandler() {
int goToTowerNumber = 0;
Utility.TowerType type = Utility.TowerType.FlameThrower;
// Check what number
if (_input.KeyPressed(Keys.Q) || _input.KeyPressed(Keys.A) || _input.KeyPressed(Keys.Z))
if (KeyboardCondition.Pressed(Keys.Q) || KeyboardCondition.Pressed(Keys.A) || KeyboardCondition.Pressed(Keys.Z))
goToTowerNumber = 1;
else if (_input.KeyPressed(Keys.W) || _input.KeyPressed(Keys.S) || _input.KeyPressed(Keys.X))
else if (KeyboardCondition.Pressed(Keys.W) || KeyboardCondition.Pressed(Keys.S) || KeyboardCondition.Pressed(Keys.X))
goToTowerNumber = 2;
else if (_input.KeyPressed(Keys.E) || _input.KeyPressed(Keys.D) || _input.KeyPressed(Keys.C))
else if (KeyboardCondition.Pressed(Keys.E) || KeyboardCondition.Pressed(Keys.D) || KeyboardCondition.Pressed(Keys.C))
goToTowerNumber = 3;
else if (_input.KeyPressed(Keys.R) || _input.KeyPressed(Keys.F) || _input.KeyPressed(Keys.V))
else if (KeyboardCondition.Pressed(Keys.R) || KeyboardCondition.Pressed(Keys.F) || KeyboardCondition.Pressed(Keys.V))
goToTowerNumber = 4;
else if (_input.KeyPressed(Keys.T) || _input.KeyPressed(Keys.G) || _input.KeyPressed(Keys.B))
else if (KeyboardCondition.Pressed(Keys.T) || KeyboardCondition.Pressed(Keys.G) || KeyboardCondition.Pressed(Keys.B))
goToTowerNumber = 5;
else if (_input.KeyPressed(Keys.Y) || _input.KeyPressed(Keys.H) || _input.KeyPressed(Keys.N))
else if (KeyboardCondition.Pressed(Keys.Y) || KeyboardCondition.Pressed(Keys.H) || KeyboardCondition.Pressed(Keys.N))
goToTowerNumber = 6;

// Check type
if (_input.KeyPressed(Keys.Q) || _input.KeyPressed(Keys.W) || _input.KeyPressed(Keys.E) || _input.KeyPressed(Keys.R) || _input.KeyPressed(Keys.T) || _input.KeyPressed(Keys.Y))
if (KeyboardCondition.Pressed(Keys.Q) || KeyboardCondition.Pressed(Keys.W) || KeyboardCondition.Pressed(Keys.E) || KeyboardCondition.Pressed(Keys.R) || KeyboardCondition.Pressed(Keys.T) || KeyboardCondition.Pressed(Keys.Y))
type = Utility.TowerType.FlameThrower;
else if (_input.KeyPressed(Keys.A) || _input.KeyPressed(Keys.S) || _input.KeyPressed(Keys.D) || _input.KeyPressed(Keys.F) || _input.KeyPressed(Keys.G) || _input.KeyPressed(Keys.H))
else if (KeyboardCondition.Pressed(Keys.A) || KeyboardCondition.Pressed(Keys.S) || KeyboardCondition.Pressed(Keys.D) || KeyboardCondition.Pressed(Keys.F) || KeyboardCondition.Pressed(Keys.G) || KeyboardCondition.Pressed(Keys.H))
type = Utility.TowerType.Shotgun;
else if (_input.KeyPressed(Keys.Z) || _input.KeyPressed(Keys.X) || _input.KeyPressed(Keys.C) || _input.KeyPressed(Keys.V) || _input.KeyPressed(Keys.B) || _input.KeyPressed(Keys.N))
else if (KeyboardCondition.Pressed(Keys.Z) || KeyboardCondition.Pressed(Keys.X) || KeyboardCondition.Pressed(Keys.C) || KeyboardCondition.Pressed(Keys.V) || KeyboardCondition.Pressed(Keys.B) || KeyboardCondition.Pressed(Keys.N))
type = Utility.TowerType.Sniper;

if (Utility.TowerList.Count >= goToTowerNumber && goToTowerNumber != 0 && IsWithinDimensions()) {
foreach (Tower t in Utility.TowerList) {
if (t.HotkeyNumber == goToTowerNumber && GameRoot.IsValidTileForTower(_input.MouseToGameGrid().X, _input.MouseToGameGrid().Y)) {
if (t.HotkeyNumber == goToTowerNumber && IsValidTileForTower(Utility.MouseToGameGrid().X, Utility.MouseToGameGrid().Y)) {
_selectedTower = null;
_previewTower = null;
GameRoot.AddTower(_input.MouseToGameGrid().X, _input.MouseToGameGrid().Y, type, t.HotkeyNumber);
AddTower(Utility.MouseToGameGrid().X, Utility.MouseToGameGrid().Y, type, t.HotkeyNumber);
Utility.TowerList.Remove(t);
break;
}
Expand All @@ -226,13 +227,13 @@ private void TowerHotkeyHandler() {

private void TowerSwitchInput() {
if (_previewTower != null) {
if (_input.KeyPressed(Keys.D1)) {
if (KeyboardCondition.Pressed(Keys.D1)) {
_previewTower = new FlameThrower(_selectedTower!.X, _selectedTower.Y, _previewTower.HotkeyNumber);
}
else if (_input.KeyPressed(Keys.D2)) {
else if (KeyboardCondition.Pressed(Keys.D2)) {
_previewTower = new Shotgun(_selectedTower!.X, _selectedTower.Y, _previewTower.HotkeyNumber);
}
else if (_input.KeyPressed(Keys.D3)) {
else if (KeyboardCondition.Pressed(Keys.D3)) {
_previewTower = new Sniper(_selectedTower!.X, _selectedTower.Y, _previewTower.HotkeyNumber);
}
}
Expand All @@ -241,21 +242,21 @@ private void TowerMovementChecker() {
if (!IsWithinDimensions())
return;

if (_input.MouseLeftButtonPressed) {
if (MouseCondition.Pressed(MouseButton.LeftButton)) {
if (_previewTower == null) {
foreach (Tower t in Utility.TowerList) {
if (t.X == _input.MouseToGameGrid().X && t.Y == _input.MouseToGameGrid().Y) {
if (t.X == Utility.MouseToGameGrid().X && t.Y == Utility.MouseToGameGrid().Y) {
_selectedTower = t;
_previewTower = t;
}
}
}
//TODO: check if there is no path on this position, and if there isn't place the tower
else {
if (!GameRoot.IsValidTileForTower(_input.MouseToGameGrid().X, _input.MouseToGameGrid().Y))
if (!IsValidTileForTower(Utility.MouseToGameGrid().X, Utility.MouseToGameGrid().Y))
return;
Utility.TowerList.Remove(_selectedTower!);
GameRoot.AddTower(_input.MouseToGameGrid().X, _input.MouseToGameGrid().Y, _previewTower.Type, _previewTower.HotkeyNumber);
AddTower(Utility.MouseToGameGrid().X, Utility.MouseToGameGrid().Y, _previewTower.Type, _previewTower.HotkeyNumber);
_previewTower = null;
}
}
Expand All @@ -281,7 +282,7 @@ private void HoveringOverTowerChecker() {
if (!IsWithinDimensions())
return;
foreach (Tower t in Utility.TowerList) {
if (t.X == _input.MouseToGameGrid().X && t.Y == _input.MouseToGameGrid().Y && !t.IsDisabled)
if (t.X == Utility.MouseToGameGrid().X && t.Y == Utility.MouseToGameGrid().Y && !t.IsDisabled)
_latestHoveredOverTower = t;
}
}
Expand All @@ -300,7 +301,6 @@ private static void AddTower(int x, int y, Utility.TowerType type, int HotKeyNum
SpriteBatch _s = null!;
RenderTarget2D _renderTarget01 = null!;
Tower? _latestHoveredOverTower;
Input _input = null!;
HUD _hud = null!;
Sidebar _sidebarUI = null!;
// list for all towers
Expand Down
43 changes: 0 additions & 43 deletions Game/Input.cs

This file was deleted.

12 changes: 5 additions & 7 deletions Game/UI/HUD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
using MonoGame.Extended;

namespace GameProject.UI {
class HUD(Input input) {
public void DrawPlacementIndicator(SpriteBatch s, Tower tower, bool isValidPosition) {
class HUD() {
public static void DrawPlacementIndicator(SpriteBatch s, Tower tower, bool isValidPosition) {
if (isValidPosition) {
HUD.DrawRangeIndicators(s, new Point(_input.MouseToGameGrid().X, _input.MouseToGameGrid().Y), tower, 0.3F);
s.FillRectangle(new RectangleF(_input.MouseGridPosition().X * Utility.Board.GridSize, _input.MouseGridPosition().Y * Utility.Board.GridSize, Utility.Board.GridSize, Utility.Board.GridSize), Color.White * 0.6F);
DrawRangeIndicators(s, Utility.MouseToGameGrid(), tower, 0.3F);
s.FillRectangle(new RectangleF(Utility.MouseGridBoardPosition(), new Vector2(Utility.Board.GridSize, Utility.Board.GridSize)), Color.White * 0.6f);
}
else s.FillRectangle(new RectangleF(_input.MouseGridPosition().X * Utility.Board.GridSize, _input.MouseGridPosition().Y * Utility.Board.GridSize, Utility.Board.GridSize, Utility.Board.GridSize), Color.Red * 0.7F);
else s.FillRectangle(new RectangleF(Utility.MouseGridBoardPosition(), new Vector2(Utility.Board.GridSize, Utility.Board.GridSize)), Color.Red * 0.7f);
}
public static void DrawRangeIndicators(SpriteBatch s, Point origin, Tower tower, float transparency = 0.1f) {
float horizontal0 = origin.X - tower.MaximumRange;
Expand All @@ -36,7 +36,5 @@ public static void DrawRangeIndicators(SpriteBatch s, Point origin, Tower tower,
s.FillRectangle(maxRangeLeft, Color.Green * transparency);
s.FillRectangle(maxRangeRight, Color.Green * transparency);
}

readonly Input _input = input;
}
}
11 changes: 11 additions & 0 deletions Game/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using GameProject.Towers;
using Apos.Input;

namespace GameProject {
/// <summary>
Expand Down Expand Up @@ -53,5 +54,15 @@ public static float ScreenToGame(float value) {
return (value + Board.GridSize) / Board.GridSize;
}
public static readonly Random random = new Random();

public static Point MouseToGameGrid() {
return new Point(ScreenToGame(InputHelper.NewMouse.X), ScreenToGame(InputHelper.NewMouse.Y));
}
public static Point MouseGridPosition() {
return new Point(InputHelper.NewMouse.X / Board.GridSize, InputHelper.NewMouse.Y / Board.GridSize);
}
public static Point MouseGridBoardPosition() {
return new Point(InputHelper.NewMouse.X / Board.GridSize * Board.GridSize, InputHelper.NewMouse.Y / Board.GridSize * Board.GridSize);
}
}
}

0 comments on commit 766d22b

Please sign in to comment.