Super happy jumping pickle man (#3)

* Added enemy class

* Added enemy class and fixed DumpFraction bug.

* Update README.md

* Fixed enemy movement bug, added game music.

* Fixed bug: Background music looping.

* Fixed enemey getting stuck on wall. Code reorganization

* Minor updates
This commit is contained in:
J. M. Ellis
2024-06-02 15:47:52 -07:00
committed by GitHub
parent ef749d0666
commit 0e8ac75cef
12 changed files with 427 additions and 180 deletions
+131 -78
View File
@@ -1,7 +1,10 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;
using System;
using System.Collections.Generic;
namespace LunaLightXMG
{
@@ -10,6 +13,7 @@ namespace LunaLightXMG
// Class Objects
private Player player;
private PlayerInput playerInput;
private List<Enemy> enemies;
private GraphicsDeviceManager graphics;
private SpriteBatch spriteBatch;
@@ -23,16 +27,21 @@ namespace LunaLightXMG
bool resizing;
// Test sprites - eventually handle in a sprite manager?
//Texture2D sprCave1Background;
//Texture2D sprCave1Foreground;
//Texture2D sprCave1Walls;
Texture2D platformTexture;
private Texture2D platformTexture;
// Test platforms
Platform[] platforms;
private Platform[] platforms;
// Music
private SoundEffect sinkBeats;
private SoundEffectInstance backgroundMusicInstance;
// Debugging
SpriteFont debugFont;
private SpriteFont debugFont;
private Texture2D debugGrid;
// Utility
private static Random random;
public Game1()
{
@@ -46,8 +55,12 @@ namespace LunaLightXMG
// Test player input and movement with abstracted player class
Vector2 initPlayerPosition = new Vector2(163, 63);
Vector2 initEnemyPosition = new Vector2(16, 16);
player = new Player(initPlayerPosition);
playerInput = new PlayerInput();
enemies = new List<Enemy>();
random = new Random(42);
}
private void OnClientSizeChanged(object sender, EventArgs eventArgs)
@@ -71,6 +84,8 @@ namespace LunaLightXMG
base.Initialize();
CalculateRenderDestination();
// Play music
PlayBackgroundMusic(sinkBeats);
// Test initialize platforms -- this should be abstracted to a class
// Also, I'm assuming using a tool like tiled will prevent having to
@@ -139,7 +154,110 @@ namespace LunaLightXMG
};
}
private void CalculateRenderDestination()
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
// Test platform
platformTexture = Content.Load<Texture2D>("platform");
// Music
sinkBeats = Content.Load<SoundEffect>("music/sinkBeats");
// load player sprite
player.LoadContent(Content); // player sprite is now handled in player.cs
// Spawn 10 enemies at the start of the level
for (int i = 0; i < 10; i++)
{
Vector2 spawnPosition = new Vector2(16 + i*3,16);
Enemy enemy = new Enemy(spawnPosition, GetRandomValueBetween1And3());
enemy.LoadContent(Content);
enemies.Add(enemy);
}
// Test Render Target
renderTarget = new RenderTarget2D(GraphicsDevice, retroWidth, retroHeight);
// Debugging
debugFont = Content.Load<SpriteFont>("fonts/debugFont");
debugGrid = Content.Load<Texture2D>("debugGrid");
}
protected override void Update(GameTime gameTime)
{
// 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();
// Test player input and movement with abstracted player class
playerInput.Update();
player.Update(gameTime, playerInput, platforms);
foreach (var enemy in enemies) {
enemy.Update(gameTime, player, platforms);
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// Test Render Target - Draw sprites to the render target
GraphicsDevice.SetRenderTarget(renderTarget);
// Test Sprite
spriteBatch.Begin(samplerState: SamplerState.PointClamp);
// Debug Grid
spriteBatch.Draw(debugGrid, new Vector2(0, 0), Color.Blue);
// Test draw tiles
foreach (var platform in platforms)
{
platform.Draw(spriteBatch, platformTexture); // Assuming all tiles use the same texture for now
}
// Test player input and movement with abstracted player class
player.Draw(spriteBatch);
foreach (var enemy in enemies)
{
enemy.Draw(spriteBatch);
}
spriteBatch.End();
// Test Render Target - Draw render target to the screen
GraphicsDevice.SetRenderTarget(null);
spriteBatch.Begin(samplerState: SamplerState.PointClamp);
spriteBatch.Draw(renderTarget, renderDestination, Color.White);
spriteBatch.End();
// Debugging
DrawDebugInfo();
base.Draw(gameTime);
}
// Method Implementation -------------------------------------------------------------------------------
private void PlayBackgroundMusic(SoundEffect backgroundMusic)
{
if (backgroundMusicInstance == null || backgroundMusicInstance.State != SoundState.Playing)
{
if (backgroundMusicInstance != null)
{
backgroundMusicInstance.Dispose();
}
backgroundMusicInstance = backgroundMusic.CreateInstance();
backgroundMusicInstance.IsLooped = true;
backgroundMusicInstance.Play();
}
}
private void CalculateRenderDestination()
{
Point size = GraphicsDevice.Viewport.Bounds.Size;
@@ -158,77 +276,7 @@ namespace LunaLightXMG
renderDestination.X = (size.X - renderDestination.Width) / 2;
renderDestination.Y = (size.Y - renderDestination.Height) / 2;
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
//sprCave1Background = Content.Load<Texture2D>("spr_cave1_background");
//sprCave1Foreground = Content.Load<Texture2D>("spr_Cave1_foreground");
//sprCave1Walls = Content.Load<Texture2D>("spr_Cave1_walls");
// Test platform
platformTexture = Content.Load<Texture2D>("platform");
// load player sprite
player.LoadContent(Content); // player sprite is now handled in player.cs
// Test Render Target
renderTarget = new RenderTarget2D(GraphicsDevice, retroWidth, retroHeight);
// Debugging
debugFont = Content.Load<SpriteFont>("fonts/debugFont");
}
protected override void Update(GameTime gameTime)
{
// 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();
// Test player input and movement with abstracted player class
playerInput.Update();
player.Update(gameTime, playerInput, platforms);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// Test Render Target - Draw sprites to the render target
GraphicsDevice.SetRenderTarget(renderTarget);
// Test Sprite
spriteBatch.Begin(samplerState: SamplerState.PointClamp);
// Draw sprites here
//spriteBatch.Draw(sprCave1Background,new Vector2(0,0),Color.White);
// Test draw tiles
foreach (var platform in platforms)
{
platform.Draw(spriteBatch, platformTexture); // Assuming all tiles use the same texture for now
}
// Test player input and movement with abstracted player class
player.Draw(spriteBatch);
//spriteBatch.Draw(sprCave1Walls, new Vector2(0, 0), Color.White);
//spriteBatch.Draw(sprCave1Foreground, new Vector2(0, 0), Color.White);
spriteBatch.End();
// Test Render Target - Draw render target to the screen
GraphicsDevice.SetRenderTarget(null);
spriteBatch.Begin(samplerState: SamplerState.PointClamp);
spriteBatch.Draw(renderTarget, renderDestination, Color.White);
spriteBatch.End();
// Debugging
DrawDebugInfo();
base.Draw(gameTime);
}
// Debugging
private void DrawDebugInfo()
{
spriteBatch.Begin();
@@ -241,5 +289,10 @@ namespace LunaLightXMG
spriteBatch.End();
}
public static float GetRandomValueBetween1And3()
{
return (float)(1 + 2 * random.NextDouble());
}
}
}