Getting a lot of errors. Putting it down for the night.

This commit is contained in:
hellka7
2025-03-30 19:14:51 -07:00
parent 6022f12cb1
commit d6842844d1
3 changed files with 326 additions and 201 deletions
+17
View File
@@ -38,6 +38,23 @@ namespace LunaLightXMG
random = new Random(42); random = new Random(42);
} }
public Enemy(Vector2 initPosition, float initHsp, ContentManager Content)
{
hsp = initHsp;
vsp = 0;
hsp_frac = 0;
vsp_frac = 0;
vspJump = -4f;
grounded = false;
walled = false;
grv = 0.2f;
position = initPosition;
boundingBox = new BoundingBox(new Rectangle((int)position.X, (int)position.Y, 8, 8));
collisionManager = new CollisionManager();
random = new Random(42);
LoadContent(Content);
}
public void LoadContent(ContentManager content) public void LoadContent(ContentManager content)
{ {
texture = content.Load<Texture2D>("enemy"); texture = content.Load<Texture2D>("enemy");
+298 -4
View File
@@ -5,11 +5,20 @@ using System;
using System.IO; using System.IO;
using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Input;
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.Xna.Framework.Audio;
namespace LunaLightXMG namespace LunaLightXMG
{ {
class Level class Level : Game, IDisposable
{ {
// Objects
private Camera2D camera2D;
private PlayerInput playerInput;
// Native retro scale
private int retroWidth = 320;
private int retroHeight = 180;
// Entities // Entities
private List<Enemy> enemies = new List<Enemy>(); private List<Enemy> enemies = new List<Enemy>();
private int numberOfEnemies; private int numberOfEnemies;
@@ -24,11 +33,296 @@ namespace LunaLightXMG
private Point exit = InvalidPosition; private Point exit = InvalidPosition;
private static readonly Point InvalidPosition = new Point(-1,-1); private static readonly Point InvalidPosition = new Point(-1,-1);
// Game State
private Random random = new Random(546452);
// Physical Structure // Physical Structure
private Platform[] platforms; private Platform[] platforms;
private int numberOfPlatforms; private int numberOfPlatforms;
// Game State
private Random random = new Random(546452);
public int Score
{
get { return score; }
}
int score;
public ContentManager Content
{
get { return content; }
}
ContentManager content;
// Music
private SoundEffect sinkBeats;
private SoundEffectInstance backgroundMusicInstance;
// Test Render Target Handler
private RenderHandler renderHandler;
bool resizing;
// Debugging
private SpriteFont debugFont;
private Texture2D debugGrid;
// Keyboard input
private KeyboardState prevKeyState;
// Utility
public Utilities utilities;
// Textures
Texture2D backgroundTexture;
Texture2D platformTexture;
#region Loading
// Level Definition
public Level(SpriteBatch spriteBatch, RenderHandler render_handler)
{
// Initialize camera
camera2D = new Camera2D(retroWidth, retroHeight);
// camera test - set level bounds
camera2D.SetLevelBounds(new Rectangle(0,0,480,270)); // Testing - default 320x180
// Initialize Render Handler
// Load background layer textures. For now, all levels
// will use the same texture.
backgroundTexture = Content.Load<Texture2D>("Sink area");
// Load platform layer textures. For now, all platforms
// will use the same texture.
platformTexture = Content.Load<Texture2D>("platform");
// Load player sprite
// Test player input and movement with abstracted player class
Vector2 initPlayerPosition = new Vector2(163, 0);
player = new Player(initPlayerPosition);
playerInput = new PlayerInput();
player.LoadContent(Content);
// Init number of platforms / enemies
numberOfPlatforms = 4;
numberOfEnemies = 10;
// Load enemies
SpawnEnemies(numberOfEnemies);
// Load Platforms
LoadPlatforms();
// Debugging
debugFont = Content.Load<SpriteFont>("fonts/debugFont");
debugGrid = Content.Load<Texture2D>("debugGrid");
// Get utilities
utilities = new Utilities();
// Misc.
prevKeyState = Keyboard.GetState();
}
private void LoadPlatforms()
{
platforms = PlatformGenerator.CreatePlatforms(numberOfPlatforms);
}
private void SpawnEnemies(int numOfEnemies)
{
enemies = new List<Enemy>();
for (int i = 0; i < numOfEnemies; i++)
{
Vector2 spawnPosition = new Vector2(16 + i * 3, -16);
enemies.Add(new Enemy(spawnPosition, utilities.RandomFromRange(1,3), Content));
}
}
#endregion
#region Update
public void Update(GameTime gameTime)
{
// Temporary quit game
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
// Check for game reset
if (Keyboard.GetState().IsKeyDown(Keys.R))
ResetGame();
else
{
// Update player
playerInput.Update();
player.Update(gameTime, playerInput, platforms);
// Update Enemies
foreach(var enemy in enemies)
enemy.Update(gameTime, player, platforms);
// Test render handler
renderHandler.UpdateZoom();
//Update camera position
camera2D.Follow(gameTime, player.position);
camera2D.UpdateScreenShake(gameTime);
// Camera testing
TestCameraWhilePlaying();
ToggleBackgroundMusic();
// Update keyState
prevKeyState = Keyboard.GetState();
}
}
public void Dispose()
{
Content.Unload();
}
#endregion
#region Draw
public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
{
// // Test Render Target Handler - Draw sprites to the render (target
renderHandler.BeginRenderTarget(camera2D.GetViewMatrix());
// 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);
}
renderHandler.EndRenderTarget();
renderHandler.DrawRenderTarget();
// Debugging
DrawDebugInfo(spriteBatch);
}
#endregion
#region Methods
private void PlayBackgroundMusic(SoundEffect backgroundMusic)
{
if (backgroundMusicInstance == null)
{
backgroundMusicInstance = backgroundMusic.CreateInstance();
backgroundMusicInstance.IsLooped = true;
}
if (backgroundMusicInstance.State == SoundState.Playing)
{
backgroundMusicInstance.Stop();
}
else
{
backgroundMusicInstance.Play();
}
}
private void ResetGame()
{
// reset cam
camera2D.Rotation = 0;
camera2D.Zoom = 1f;
// reset render target zoom
renderHandler.ZoomNormalize();
// Reset player position
player.Reset(new Vector2(163, 0));
// Clear and respawn enemies
enemies.Clear();
SpawnEnemies(numberOfEnemies);
// Regenerate platforms
LoadPlatforms();
}
private void DrawDebugInfo(SpriteBatch spriteBatch)
{
spriteBatch.Begin();
// 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: {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, $"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);
spriteBatch.End();
}
#endregion
#region Testing
private void ToggleBackgroundMusic()
{
KeyboardState currKeyState = Keyboard.GetState();
if (prevKeyState.IsKeyUp(Keys.M) && currKeyState.IsKeyDown(Keys.M))
{
PlayBackgroundMusic(sinkBeats);
}
}
private void TestCameraWhilePlaying()
{
KeyboardState keyboardState = Keyboard.GetState();
var KeyK = keyboardState.IsKeyDown(Keys.K);
var KeyL = keyboardState.IsKeyDown(Keys.L);
var KeyU = keyboardState.IsKeyDown(Keys.U);
var KeyI = keyboardState.IsKeyDown(Keys.I);
var KeyO = keyboardState.IsKeyDown(Keys.O);
var KeyP = keyboardState.IsKeyDown(Keys.P);
if (KeyK)
{
camera2D.Rotation -= 0.01f;
}
if (KeyL)
{
camera2D.Rotation += 0.01f;
}
if (KeyU)
{
renderHandler.ZoomIn();
}
if (KeyI)
{
renderHandler.ZoomNormalize();
}
if (KeyO)
{
renderHandler.ZoomOut();
}
if (KeyP)
{
camera2D.ScreenShake(0.5f, 5f);
}
}
#endregion
} }
} }
+10 -196
View File
@@ -36,6 +36,9 @@ namespace LunaLightXMG
private Platform[] platforms; private Platform[] platforms;
private int numOfPlatforms; private int numOfPlatforms;
// Level Object
private Level level;
// Music // Music
private SoundEffect sinkBeats; private SoundEffect sinkBeats;
private SoundEffectInstance backgroundMusicInstance; private SoundEffectInstance backgroundMusicInstance;
@@ -59,17 +62,9 @@ namespace LunaLightXMG
Window.AllowUserResizing = true; Window.AllowUserResizing = true;
IsMouseVisible = true; IsMouseVisible = true;
// Test player input and movement with abstracted player class
Vector2 initPlayerPosition = new Vector2(163, 0);
player = new Player(initPlayerPosition);
playerInput = new PlayerInput(); playerInput = new PlayerInput();
enemies = new List<Enemy>();
// Init number of platforms / enemies //level = new Level(spriteBatch, renderHandler);
numOfPlatforms = 4;
numOfEnemies = 10;
util = new Utilities();
} }
protected override void Initialize() protected override void Initialize()
@@ -80,131 +75,35 @@ namespace LunaLightXMG
graphics.ApplyChanges(); graphics.ApplyChanges();
playerInput.HasControl = true; // May need to find a new home for this playerInput.HasControl = true; // May need to find a new home for this
prevKeyState = Keyboard.GetState();
base.Initialize(); base.Initialize();
// Initialize camera
camera2D = new Camera2D(retroWidth, retroHeight);
// camera test - set level bounds
camera2D.SetLevelBounds(new Rectangle(0,0,480,270)); // Testing - default 320x180
renderHandler.CalculateRenderDestination();
// Generate random platforms - test this idea out...
platforms = PlatformGenerator.CreatePlatforms(numOfPlatforms);
} }
protected override void LoadContent() protected override void LoadContent()
{ {
spriteBatch = new SpriteBatch(GraphicsDevice); spriteBatch = new SpriteBatch(GraphicsDevice);
// Test platform
platformTexture = Content.Load<Texture2D>("platform");
sinkArea = Content.Load<Texture2D>("Sink area");
// 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
enemies = SpawnEnemies(numOfEnemies);
// Test Render Target Handler
renderHandler = new RenderHandler(GraphicsDevice, spriteBatch, retroWidth, retroHeight); renderHandler = new RenderHandler(GraphicsDevice, spriteBatch, retroWidth, retroHeight);
// Debugging if (level != null)
debugFont = Content.Load<SpriteFont>("fonts/debugFont"); level.Dispose();
debugGrid = Content.Load<Texture2D>("debugGrid");
level = new Level(spriteBatch, renderHandler);
} }
protected override void Update(GameTime gameTime) protected override void Update(GameTime gameTime)
{ {
// Temporary quit game - eventually handle in a pause menu class level.Update(gameTime);
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
// Check for game reset
if (Keyboard.GetState().IsKeyDown(Keys.R))
ResetGame();
// 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);
}
// test render handler zoom
renderHandler.UpdateZoom();
// Update camera position
camera2D.Follow(gameTime, player.position);
camera2D.UpdateScreenShake(gameTime);
// camera testing
TestCameraWhilePlaying();
ToggleBackgroundMusic();
// Update keyState
prevKeyState = Keyboard.GetState();
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);
// // Test Render Target Handler - Draw sprites to the render (target
renderHandler.BeginRenderTarget(camera2D.GetViewMatrix());
// 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);
}
renderHandler.EndRenderTarget();
renderHandler.DrawRenderTarget();
// Debugging
DrawDebugInfo();
base.Draw(gameTime); base.Draw(gameTime);
} }
#region Methods #region Methods
private List<Enemy> SpawnEnemies(int numberOfEnemies) {
List<Enemy> enemies = new List<Enemy>();
for (int i = 0; i < numberOfEnemies; i++)
{
Vector2 spawnPosition = new Vector2(16 + i * 3, -16);
Enemy enemy = new(spawnPosition, util.RandomFromRange(1,3));
enemy.LoadContent(Content);
enemies.Add(enemy);
}
return enemies;
}
private void PlayBackgroundMusic(SoundEffect backgroundMusic)
{
if (backgroundMusicInstance == null)
{
backgroundMusicInstance = backgroundMusic.CreateInstance();
backgroundMusicInstance.IsLooped = true;
}
if (backgroundMusicInstance.State == SoundState.Playing)
{
backgroundMusicInstance.Stop();
}
else
{
backgroundMusicInstance.Play();
}
}
private void OnClientSizeChanged(object sender, EventArgs eventArgs) private void OnClientSizeChanged(object sender, EventArgs eventArgs)
{ {
@@ -216,94 +115,9 @@ namespace LunaLightXMG
} }
} }
private void DrawDebugInfo()
{
spriteBatch.Begin();
// 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: {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, $"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);
spriteBatch.End();
}
private void ResetGame()
{
// reset cam
camera2D.Rotation = 0;
camera2D.Zoom = 1f;
// reset render target zoom
renderHandler.ZoomNormalize();
// Reset player position
player.Reset(new Vector2(163, 0));
// Clear and respawn enemies
enemies.Clear();
enemies = SpawnEnemies(numOfEnemies);
// Regenerate platforms
platforms = PlatformGenerator.CreatePlatforms(numOfPlatforms);
}
#endregion #endregion
// Testing ------------------------
private void ToggleBackgroundMusic()
{
KeyboardState currKeyState = Keyboard.GetState();
if (prevKeyState.IsKeyUp(Keys.M) && currKeyState.IsKeyDown(Keys.M))
{
PlayBackgroundMusic(sinkBeats);
}
}
private void TestCameraWhilePlaying()
{
KeyboardState keyboardState = Keyboard.GetState();
var KeyK = keyboardState.IsKeyDown(Keys.K);
var KeyL = keyboardState.IsKeyDown(Keys.L);
var KeyU = keyboardState.IsKeyDown(Keys.U);
var KeyI = keyboardState.IsKeyDown(Keys.I);
var KeyO = keyboardState.IsKeyDown(Keys.O);
var KeyP = keyboardState.IsKeyDown(Keys.P);
if (KeyK)
{
camera2D.Rotation -= 0.01f;
}
if (KeyL)
{
camera2D.Rotation += 0.01f;
}
if (KeyU)
{
renderHandler.ZoomIn();
}
if (KeyI)
{
renderHandler.ZoomNormalize();
}
if (KeyO)
{
renderHandler.ZoomOut();
}
if (KeyP)
{
camera2D.ScreenShake(0.5f, 5f);
}
}
// ---------------------------------------
} }
} }