added workbench block + fixed breaking

This commit is contained in:
MasterGordon 2022-12-21 10:59:44 +01:00
parent 0520d617f3
commit 5cefdfd802
25 changed files with 115 additions and 26 deletions

View File

Before

Width:  |  Height:  |  Size: 277 B

After

Width:  |  Height:  |  Size: 277 B

View File

Before

Width:  |  Height:  |  Size: 185 B

After

Width:  |  Height:  |  Size: 185 B

View File

Before

Width:  |  Height:  |  Size: 190 B

After

Width:  |  Height:  |  Size: 190 B

View File

Before

Width:  |  Height:  |  Size: 254 B

After

Width:  |  Height:  |  Size: 254 B

View File

Before

Width:  |  Height:  |  Size: 431 B

After

Width:  |  Height:  |  Size: 431 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 257 B

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 280 B

After

Width:  |  Height:  |  Size: 282 B

View File

@ -14,10 +14,10 @@ public class TextureFactory
public IntPtr CreateTexture(string[] path) public IntPtr CreateTexture(string[] path)
{ {
var target = this.LoadTexture(path[0]); var target = this.LoadTexture("blocks." + path[0]);
for (var i = 1; i < path.Length; i++) for (var i = 1; i < path.Length; i++)
{ {
var t = this.LoadTexture(path[i]); var t = this.LoadTexture("blocks." + path[i]);
target = this.MergeTextures(target, t); target = this.MergeTextures(target, t);
} }
return target; return target;

View File

@ -1,6 +1,7 @@
using Mine2d.engine; using Mine2d.engine;
using Mine2d.engine.networking; using Mine2d.engine.networking;
using Mine2d.game.core; using Mine2d.game.core;
using Mine2d.game.core.data;
using Mine2d.game.state; using Mine2d.game.state;
namespace Mine2d.game; namespace Mine2d.game;

View File

@ -40,6 +40,13 @@ public class Breaking
player.MiningCooldown = 10; player.MiningCooldown = 10;
var tileRegistry = ctx.TileRegistry; var tileRegistry = ctx.TileRegistry;
var hardness = tileRegistry.GetTile(tileId).Hardness; var hardness = tileRegistry.GetTile(tileId).Hardness;
if(tile.Hits == 0) {
ctx.GameState.World.Cracks.Enqueue(new CrackQueueEntry
{
Pos = player.Mining,
ResetTime = DateTime.Now.AddSeconds(5)
});
}
chunk.SetTileAt(player.Mining, tile with { Hits = tile.Hits + 1 }); chunk.SetTileAt(player.Mining, tile with { Hits = tile.Hits + 1 });
if (tile.Hits >= hardness) if (tile.Hits >= hardness)
{ {
@ -55,6 +62,7 @@ public class Breaking
} }
} }
} }
Context.Get().GameState.World.ProcessCrackQueue();
} }
[Interaction(InteractorKind.Server, PacketType.Break)] [Interaction(InteractorKind.Server, PacketType.Break)]

View File

@ -1,5 +1,6 @@
using Mine2d.engine.system.annotations; using Mine2d.engine.system.annotations;
using Mine2d.game.backend.network.packets; using Mine2d.game.backend.network.packets;
using Mine2d.game.core.data;
using Mine2d.game.state; using Mine2d.game.state;
namespace Mine2d.game.backend.interactor; namespace Mine2d.game.backend.interactor;
@ -14,14 +15,15 @@ public class Connect
var player = ctx.GameState.Players.Find(p => p.Name == packet.PlayerName); var player = ctx.GameState.Players.Find(p => p.Name == packet.PlayerName);
if (player == null) if (player == null)
{ {
player = new Player
{
Name = packet.PlayerName,
Id = packet.PlayerGuid,
Position = new Vector2(512244, 5390),
Movement = new Vector2(0, 0)
};
ctx.GameState.Players.Add( ctx.GameState.Players.Add(
new Player player
{
Name = packet.PlayerName,
Id = packet.PlayerGuid,
Position = new Vector2(512244, 5390),
Movement = new Vector2(0, 0)
}
); );
} }
} }

View File

@ -23,7 +23,7 @@ public class ItemPhysics
itemEntity.Velocity += new Vector2(0, 0.1f); itemEntity.Velocity += new Vector2(0, 0.1f);
itemEntity.Velocity *= new Vector2(0.7f, 1f); itemEntity.Velocity *= new Vector2(0.7f, 1f);
var nextPos = itemEntity.Position + itemEntity.Velocity; var nextPos = itemEntity.Position + itemEntity.Velocity;
if (world.HasChunkAt(nextPos) && world.GetChunkAt(nextPos).HasTileAt(nextPos)) if (world.HasChunkAt(nextPos) && world.GetChunkAt(nextPos).HasSolidTileAt(nextPos))
{ {
itemEntity.Velocity = new Vector2(0, 0); itemEntity.Velocity = new Vector2(0, 0);
continue; continue;

View File

@ -39,8 +39,8 @@ public class PlayerEntity
var pL = p.Position + new Vector2(0, -8); var pL = p.Position + new Vector2(0, -8);
var pL2 = p.Position + new Vector2(0, -24); var pL2 = p.Position + new Vector2(0, -24);
hasCollision = hasCollision =
world.HasChunkAt(pL) && world.GetChunkAt(pL).HasTileAt(pL) world.HasChunkAt(pL) && world.GetChunkAt(pL).HasSolidTileAt(pL)
|| world.HasChunkAt(pL2) && world.GetChunkAt(pL2).HasTileAt(pL2); || world.HasChunkAt(pL2) && world.GetChunkAt(pL2).HasSolidTileAt(pL2);
if (hasCollision) if (hasCollision)
{ {
p.Movement = p.Movement with { X = 0 }; p.Movement = p.Movement with { X = 0 };
@ -52,8 +52,8 @@ public class PlayerEntity
var pR = p.Position + new Vector2(14, -8); var pR = p.Position + new Vector2(14, -8);
var pR2 = p.Position + new Vector2(14, -24); var pR2 = p.Position + new Vector2(14, -24);
hasCollision = hasCollision =
world.HasChunkAt(pR) && world.GetChunkAt(pR).HasTileAt(pR) world.HasChunkAt(pR) && world.GetChunkAt(pR).HasSolidTileAt(pR)
|| world.HasChunkAt(pR2) && world.GetChunkAt(pR2).HasTileAt(pR2); || world.HasChunkAt(pR2) && world.GetChunkAt(pR2).HasSolidTileAt(pR2);
if (hasCollision) if (hasCollision)
{ {
p.Movement = p.Movement with { X = 0 }; p.Movement = p.Movement with { X = 0 };
@ -65,8 +65,8 @@ public class PlayerEntity
var pL = p.Position + new Vector2(0, 0); var pL = p.Position + new Vector2(0, 0);
var pR = p.Position + new Vector2(14, 0); var pR = p.Position + new Vector2(14, 0);
hasCollision = hasCollision =
world.HasChunkAt(pL) && world.GetChunkAt(pL).HasTileAt(pL) world.HasChunkAt(pL) && world.GetChunkAt(pL).HasSolidTileAt(pL)
|| world.HasChunkAt(pR) && world.GetChunkAt(pR).HasTileAt(pR); || world.HasChunkAt(pR) && world.GetChunkAt(pR).HasSolidTileAt(pR);
if (hasCollision) if (hasCollision)
{ {
p.Movement = p.Movement with { Y = 0 }; p.Movement = p.Movement with { Y = 0 };
@ -78,8 +78,8 @@ public class PlayerEntity
var pL = p.Position + new Vector2(0, -28); var pL = p.Position + new Vector2(0, -28);
var pR = p.Position + new Vector2(14, -28); var pR = p.Position + new Vector2(14, -28);
hasCollision = hasCollision =
world.HasChunkAt(pL) && world.GetChunkAt(pL).HasTileAt(pL) world.HasChunkAt(pL) && world.GetChunkAt(pL).HasSolidTileAt(pL)
|| world.HasChunkAt(pR) && world.GetChunkAt(pR).HasTileAt(pR); || world.HasChunkAt(pR) && world.GetChunkAt(pR).HasSolidTileAt(pR);
if (hasCollision) if (hasCollision)
{ {
p.Movement = p.Movement with { Y = 0 }; p.Movement = p.Movement with { Y = 0 };

View File

@ -36,6 +36,20 @@ public class Chunk
return this.HasTile(tileX, tileY); return this.HasTile(tileX, tileY);
} }
public bool HasSolidTileAt(Vector2 pos)
{
return this.HasSolidTileAt((int)pos.X, (int)pos.Y);
}
public bool HasSolidTileAt(int x, int y)
{
var posInChunk = this.GetPositionInChunk(new Vector2(x, y));
var tileX = (int)Math.Floor(posInChunk.X / Constants.TileSize);
var tileY = (int)Math.Floor(posInChunk.Y / Constants.TileSize);
if(!this.HasTile(tileX, tileY)) return false;
return Context.Get().TileRegistry.GetTile(this.GetTile(tileX, tileY).Id).IsSolid();
}
public STile GetTileAt(Vector2 pos) public STile GetTileAt(Vector2 pos)
{ {
return this.GetTileAt((int)pos.X, (int)pos.Y); return this.GetTileAt((int)pos.X, (int)pos.Y);

View File

@ -4,4 +4,5 @@ public enum ItemId
{ {
Air = 0, Air = 0,
Stone = 1, Stone = 1,
Workbench = 6,
} }

View File

@ -5,6 +5,16 @@ public class ItemStack
public ItemId Id { get; set; } public ItemId Id { get; set; }
public int Count { get; set; } public int Count { get; set; }
public ItemStack()
{
}
public ItemStack(ItemId id, int count)
{
this.Id = id;
this.Count = count;
}
public IntPtr GetTexture() public IntPtr GetTexture()
{ {
return Context.Get().ItemRegistry.GetItem(this.Id).GetTexture(); return Context.Get().ItemRegistry.GetItem(this.Id).GetTexture();

View File

@ -1,8 +1,15 @@
namespace Mine2d.game.core.data; namespace Mine2d.game.core.data;
public struct CrackQueueEntry
{
public Vector2 Pos { get; set; }
public DateTime ResetTime { get; set; }
}
public class World public class World
{ {
public Dictionary<string, Chunk> Chunks { get; set; } = new Dictionary<string, Chunk>(); public Dictionary<string, Chunk> Chunks { get; set; } = new Dictionary<string, Chunk>();
public Queue<CrackQueueEntry> Cracks { get; set; } = new();
public void AddChunk(Chunk chunk) public void AddChunk(Chunk chunk)
{ {
@ -79,4 +86,27 @@ public class World
{ {
return this.HasChunkAt(x, y) && this.GetChunkAt(x, y).HasTileAt(new Vector2(x, y)); return this.HasChunkAt(x, y) && this.GetChunkAt(x, y).HasTileAt(new Vector2(x, y));
} }
public void ProcessCrackQueue()
{
var now = DateTime.Now;
var needsReorder = false;
while (this.Cracks.Count > 0 && this.Cracks.Peek().ResetTime < now)
{
var head = this.Cracks.Dequeue();
var stile = this.GetTileAt((int)head.Pos.X, (int)head.Pos.Y);
this.SetTileAt((int)head.Pos.X, (int)head.Pos.Y, stile with { Hits = stile.Hits - 1 });
if (stile.Hits >= 1)
{
this.Cracks.Enqueue(new CrackQueueEntry { Pos = head.Pos, ResetTime = now.AddSeconds(1) });
needsReorder = true;
}
}
if (needsReorder)
{
this.Cracks = new(this.Cracks.OrderBy(x => x.ResetTime));
}
Console.WriteLine(this.Cracks.Count);
Console.WriteLine(this.Cracks.ToString());
}
} }

View File

@ -12,8 +12,7 @@ public class Item
{ {
this.Id = id; this.Id = id;
this.Name = name; this.Name = name;
var texture = Context.Get().TextureFactory.CreateTexture(textureName); this.texture = Context.Get().TextureFactory.CreateTexture(textureName);
this.texture = texture;
} }
public void Render(Vector2 position) public void Render(Vector2 position)

View File

@ -9,6 +9,7 @@ public class ItemRegistry
public void RegisterItems() public void RegisterItems()
{ {
this.Register(ItemId.Stone, new Item(ItemId.Stone, "Stone", new[] { "stone" })); this.Register(ItemId.Stone, new Item(ItemId.Stone, "Stone", new[] { "stone" }));
this.Register(ItemId.Workbench, new Item(ItemId.Workbench, "Workbench", new[] { "workbench" }));
} }
public void Register(ItemId id, Item item) public void Register(ItemId id, Item item)

View File

@ -19,7 +19,7 @@ public class Tile
this.Drop = drop; this.Drop = drop;
var rl = Context.Get().ResourceLoader; var rl = Context.Get().ResourceLoader;
var (ptr, size) = rl.LoadToIntPtr("assets." + textureName + ".png"); var (ptr, size) = rl.LoadToIntPtr("assets.blocks." + textureName + ".png");
var sdlBuffer = SDL_RWFromMem(ptr, size); var sdlBuffer = SDL_RWFromMem(ptr, size);
var surface = IMG_Load_RW(sdlBuffer, 1); var surface = IMG_Load_RW(sdlBuffer, 1);
this.texture = Context.Get().Renderer.CreateTextureFromSurface(surface); this.texture = Context.Get().Renderer.CreateTextureFromSurface(surface);
@ -92,4 +92,9 @@ public class Tile
breakingTexture = Context.Get().Renderer.CreateTextureFromSurface(surface); breakingTexture = Context.Get().Renderer.CreateTextureFromSurface(surface);
SDL_FreeSurface(surface); SDL_FreeSurface(surface);
} }
public virtual bool IsSolid()
{
return true;
}
} }

View File

@ -9,6 +9,7 @@ public enum Tiles
Ore2 = 3, Ore2 = 3,
Ore3 = 4, Ore3 = 4,
Ore4 = 5, Ore4 = 5,
Workbench = 6,
} }
public class TileRegistry public class TileRegistry
@ -22,6 +23,7 @@ public class TileRegistry
this.Tiles.Add(3, new OreTile("ore2", new[] { "stone", "ore2" }, 7)); this.Tiles.Add(3, new OreTile("ore2", new[] { "stone", "ore2" }, 7));
this.Tiles.Add(4, new OreTile("ore3", new[] { "stone", "ore3" }, 8)); this.Tiles.Add(4, new OreTile("ore3", new[] { "stone", "ore3" }, 8));
this.Tiles.Add(5, new OreTile("ore4", new[] { "stone", "ore4" }, 10)); this.Tiles.Add(5, new OreTile("ore4", new[] { "stone", "ore4" }, 10));
this.Tiles.Add(6, new Workbench("workbench", "workbench", 10));
} }
public Tile GetTile(int id) public Tile GetTile(int id)

View File

@ -0,0 +1,16 @@
using Mine2d.game.core.data;
namespace Mine2d.game.core.tiles
{
public class Workbench : Tile
{
public Workbench(string name, string texturePath, int hardness) : base(name, texturePath, hardness, ItemId.Workbench)
{
}
public override bool IsSolid()
{
return false;
}
}
}

View File

@ -74,7 +74,7 @@ public class ChunkGenerator
chunk.SetTile(17, 16, STile.From(0)); chunk.SetTile(17, 16, STile.From(0));
chunk.SetTile(14, 16, STile.From(0)); chunk.SetTile(14, 16, STile.From(0));
chunk.SetTile(16, 14, STile.From(0)); chunk.SetTile(16, 14, STile.From(0));
chunk.SetTile(15, 14, STile.From(0)); chunk.SetTile(17, 16, STile.From((int)Tiles.Workbench));
return chunk; return chunk;
} }
} }

View File

@ -52,7 +52,7 @@ public class WorldRenderer : IRenderer
var chunkOffsetY = chunk.Y * Constants.TileSize * Constants.ChunkSize; var chunkOffsetY = chunk.Y * Constants.TileSize * Constants.ChunkSize;
var drawX = x * 16 + chunkOffsetX; var drawX = x * 16 + chunkOffsetX;
var drawY = y * 16 + chunkOffsetY; var drawY = y * 16 + chunkOffsetY;
if (stile.Id == 0) if (stile.Id == 0 || !tileRegistry.GetTile(stile.Id).IsSolid())
{ {
Renderer.ProcessStatus(SDL_SetRenderTarget(ctx.Renderer.GetRaw(), this.overlay)); Renderer.ProcessStatus(SDL_SetRenderTarget(ctx.Renderer.GetRaw(), this.overlay));
renderer.DrawTexture( renderer.DrawTexture(
@ -62,11 +62,11 @@ public class WorldRenderer : IRenderer
96 * scale, 96 * scale,
96 * scale 96 * scale
); );
continue;
} }
else Renderer.ProcessStatus(SDL_SetRenderTarget(ctx.Renderer.GetRaw(), IntPtr.Zero));
if (stile.Id == 0)
{ {
Renderer.ProcessStatus(SDL_SetRenderTarget(ctx.Renderer.GetRaw(), IntPtr.Zero)); continue;
} }
var tile = tileRegistry.GetTile(stile.Id); var tile = tileRegistry.GetTile(stile.Id);