Created basic camera class.

This commit is contained in:
jme9
2025-03-17 09:15:46 -07:00
parent 306d4328e2
commit 529522499e
4 changed files with 166 additions and 17 deletions
+59 -15
View File
@@ -5,6 +5,7 @@ using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace LunaLightXMG
{
@@ -16,13 +17,12 @@ namespace LunaLightXMG
private List<Enemy> enemies;
private int numOfEnemies;
private GraphicsDeviceManager graphics;
private Camera2D camera2D;
private SpriteBatch spriteBatch;
// Native retro scale
private int retroWidth = 320;
private int retroHeight = 180;
// private int retroWidth = 480;
// private int retroHeight = 270;
// Test Render Target
private RenderTarget2D renderTarget;
@@ -47,7 +47,7 @@ namespace LunaLightXMG
// Utility
private static Random random;
public LunaLightGame()
{
graphics = new GraphicsDeviceManager(this);
@@ -70,26 +70,58 @@ namespace LunaLightXMG
numOfPlatforms = 4;
numOfEnemies = 10;
}
protected override void Initialize()
{
// Set initial window size
graphics.PreferredBackBufferWidth = 1920;
graphics.PreferredBackBufferHeight = 1080;
graphics.ApplyChanges();
playerInput.HasControl = true; // May need to find a new home for this
base.Initialize();
// Initialize camera
camera2D = new Camera2D(retroWidth, retroHeight);
// camera test - set level bounds
camera2D.SetLevelBounds(new Rectangle(0,0,480,270)); // default 320x180
CalculateRenderDestination();
// Play music
// PlayBackgroundMusic(sinkBeats);
// Generate random platforms - test this idea out...
platforms = PlatformGenerator.CreatePlatforms(numOfPlatforms);
}
// Testing camera ------------------------
private void TestCameraWhilePlaying()
{
KeyboardState keyboardState = Keyboard.GetState();
var KeyI = keyboardState.IsKeyDown(Keys.I) ? true : false;
var KeyO = keyboardState.IsKeyDown(Keys.O) ? true : false;
var KeyK = keyboardState.IsKeyDown(Keys.K) ? true : false;
var KeyL = keyboardState.IsKeyDown(Keys.L) ? true : false;
if (KeyI)
{
camera2D.Zoom += 1f;
}
if (KeyO)
{
camera2D.Zoom -= 1f;
}
if (KeyK)
{
camera2D.Rotation -= 0.01f;
}
if (KeyL)
{
camera2D.Rotation += 0.01f;
}
}
// ---------------------------------------
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
@@ -129,23 +161,32 @@ namespace LunaLightXMG
foreach (var enemy in enemies) {
enemy.Update(gameTime, player, platforms);
}
// Update camera position
camera2D.Follow(player.position);
// camera testing
TestCameraWhilePlaying();
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);
// Add camera to draw batch
spriteBatch.Begin(
transformMatrix: camera2D.GetViewMatrix(),
samplerState: SamplerState.PointClamp
);
// Debug Grid
// spriteBatch.Draw(debugGrid, new Vector2(0, 0), Color.Blue);
// spriteBatch.Draw(sinkArea, new Vector2(0, 0), Color.White);
// Test draw tiles
foreach (var platform in platforms)
{
@@ -242,7 +283,10 @@ namespace LunaLightXMG
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 Zoom: {camera2D.Zoom}", new Vector2(10, 110), Color.White);
spriteBatch.DrawString(debugFont, $"Camera Rotation: {camera2D.Rotation}", new Vector2(10, 130), Color.White);
spriteBatch.End();
}