Skip to content

Commit

Permalink
Adds scenes
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkSuckerberg committed Aug 28, 2023
1 parent 24a507b commit a714c3a
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 104 deletions.
109 changes: 7 additions & 102 deletions Blocktest/BlocktestGame.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
using Blocktest.Rendering;
using Microsoft.Xna.Framework.Input;
using Blocktest.Rendering;
using Blocktest.Scenes;

namespace Blocktest
{
/// <inheritdoc />
public class BlocktestGame : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
bool latch = false; //latch for button pressing
private bool latchBlockSelect = false; //same but for block selection
bool buildMode = true; //true for build, false for destroy
private int blockSelected = 0; //ID of the block to place

private Scene? _currentScene;

/// <inheritdoc />
public BlocktestGame()
Expand All @@ -23,122 +18,32 @@ public BlocktestGame()
}

/// <inheritdoc />
protected override void Initialize()
{
Globals.Game = this;

protected override void Initialize() {
BlockManager.Initialize();

Globals.BackgroundTilemap = new Tilemap(Globals.maxX, Globals.maxY);
Globals.ForegroundTilemap = new Tilemap(Globals.maxX, Globals.maxY);

base.Initialize();

for (int i = 0; i < Globals.maxX; i++) {
BuildSystem.PlaceBlockCell(BlockManager.AllBlocks[2], true, new Vector2Int(i, 5));
BuildSystem.PlaceBlockCell(BlockManager.AllBlocks[0], true, new Vector2Int(i, 4));
BuildSystem.PlaceBlockCell(BlockManager.AllBlocks[0], true, new Vector2Int(i, 3));
BuildSystem.PlaceBlockCell(BlockManager.AllBlocks[0], true, new Vector2Int(i, 2));
BuildSystem.PlaceBlockCell(BlockManager.AllBlocks[0], true, new Vector2Int(i, 1));
BuildSystem.PlaceBlockCell(BlockManager.AllBlocks[1], true, new Vector2Int(i, 0));
}

BuildSystem.PlaceBlockCell(BlockManager.AllBlocks[0], true, new Vector2Int(20, 20));
}

/// <inheritdoc />
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
Drawable.ContentManager = Content;
BlockManager.LoadBlockSprites(Content);
_currentScene = new GameScene(this);
}

/// <inheritdoc />
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) {
Exit();
}

//press E to toggle build/destroy
if (Keyboard.GetState().IsKeyUp(Keys.E))
{
latch = false;
}
else if (latch == false)
{
buildMode = !buildMode;
latch = true;
}

//for block placement
MouseState currentState = Mouse.GetState();

//Q changes which block you have selected
if (Keyboard.GetState().IsKeyUp(Keys.Q))
{
latchBlockSelect = false;
}
else if (latchBlockSelect == false)
{
blockSelected++;
if (blockSelected >= BlockManager.AllBlocks.Length)
{
blockSelected = 0;
}

latchBlockSelect = true;
}

//build and destroy mode
if (buildMode)
{
if(currentState.LeftButton == ButtonState.Pressed)
{
BuildSystem.PlaceBlockCell(BlockManager.AllBlocks[blockSelected], true,
new Vector2Int(MathHelper.Clamp(currentState.X / Globals.gridSize.X, 0, Globals.maxX),
MathHelper.Clamp(currentState.Y / Globals.gridSize.Y, 0, Globals.maxY)));
} else if (currentState.RightButton == ButtonState.Pressed) {
BuildSystem.PlaceBlockCell(BlockManager.AllBlocks[blockSelected], false,
new Vector2Int(MathHelper.Clamp(currentState.X / Globals.gridSize.X, 0, Globals.maxX),
MathHelper.Clamp(currentState.Y / Globals.gridSize.Y, 0, Globals.maxY)));
}
}
else
{
if(currentState.LeftButton == ButtonState.Pressed)
{
BuildSystem.BreakBlockCell( true,
new Vector2Int(MathHelper.Clamp(currentState.X / Globals.gridSize.X, 0, Globals.maxX),
MathHelper.Clamp(currentState.Y / Globals.gridSize.Y, 0, Globals.maxY)));
} else if (currentState.RightButton == ButtonState.Pressed) {
BuildSystem.BreakBlockCell( false,
new Vector2Int(MathHelper.Clamp(currentState.X / Globals.gridSize.X, 0, Globals.maxX),
MathHelper.Clamp(currentState.Y / Globals.gridSize.Y, 0, Globals.maxY)));
}
}
_currentScene?.Update(gameTime);

base.Update(gameTime);
}

/// <inheritdoc />
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);

_spriteBatch.Begin();
Globals.BackgroundTilemap.Draw(_spriteBatch);
Globals.ForegroundTilemap.Draw(_spriteBatch);
// placement preview
if(buildMode)
_spriteBatch.Draw(BlockManager.AllBlocks[blockSelected].blockSprite.Texture,
new Vector2Int(Mouse.GetState().X - (Mouse.GetState().X % 8),
(Mouse.GetState().Y - Mouse.GetState().Y % 8)),
new Rectangle(1, 1, 10, 10), Color.DimGray);
_currentScene?.Draw(gameTime, GraphicsDevice);

_spriteBatch.End();

base.Draw(gameTime);
}
}
Expand Down
2 changes: 0 additions & 2 deletions Blocktest/Code/Globals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
{
public static class Globals
{
/// <summary> The main Game instance. </summary>
public static BlocktestGame Game;

/// <summary> Tilemap for foreground objects. </summary>
private static Tilemap foregroundTilemap;

Check warning on line 7 in Blocktest/Code/Globals.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'foregroundTilemap' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
Expand Down
105 changes: 105 additions & 0 deletions Blocktest/Code/Scenes/GameScene.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using Microsoft.Xna.Framework.Input;
namespace Blocktest.Scenes;

public class GameScene : Scene {
private readonly BlocktestGame _game;
private readonly SpriteBatch _spriteBatch;

bool latch = false; //latch for button pressing
private bool latchBlockSelect = false; //same but for block selection
bool buildMode = true; //true for build, false for destroy
private int blockSelected = 0; //ID of the block to place

public void Update(GameTime gameTime) {
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) {
_game.Exit();
}

//press E to toggle build/destroy
if (Keyboard.GetState().IsKeyUp(Keys.E))
{
latch = false;
}
else if (latch == false)
{
buildMode = !buildMode;
latch = true;
}

//for block placement
MouseState currentState = Mouse.GetState();

//Q changes which block you have selected
if (Keyboard.GetState().IsKeyUp(Keys.Q))
{
latchBlockSelect = false;
}
else if (latchBlockSelect == false)
{
blockSelected++;
if (blockSelected >= BlockManager.AllBlocks.Length)
{
blockSelected = 0;
}

latchBlockSelect = true;
}

//build and destroy mode
if (buildMode)
{
if(currentState.LeftButton == ButtonState.Pressed)
{
BuildSystem.PlaceBlockCell(BlockManager.AllBlocks[blockSelected], true,
new Vector2Int(MathHelper.Clamp(currentState.X / Globals.gridSize.X, 0, Globals.maxX),
MathHelper.Clamp(currentState.Y / Globals.gridSize.Y, 0, Globals.maxY)));
} else if (currentState.RightButton == ButtonState.Pressed) {
BuildSystem.PlaceBlockCell(BlockManager.AllBlocks[blockSelected], false,
new Vector2Int(MathHelper.Clamp(currentState.X / Globals.gridSize.X, 0, Globals.maxX),
MathHelper.Clamp(currentState.Y / Globals.gridSize.Y, 0, Globals.maxY)));
}
}
else
{
if(currentState.LeftButton == ButtonState.Pressed)
{
BuildSystem.BreakBlockCell( true,
new Vector2Int(MathHelper.Clamp(currentState.X / Globals.gridSize.X, 0, Globals.maxX),
MathHelper.Clamp(currentState.Y / Globals.gridSize.Y, 0, Globals.maxY)));
} else if (currentState.RightButton == ButtonState.Pressed) {
BuildSystem.BreakBlockCell( false,
new Vector2Int(MathHelper.Clamp(currentState.X / Globals.gridSize.X, 0, Globals.maxX),
MathHelper.Clamp(currentState.Y / Globals.gridSize.Y, 0, Globals.maxY)));
}
}

}

public void Draw(GameTime gameTime, GraphicsDevice graphicsDevice) {
graphicsDevice.Clear(Color.CornflowerBlue);

_spriteBatch.Begin();
Globals.BackgroundTilemap.Draw(_spriteBatch);
Globals.ForegroundTilemap.Draw(_spriteBatch);
_spriteBatch.End();
}

public GameScene(BlocktestGame game) {
_spriteBatch = new SpriteBatch(game.GraphicsDevice);
_game = game;

Globals.BackgroundTilemap = new Tilemap(Globals.maxX, Globals.maxY);
Globals.ForegroundTilemap = new Tilemap(Globals.maxX, Globals.maxY);

for (int i = 0; i < Globals.maxX; i++) {
BuildSystem.PlaceBlockCell(BlockManager.AllBlocks[2], true, new Vector2Int(i, 5));
BuildSystem.PlaceBlockCell(BlockManager.AllBlocks[0], true, new Vector2Int(i, 4));
BuildSystem.PlaceBlockCell(BlockManager.AllBlocks[0], true, new Vector2Int(i, 3));
BuildSystem.PlaceBlockCell(BlockManager.AllBlocks[0], true, new Vector2Int(i, 2));
BuildSystem.PlaceBlockCell(BlockManager.AllBlocks[0], true, new Vector2Int(i, 1));
BuildSystem.PlaceBlockCell(BlockManager.AllBlocks[1], true, new Vector2Int(i, 0));
}

BuildSystem.PlaceBlockCell(BlockManager.AllBlocks[0], true, new Vector2Int(20, 20));
}
}
7 changes: 7 additions & 0 deletions Blocktest/Code/Scenes/Scene.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Blocktest.Scenes;

public interface Scene {
public void Update(GameTime gameTime);

public void Draw(GameTime gameTime, GraphicsDevice graphicsDevice);
}

0 comments on commit a714c3a

Please sign in to comment.