Skip to content

Commit

Permalink
Merge pull request #5 from triplezeta/blockedtest
Browse files Browse the repository at this point in the history
Initial building implementation
  • Loading branch information
MarkSuckerberg authored Aug 22, 2023
2 parents 4ec8b89 + d758bb9 commit c1d5c9c
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 23 deletions.
72 changes: 65 additions & 7 deletions Blocktest/BlocktestGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ public class BlocktestGame : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
bool latch = false; //latch for button pressing
private bool latchBlockSelect = false;
bool buildMode = true; //true for build, false for destroy
private int blockSelected = 0;


/// <inheritdoc />
public BlocktestGame()

Check warning on line 18 in Blocktest/BlocktestGame.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field '_spriteBatch' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
Expand All @@ -24,18 +29,18 @@ protected override void Initialize()

BlockManager.Initialize();

Globals.ForegroundTilemap = new Tilemap(Globals.maxX, Globals.maxY);
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, 0));
BuildSystem.PlaceBlockCell(BlockManager.AllBlocks[0], true, new Vector2Int(i, 1));
BuildSystem.PlaceBlockCell(BlockManager.AllBlocks[0], true, new Vector2Int(i, 2));
BuildSystem.PlaceBlockCell(BlockManager.AllBlocks[0], true, new Vector2Int(i, 3));
BuildSystem.PlaceBlockCell(BlockManager.AllBlocks[2], true, new Vector2Int(i, 5));
BuildSystem.PlaceBlockCell(BlockManager.AllBlocks[0], true, new Vector2Int(i, 4));
BuildSystem.PlaceBlockCell(BlockManager.AllBlocks[1], true, new Vector2Int(i, 5));
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));
Expand All @@ -56,6 +61,59 @@ protected override void Update(GameTime gameTime)
Exit();
}

if (Keyboard.GetState().IsKeyUp(Keys.E))
{
latch = false;
}
else if (latch == false)
{
buildMode = !buildMode;
latch = true;
}
MouseState currentState = Mouse.GetState();

if (Keyboard.GetState().IsKeyUp(Keys.Q))
{
latchBlockSelect = false;
}
else if (latchBlockSelect == false)
{
blockSelected++;
if (blockSelected >= BlockManager.AllBlocks.Length)
{
blockSelected = 0;
}

latchBlockSelect = true;
}

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)));
}
}

base.Update(gameTime);
}

Expand All @@ -65,8 +123,8 @@ protected override void Draw(GameTime gameTime)
GraphicsDevice.Clear(Color.CornflowerBlue);

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

base.Draw(gameTime);
Expand Down
6 changes: 3 additions & 3 deletions Blocktest/Code/Block System/Tilemap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public Tile(Block newBlock, Vector2Int position)
{
SourceBlock = newBlock;
sprite = SourceBlock.blockSprite;
rectangle = new Rectangle(Globals.gridSize.X * position.X, Globals.Game.GraphicsDevice.Viewport.Height - (Globals.gridSize.Y * (position.Y + 1)), size, size); // HACK: This can probably be done better
rectangle = new Rectangle(Globals.gridSize.X * position.X, Globals.gridSize.Y * position.Y, size, size); // HACK: This can probably be done better
}

/// <summary>
Expand All @@ -183,10 +183,10 @@ public void UpdateAdjacencies(Vector2Int position, Tilemap tilemap)
int bitmask = 0; // Using bitmask smoothing, look it up

if (HasSmoothableTile(position + Vector2Int.Up, tilemap)) {
bitmask += 1;
bitmask += 2;
}
if (HasSmoothableTile(position + Vector2Int.Down, tilemap)) {
bitmask += 2;
bitmask += 1;
}
if (HasSmoothableTile(position + Vector2Int.Right, tilemap)) {
bitmask += 4;
Expand Down
26 changes: 13 additions & 13 deletions Blocktest/Code/BuildSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,24 @@ public static class BuildSystem
/// <param name="tilePosition">The position of the block to destroy (grid coords)</param>
public static void BreakBlockCell(bool foreground, Vector2Int tilePosition)
{
if (foreground && Globals.ForegroundTilemap.HasTile(tilePosition)) {
Tile prevTile = Globals.ForegroundTilemap.GetTile(tilePosition);
prevTile.SourceBlock.OnBreak(tilePosition, true);
Tilemap tilemap = foreground ? Globals.ForegroundTilemap : Globals.BackgroundTilemap;

if (tilemap.HasTile(tilePosition))
{
tilemap.GetTile(tilePosition).SourceBlock.OnBreak(tilePosition, true);

Check warning on line 28 in Blocktest/Code/BuildSystem.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Globals.ForegroundTilemap.SetTile(tilePosition, null);
tilemap.SetTile(tilePosition, null);

Check warning on line 30 in Blocktest/Code/BuildSystem.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
currentWorld[tilePosition.X, tilePosition.Y, 0] = 0;
} else if (!foreground && Globals.BackgroundTilemap.HasTile(tilePosition)) {
Tile prevTile = Globals.BackgroundTilemap.GetTile(tilePosition);
prevTile.SourceBlock.OnBreak(tilePosition, false);

Globals.BackgroundTilemap.SetTile(tilePosition, null);
currentWorld[tilePosition.X, tilePosition.Y, 1] = 0;
}
else
{
return;
}

Tilemap tilemap = foreground ? Globals.ForegroundTilemap : Globals.BackgroundTilemap;

foreach (Vector2Int loc in new List<Vector2Int>() { Vector2Int.Up, Vector2Int.Down, Vector2Int.Left, Vector2Int.Right }) { // Refreshes all blocks in cardinal dirs
tilemap.GetTile(tilePosition + loc).UpdateAdjacencies(tilePosition + loc, tilemap);
if(tilemap.HasTile(tilePosition + loc)){
tilemap.GetTile(tilePosition + loc).UpdateAdjacencies(tilePosition + loc, tilemap);

Check warning on line 40 in Blocktest/Code/BuildSystem.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
}
}

}
Expand Down

0 comments on commit c1d5c9c

Please sign in to comment.