mirror of
https://github.com/TheRavenwoodArts/LunaLightXMG.git
synced 2026-07-31 00:15:02 -07:00
Cannot get keyboard input to function still.
This commit is contained in:
@@ -7,6 +7,12 @@ namespace LunaLightXMG
|
|||||||
{
|
{
|
||||||
public class Enemy
|
public class Enemy
|
||||||
{
|
{
|
||||||
|
public Level Level
|
||||||
|
{
|
||||||
|
get { return level; }
|
||||||
|
}
|
||||||
|
Level level;
|
||||||
|
|
||||||
private float hsp; // Horizontal speed
|
private float hsp; // Horizontal speed
|
||||||
private float hspPrev;
|
private float hspPrev;
|
||||||
private float vsp; // Vertical speed
|
private float vsp; // Vertical speed
|
||||||
@@ -38,8 +44,9 @@ namespace LunaLightXMG
|
|||||||
random = new Random(42);
|
random = new Random(42);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Enemy(Vector2 initPosition, float initHsp, ContentManager Content)
|
public Enemy(Level level, Vector2 initPosition, float initHsp, ContentManager Content)
|
||||||
{
|
{
|
||||||
|
this.level = level;
|
||||||
hsp = initHsp;
|
hsp = initHsp;
|
||||||
vsp = 0;
|
vsp = 0;
|
||||||
hsp_frac = 0;
|
hsp_frac = 0;
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ using Microsoft.Xna.Framework.Audio;
|
|||||||
|
|
||||||
namespace LunaLightXMG
|
namespace LunaLightXMG
|
||||||
{
|
{
|
||||||
class Level : Game, IDisposable
|
public class Level : Game, IDisposable
|
||||||
{
|
{
|
||||||
// Objects
|
// Objects
|
||||||
private Camera2D camera2D;
|
private Camera2D camera2D;
|
||||||
@@ -20,7 +20,7 @@ namespace LunaLightXMG
|
|||||||
private int retroHeight = 180;
|
private int retroHeight = 180;
|
||||||
|
|
||||||
// Entities
|
// Entities
|
||||||
private List<Enemy> enemies = new List<Enemy>();
|
private List<Enemy> enemies;
|
||||||
private int numberOfEnemies;
|
private int numberOfEnemies;
|
||||||
public Player Player
|
public Player Player
|
||||||
{
|
{
|
||||||
@@ -78,8 +78,13 @@ namespace LunaLightXMG
|
|||||||
#region Loading
|
#region Loading
|
||||||
|
|
||||||
// Level Definition
|
// Level Definition
|
||||||
public Level(SpriteBatch spriteBatch, RenderHandler render_handler)
|
public Level(IServiceProvider serviceProvider,SpriteBatch spriteBatch, RenderHandler render_handler)
|
||||||
{
|
{
|
||||||
|
// Create content manager to load content used by just this level.
|
||||||
|
content = new ContentManager(serviceProvider, "Content");
|
||||||
|
|
||||||
|
renderHandler = render_handler;
|
||||||
|
|
||||||
// Initialize camera
|
// Initialize camera
|
||||||
camera2D = new Camera2D(retroWidth, retroHeight);
|
camera2D = new Camera2D(retroWidth, retroHeight);
|
||||||
// camera test - set level bounds
|
// camera test - set level bounds
|
||||||
@@ -99,6 +104,7 @@ namespace LunaLightXMG
|
|||||||
Vector2 initPlayerPosition = new Vector2(163, 0);
|
Vector2 initPlayerPosition = new Vector2(163, 0);
|
||||||
player = new Player(initPlayerPosition);
|
player = new Player(initPlayerPosition);
|
||||||
playerInput = new PlayerInput();
|
playerInput = new PlayerInput();
|
||||||
|
playerInput.HasControl = true;
|
||||||
player.LoadContent(Content);
|
player.LoadContent(Content);
|
||||||
|
|
||||||
// Init number of platforms / enemies
|
// Init number of platforms / enemies
|
||||||
@@ -106,7 +112,7 @@ namespace LunaLightXMG
|
|||||||
numberOfEnemies = 10;
|
numberOfEnemies = 10;
|
||||||
|
|
||||||
// Load enemies
|
// Load enemies
|
||||||
SpawnEnemies(numberOfEnemies);
|
enemies = SpawnEnemies(numberOfEnemies);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -129,52 +135,53 @@ namespace LunaLightXMG
|
|||||||
platforms = PlatformGenerator.CreatePlatforms(numberOfPlatforms);
|
platforms = PlatformGenerator.CreatePlatforms(numberOfPlatforms);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SpawnEnemies(int numOfEnemies)
|
private List<Enemy> SpawnEnemies(int numOfEnemies)
|
||||||
{
|
{
|
||||||
enemies = new List<Enemy>();
|
List<Enemy> newEnemies = new List<Enemy>();
|
||||||
for (int i = 0; i < numOfEnemies; i++)
|
for (int i = 0; i < numOfEnemies; i++)
|
||||||
{
|
{
|
||||||
Vector2 spawnPosition = new Vector2(16 + i * 3, -16);
|
Vector2 spawnPosition = new Vector2(16 + i * 3, -16);
|
||||||
enemies.Add(new Enemy(spawnPosition, utilities.RandomFromRange(1,3), Content));
|
Enemy enemy = new(spawnPosition, random.Next(1,3));
|
||||||
|
enemy.LoadContent(Content);
|
||||||
|
newEnemies.Add(enemy);
|
||||||
}
|
}
|
||||||
|
return newEnemies;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Update
|
#region Update
|
||||||
|
|
||||||
public void Update(GameTime gameTime)
|
public void Update(GameTime gameTime, KeyboardState keyboardState)
|
||||||
{
|
{
|
||||||
// Temporary quit game
|
// Temporary quit game
|
||||||
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
|
if (keyboardState.IsKeyDown(Keys.Escape))
|
||||||
Exit();
|
Exit();
|
||||||
// Check for game reset
|
// Check for game reset
|
||||||
if (Keyboard.GetState().IsKeyDown(Keys.R))
|
if (keyboardState.IsKeyDown(Keys.R))
|
||||||
ResetGame();
|
ResetGame();
|
||||||
else
|
|
||||||
{
|
|
||||||
// Update player
|
|
||||||
playerInput.Update();
|
|
||||||
player.Update(gameTime, playerInput, platforms);
|
|
||||||
|
|
||||||
// Update Enemies
|
// Update player
|
||||||
foreach(var enemy in enemies)
|
playerInput.Update(keyboardState);
|
||||||
enemy.Update(gameTime, player, platforms);
|
player.Update(gameTime, playerInput, platforms);
|
||||||
|
|
||||||
// Test render handler
|
// Update Enemies
|
||||||
renderHandler.UpdateZoom();
|
foreach(var enemy in enemies)
|
||||||
|
enemy.Update(gameTime, player, platforms);
|
||||||
|
|
||||||
//Update camera position
|
// Test render handler
|
||||||
camera2D.Follow(gameTime, player.position);
|
renderHandler.UpdateZoom();
|
||||||
camera2D.UpdateScreenShake(gameTime);
|
|
||||||
|
|
||||||
// Camera testing
|
//Update camera position
|
||||||
TestCameraWhilePlaying();
|
camera2D.Follow(gameTime, player.position);
|
||||||
ToggleBackgroundMusic();
|
camera2D.UpdateScreenShake(gameTime);
|
||||||
|
|
||||||
// Update keyState
|
// Camera testing
|
||||||
prevKeyState = Keyboard.GetState();
|
TestCameraWhilePlaying();
|
||||||
}
|
ToggleBackgroundMusic();
|
||||||
|
|
||||||
|
// Update keyState
|
||||||
|
prevKeyState = Keyboard.GetState();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
@@ -206,10 +213,10 @@ namespace LunaLightXMG
|
|||||||
}
|
}
|
||||||
|
|
||||||
renderHandler.EndRenderTarget();
|
renderHandler.EndRenderTarget();
|
||||||
renderHandler.DrawRenderTarget();
|
|
||||||
|
|
||||||
// Debugging
|
renderHandler.DrawRenderTarget(player, debugFont, camera2D);
|
||||||
DrawDebugInfo(spriteBatch);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@@ -233,7 +240,7 @@ namespace LunaLightXMG
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ResetGame()
|
public void ResetGame()
|
||||||
{
|
{
|
||||||
// reset cam
|
// reset cam
|
||||||
camera2D.Rotation = 0;
|
camera2D.Rotation = 0;
|
||||||
@@ -253,10 +260,14 @@ namespace LunaLightXMG
|
|||||||
LoadPlatforms();
|
LoadPlatforms();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Testing
|
||||||
|
|
||||||
private void DrawDebugInfo(SpriteBatch spriteBatch)
|
private void DrawDebugInfo(SpriteBatch spriteBatch)
|
||||||
{
|
{
|
||||||
spriteBatch.Begin();
|
|
||||||
|
|
||||||
// Draw debug messages
|
// Draw debug messages
|
||||||
spriteBatch.DrawString(debugFont, $"VSP: {player.vsp}", new Vector2(10, 10), Color.White);
|
spriteBatch.DrawString(debugFont, $"VSP: {player.vsp}", new Vector2(10, 10), Color.White);
|
||||||
spriteBatch.DrawString(debugFont, $"Grounded: {player.grounded}", new Vector2(10, 30), Color.White);
|
spriteBatch.DrawString(debugFont, $"Grounded: {player.grounded}", new Vector2(10, 30), Color.White);
|
||||||
@@ -266,20 +277,13 @@ namespace LunaLightXMG
|
|||||||
spriteBatch.DrawString(debugFont, $"Camera Rotation: {camera2D.Rotation}", new Vector2(10, 110), Color.White);
|
spriteBatch.DrawString(debugFont, $"Camera Rotation: {camera2D.Rotation}", new Vector2(10, 110), Color.White);
|
||||||
spriteBatch.DrawString(debugFont, $"Render Target Zoom: {renderHandler.ZoomLevel}", new Vector2(10, 130), Color.White);
|
spriteBatch.DrawString(debugFont, $"Render Target Zoom: {renderHandler.ZoomLevel}", new Vector2(10, 130), Color.White);
|
||||||
|
|
||||||
|
|
||||||
spriteBatch.DrawString(debugFont, $"Camera Rotation Control - Rotate left: K, Rotate right: L", new Vector2(10, 190), Color.White);
|
spriteBatch.DrawString(debugFont, $"Camera Rotation Control - Rotate left: K, Rotate right: L", new Vector2(10, 190), Color.White);
|
||||||
spriteBatch.DrawString(debugFont, $"Render Target Zoom Control - Zoom in: U, Zoom normal: I, Zoom out: O", new Vector2(10, 210), Color.White);
|
spriteBatch.DrawString(debugFont, $"Render Target Zoom Control - Zoom in: U, Zoom normal: I, Zoom out: O", new Vector2(10, 210), Color.White);
|
||||||
spriteBatch.DrawString(debugFont, $"Press P to test screen shake.", new Vector2(10, 230), Color.White);
|
spriteBatch.DrawString(debugFont, $"Press P to test screen shake.", new Vector2(10, 230), Color.White);
|
||||||
spriteBatch.DrawString(debugFont, $"Press M to toggle game music.", new Vector2(10, 250), Color.White);
|
spriteBatch.DrawString(debugFont, $"Press M to toggle game music.", new Vector2(10, 250), Color.White);
|
||||||
spriteBatch.DrawString(debugFont, $"Press R to reset level.", new Vector2(10, 270), Color.White);
|
spriteBatch.DrawString(debugFont, $"Press R to reset level.", new Vector2(10, 270), Color.White);
|
||||||
|
|
||||||
spriteBatch.End();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Testing
|
|
||||||
|
|
||||||
private void ToggleBackgroundMusic()
|
private void ToggleBackgroundMusic()
|
||||||
{
|
{
|
||||||
KeyboardState currKeyState = Keyboard.GetState();
|
KeyboardState currKeyState = Keyboard.GetState();
|
||||||
|
|||||||
+26
-31
@@ -6,6 +6,7 @@ using Microsoft.Xna.Framework.Media;
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Reflection.Metadata;
|
||||||
|
|
||||||
namespace LunaLightXMG
|
namespace LunaLightXMG
|
||||||
{
|
{
|
||||||
@@ -14,8 +15,6 @@ namespace LunaLightXMG
|
|||||||
// Class Objects
|
// Class Objects
|
||||||
private Player player;
|
private Player player;
|
||||||
private PlayerInput playerInput;
|
private PlayerInput playerInput;
|
||||||
private List<Enemy> enemies;
|
|
||||||
private int numOfEnemies;
|
|
||||||
private GraphicsDeviceManager graphics;
|
private GraphicsDeviceManager graphics;
|
||||||
private Camera2D camera2D;
|
private Camera2D camera2D;
|
||||||
private SpriteBatch spriteBatch;
|
private SpriteBatch spriteBatch;
|
||||||
@@ -28,29 +27,13 @@ namespace LunaLightXMG
|
|||||||
private RenderHandler renderHandler;
|
private RenderHandler renderHandler;
|
||||||
bool resizing;
|
bool resizing;
|
||||||
|
|
||||||
// Test sprites - eventually handle in a sprite manager?
|
|
||||||
private Texture2D platformTexture;
|
|
||||||
private Texture2D sinkArea;
|
|
||||||
|
|
||||||
// Test platforms
|
|
||||||
private Platform[] platforms;
|
|
||||||
private int numOfPlatforms;
|
|
||||||
|
|
||||||
// Level Object
|
// Level Object
|
||||||
private Level level;
|
private Level level;
|
||||||
|
|
||||||
// Music
|
// Input
|
||||||
private SoundEffect sinkBeats;
|
// We store our input states so that we only poll once per frame,
|
||||||
private SoundEffectInstance backgroundMusicInstance;
|
// then we use the same input state wherever needed
|
||||||
|
private KeyboardState keyboardState;
|
||||||
// Debugging
|
|
||||||
private SpriteFont debugFont;
|
|
||||||
private Texture2D debugGrid;
|
|
||||||
|
|
||||||
// Keyboard input
|
|
||||||
private KeyboardState prevKeyState;
|
|
||||||
// Util
|
|
||||||
Utilities util;
|
|
||||||
|
|
||||||
public LunaLightGame()
|
public LunaLightGame()
|
||||||
{
|
{
|
||||||
@@ -58,13 +41,14 @@ namespace LunaLightXMG
|
|||||||
Content.RootDirectory = "Content";
|
Content.RootDirectory = "Content";
|
||||||
IsMouseVisible = true;
|
IsMouseVisible = true;
|
||||||
|
|
||||||
|
//Vector2 initPlayerPosition = new Vector2(163,0);
|
||||||
|
//player = new Player(initPlayerPosition);
|
||||||
|
//playerInput = new PlayerInput();
|
||||||
|
|
||||||
|
|
||||||
Window.ClientSizeChanged += OnClientSizeChanged;
|
Window.ClientSizeChanged += OnClientSizeChanged;
|
||||||
Window.AllowUserResizing = true;
|
Window.AllowUserResizing = true;
|
||||||
IsMouseVisible = true;
|
IsMouseVisible = true;
|
||||||
|
|
||||||
playerInput = new PlayerInput();
|
|
||||||
|
|
||||||
//level = new Level(spriteBatch, renderHandler);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void Initialize()
|
protected override void Initialize()
|
||||||
@@ -73,8 +57,6 @@ namespace LunaLightXMG
|
|||||||
graphics.PreferredBackBufferWidth = 1920;
|
graphics.PreferredBackBufferWidth = 1920;
|
||||||
graphics.PreferredBackBufferHeight = 1080;
|
graphics.PreferredBackBufferHeight = 1080;
|
||||||
graphics.ApplyChanges();
|
graphics.ApplyChanges();
|
||||||
|
|
||||||
playerInput.HasControl = true; // May need to find a new home for this
|
|
||||||
base.Initialize();
|
base.Initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,24 +69,38 @@ namespace LunaLightXMG
|
|||||||
if (level != null)
|
if (level != null)
|
||||||
level.Dispose();
|
level.Dispose();
|
||||||
|
|
||||||
level = new Level(spriteBatch, renderHandler);
|
level = new Level(Services, spriteBatch, renderHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void Update(GameTime gameTime)
|
protected override void Update(GameTime gameTime)
|
||||||
{
|
{
|
||||||
level.Update(gameTime);
|
HandleInput(gameTime);
|
||||||
|
level.Update(gameTime, keyboardState);
|
||||||
base.Update(gameTime);
|
base.Update(gameTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void Draw(GameTime gameTime)
|
protected override void Draw(GameTime gameTime)
|
||||||
{
|
{
|
||||||
GraphicsDevice.Clear(Color.CornflowerBlue);
|
GraphicsDevice.Clear(Color.CornflowerBlue);
|
||||||
|
|
||||||
level.Draw(gameTime, spriteBatch);
|
level.Draw(gameTime, spriteBatch);
|
||||||
base.Draw(gameTime);
|
base.Draw(gameTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
|
|
||||||
|
private void HandleInput(GameTime gameTime)
|
||||||
|
{
|
||||||
|
keyboardState = Keyboard.GetState();
|
||||||
|
|
||||||
|
// Temporary quit game - eventually handle in a pause menu class
|
||||||
|
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
|
||||||
|
Exit();
|
||||||
|
|
||||||
|
if (keyboardState.IsKeyDown(Keys.R))
|
||||||
|
level.ResetGame();
|
||||||
|
}
|
||||||
|
|
||||||
private void OnClientSizeChanged(object sender, EventArgs eventArgs)
|
private void OnClientSizeChanged(object sender, EventArgs eventArgs)
|
||||||
{
|
{
|
||||||
if (!resizing && Window.ClientBounds.Width > 0 && Window.ClientBounds.Height > 0)
|
if (!resizing && Window.ClientBounds.Width > 0 && Window.ClientBounds.Height > 0)
|
||||||
@@ -116,7 +112,6 @@ namespace LunaLightXMG
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-3
@@ -18,14 +18,14 @@ namespace LunaLightXMG
|
|||||||
public bool KeyAttack { get; private set; }
|
public bool KeyAttack { get; private set; }
|
||||||
public bool KeyWallClimb { get; private set; }
|
public bool KeyWallClimb { get; private set; }
|
||||||
|
|
||||||
public void Update()
|
public void Update(KeyboardState keyboardState)
|
||||||
{
|
{
|
||||||
if (HasControl)
|
if (HasControl)
|
||||||
{
|
{
|
||||||
if (Controller)
|
if (Controller)
|
||||||
{
|
{
|
||||||
// If using controller, switch to keyboard by pressing any key
|
// If using controller, switch to keyboard by pressing any key
|
||||||
if (Keyboard.GetState().GetPressedKeys().Length > 0)
|
if (keyboardState.GetPressedKeys().Length > 0)
|
||||||
{
|
{
|
||||||
// TODO: Insert logic to create the objMouseJoyStick instance
|
// TODO: Insert logic to create the objMouseJoyStick instance
|
||||||
Controller = false;
|
Controller = false;
|
||||||
@@ -72,7 +72,7 @@ namespace LunaLightXMG
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Keyboard input
|
// Keyboard input
|
||||||
KeyboardState keyboardState = Keyboard.GetState();
|
//KeyboardState keyboardState = Keyboard.GetState();
|
||||||
|
|
||||||
KeyLeft = keyboardState.IsKeyDown(Keys.A) ? 1 : 0;
|
KeyLeft = keyboardState.IsKeyDown(Keys.A) ? 1 : 0;
|
||||||
KeyRight = keyboardState.IsKeyDown(Keys.D) ? 1 : 0;
|
KeyRight = keyboardState.IsKeyDown(Keys.D) ? 1 : 0;
|
||||||
|
|||||||
@@ -101,5 +101,31 @@ namespace LunaLightXMG
|
|||||||
spriteBatch.End();
|
spriteBatch.End();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void DrawRenderTarget(Player player, SpriteFont debugFont, Camera2D camera2D)
|
||||||
|
{
|
||||||
|
spriteBatch.Begin(samplerState: SamplerState.PointClamp);
|
||||||
|
spriteBatch.Draw(renderTarget, renderDestination, Color.White);
|
||||||
|
DebugInfo(player, debugFont, camera2D);
|
||||||
|
spriteBatch.End();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DebugInfo(Player player, SpriteFont debugFont, Camera2D camera2D)
|
||||||
|
{
|
||||||
|
// Draw debug messages
|
||||||
|
spriteBatch.DrawString(debugFont, $"VSP: {player.vsp}", new Vector2(10, 10), Color.White);
|
||||||
|
spriteBatch.DrawString(debugFont, $"Grounded: {player.grounded}", new Vector2(10, 30), Color.White);
|
||||||
|
spriteBatch.DrawString(debugFont, $"Walled: {player.walled}", new Vector2(10, 50), Color.White);
|
||||||
|
spriteBatch.DrawString(debugFont, $"Position: {player.position}", new Vector2(10, 70), Color.White);
|
||||||
|
spriteBatch.DrawString(debugFont, $"Camera Position: {camera2D.Position}", new Vector2(10, 90), Color.White);
|
||||||
|
spriteBatch.DrawString(debugFont, $"Camera Rotation: {camera2D.Rotation}", new Vector2(10, 110), Color.White);
|
||||||
|
spriteBatch.DrawString(debugFont, $"Render Target Zoom: {ZoomLevel}", new Vector2(10, 130), Color.White);
|
||||||
|
|
||||||
|
spriteBatch.DrawString(debugFont, $"Camera Rotation Control - Rotate left: K, Rotate right: L", new Vector2(10, 190), Color.White);
|
||||||
|
spriteBatch.DrawString(debugFont, $"Render Target Zoom Control - Zoom in: U, Zoom normal: I, Zoom out: O", new Vector2(10, 210), Color.White);
|
||||||
|
spriteBatch.DrawString(debugFont, $"Press P to test screen shake.", new Vector2(10, 230), Color.White);
|
||||||
|
spriteBatch.DrawString(debugFont, $"Press M to toggle game music.", new Vector2(10, 250), Color.White);
|
||||||
|
spriteBatch.DrawString(debugFont, $"Press R to reset level.", new Vector2(10, 270), Color.White);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user