Browse Source

clamp player movement to world

main
Colin McMillen 11 months ago
parent
commit
7695f45cc0
  1. 15
      Shared/ShmupScene.cs
  2. 15
      Shared/ShmupWorld.cs
  3. 2
      Shared/SneakGame.cs

15
Shared/ShmupScene.cs

@ -8,17 +8,17 @@ namespace SemiColinGames {
const float DESIRED_ASPECT_RATIO = 1920.0f / 1080.0f;
private readonly Color backgroundColor = Color.DarkBlue;
private readonly Color backgroundColor = Color.Black;
private readonly GraphicsDevice graphics;
private readonly RenderTarget2D sceneTarget;
private readonly SpriteBatch spriteBatch;
public ShmupScene(GraphicsDevice graphics) {
public ShmupScene(GraphicsDevice graphics, Point worldSize) {
this.graphics = graphics;
sceneTarget = new RenderTarget2D(
graphics, 1920 / 4, 1080 / 4, false /* mipmap */,
graphics, worldSize.X, worldSize.Y, false /* mipmap */,
graphics.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
spriteBatch = new SpriteBatch(graphics);
}
@ -28,6 +28,8 @@ namespace SemiColinGames {
}
public void Dispose() {
sceneTarget.Dispose();
spriteBatch.Dispose();
GC.SuppressFinalize(this);
}
@ -43,7 +45,12 @@ namespace SemiColinGames {
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend,
SamplerState.PointClamp, DepthStencilState.Default,
RasterizerState.CullNone);
spriteBatch.Draw(Textures.Yellow2.Get, Vector2.Floor(world.Player.Position), Color.White);
// Draw player.
Texture2D playerTexture = world.Player.Texture.Get;
Vector2 spriteCenter = new Vector2(playerTexture.Width / 2, playerTexture.Height / 2);
Vector2 drawPos = Vector2.Floor(Vector2.Subtract(world.Player.Position, spriteCenter));
spriteBatch.Draw(playerTexture, drawPos, Color.White);
spriteBatch.End();
// Get ready to draw sceneTarget to screen.

15
Shared/ShmupWorld.cs

@ -5,14 +5,18 @@ using System.Collections.Generic;
namespace SemiColinGames {
public sealed class ShmupWorld : IWorld {
public struct ShmupPlayer {
public class ShmupPlayer {
public TextureRef Texture = Textures.Yellow2;
// Center of player sprite.
public Vector2 Position;
public Vector2 Position = new Vector2(32, 1080 / 8);
public Vector2 HalfSize = new Vector2(16, 10);
}
public ShmupPlayer Player;
public readonly Point Size;
public readonly ShmupPlayer Player;
public ShmupWorld() {
Size = new Point(1920 / 4, 1080 / 4);
Player = new ShmupPlayer();
}
@ -28,6 +32,11 @@ namespace SemiColinGames {
float speed = 150f;
Vector2 motion = Vector2.Multiply(input[0].Motion, modelTime * speed);
Player.Position = Vector2.Add(Player.Position, motion);
Player.Position.X = Math.Max(Player.Position.X, Player.HalfSize.X);
Player.Position.X = Math.Min(Player.Position.X, Size.X - Player.HalfSize.X);
Player.Position.Y = Math.Max(Player.Position.Y, Player.HalfSize.Y);
Player.Position.Y = Math.Min(Player.Position.Y, Size.Y - Player.HalfSize.Y);
}
}
}

2
Shared/SneakGame.cs

@ -70,7 +70,7 @@ namespace SemiColinGames {
scene?.Dispose();
// scene = new SneakScene(GraphicsDevice, ((SneakWorld) world).Camera);
// scene = new TreeScene(GraphicsDevice);
scene = new ShmupScene(GraphicsDevice);
scene = new ShmupScene(GraphicsDevice, ((ShmupWorld) world).Size);
GC.Collect();
GC.WaitForPendingFinalizers();

Loading…
Cancel
Save