added itemdrops and sounds
This commit is contained in:
parent
6d3e12597a
commit
8d686b0f84
|
|
@ -0,0 +1,61 @@
|
|||
using Mine2d.engine;
|
||||
using Mine2d.engine.system.annotations;
|
||||
using Mine2d.game.core.data.entities;
|
||||
|
||||
namespace Mine2d.game.backend.interactor;
|
||||
|
||||
[Interactor]
|
||||
public class ItemPhysics
|
||||
{
|
||||
|
||||
[Interaction(InteractorKind.Hybrid, "tick")]
|
||||
public static void TickHybrid()
|
||||
{
|
||||
var gameState = Context.Get().GameState;
|
||||
var world = gameState.World;
|
||||
foreach (var chunk in world.Chunks)
|
||||
{
|
||||
var entities = chunk.Value.Entities;
|
||||
foreach (var entity in entities)
|
||||
{
|
||||
if (entity is ItemEntity itemEntity)
|
||||
{
|
||||
itemEntity.Velocity += new Vector2(0, 0.1f);
|
||||
var nextPos = itemEntity.Position + itemEntity.Velocity;
|
||||
if (world.HasChunkAt(nextPos) && world.GetChunkAt(nextPos).HasTileAt(nextPos))
|
||||
{
|
||||
itemEntity.Velocity = new Vector2(0, 0);
|
||||
continue;
|
||||
}
|
||||
itemEntity.Position = nextPos;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Interaction(InteractorKind.Hybrid, "tick")]
|
||||
public static void Pickup()
|
||||
{
|
||||
var gameState = Context.Get().GameState;
|
||||
var world = gameState.World;
|
||||
foreach (var chunk in world.Chunks)
|
||||
{
|
||||
var entities = chunk.Value.Entities;
|
||||
foreach (var entity in entities)
|
||||
{
|
||||
foreach (var player in gameState.Players)
|
||||
{
|
||||
|
||||
var items = chunk.Value.Entities.RemoveAll(e =>
|
||||
{
|
||||
return e is ItemEntity itemEntity && (player.GetCenter() - itemEntity.Position).LengthSquared() < 8 * 8;
|
||||
});
|
||||
if (items > 0)
|
||||
{
|
||||
Context.Get().GameAudio.Play(Sound.ItemPickup);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -19,6 +19,7 @@
|
|||
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="assets/*" />
|
||||
<EmbeddedResource Include="assets/audio/*" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
|||
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -1,6 +1,11 @@
|
|||
namespace Mine2d.engine;
|
||||
|
||||
public enum Sound { }
|
||||
public enum Sound
|
||||
{
|
||||
BlockHit,
|
||||
BlockBreak,
|
||||
ItemPickup,
|
||||
}
|
||||
|
||||
public class AudioPlayer
|
||||
{
|
||||
|
|
@ -22,6 +27,7 @@ public class AudioPlayer
|
|||
{
|
||||
var buffer = this.audioFiles[name];
|
||||
var sound = SDL2.SDL_mixer.Mix_QuickLoad_WAV(buffer);
|
||||
SDL2.SDL_mixer.Mix_PlayChannel((int)name, sound, 0);
|
||||
|
||||
var ret = SDL2.SDL_mixer.Mix_PlayChannel((int)name, sound, 0);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ using Mine2d.engine;
|
|||
using Mine2d.engine.networking;
|
||||
using Mine2d.game.core.items;
|
||||
using Mine2d.game.core.tiles;
|
||||
using Mine2d.game.frontend;
|
||||
using Mine2d.game.state;
|
||||
|
||||
namespace Mine2d.game;
|
||||
|
|
@ -19,6 +20,7 @@ public class Context
|
|||
public ItemRegistry ItemRegistry { get; set; }
|
||||
public ResourceLoader ResourceLoader { get; set; }
|
||||
public TextureFactory TextureFactory { get; set; }
|
||||
public GameAudio GameAudio { get; set; }
|
||||
public static Context Instance { get; set; }
|
||||
|
||||
public Context(
|
||||
|
|
@ -42,6 +44,7 @@ public class Context
|
|||
this.TextureFactory = new TextureFactory(this.ResourceLoader, this.Renderer);
|
||||
this.TileRegistry = new TileRegistry();
|
||||
this.ItemRegistry = new ItemRegistry();
|
||||
this.GameAudio = new GameAudio();
|
||||
Instance = this;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,15 @@
|
|||
using Mine2d.engine;
|
||||
using Mine2d.engine.system.annotations;
|
||||
|
||||
namespace Mine2d.game.backend.interactor;
|
||||
|
||||
[Interactor]
|
||||
public class Audio
|
||||
{
|
||||
[Interaction(InteractorKind.Client, "blockBroken")]
|
||||
public static void BlockBroken()
|
||||
{
|
||||
var ctx = Context.Get();
|
||||
ctx.GameAudio.Play(Sound.BlockBreak);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
using Mine2d.engine;
|
||||
using Mine2d.engine.system.annotations;
|
||||
using Mine2d.game.backend.data;
|
||||
using Mine2d.game.core;
|
||||
|
|
@ -43,7 +44,8 @@ public class Breaking
|
|||
chunk.SetTileAt(player.Mining, tile with { Hits = tile.Hits + 1 });
|
||||
if (tile.Hits >= hardness)
|
||||
{
|
||||
ctx.Backend.ProcessPacket(new BlockBrokenPacket(player.Id, player.Mining, tile));
|
||||
var blockPos = new Vector2((int)Math.Floor(player.Mining.X / 16) * 16, (int)Math.Floor(player.Mining.Y / 16) * 16);
|
||||
ctx.Backend.ProcessPacket(new BlockBrokenPacket(player.Id, blockPos, tile));
|
||||
chunk.SetTileAt(player.Mining, STile.From(0));
|
||||
}
|
||||
}
|
||||
|
|
@ -62,4 +64,12 @@ public class Breaking
|
|||
}
|
||||
player.Mining = packet.Target;
|
||||
}
|
||||
|
||||
[Interaction(InteractorKind.Server, "blockBroken")]
|
||||
public static void BreakServer(BlockBrokenPacket packet)
|
||||
{
|
||||
var ctx = Context.Get();
|
||||
var tile = ctx.TileRegistry.GetTile(packet.Tile.Id);
|
||||
tile.DropItem(packet.Target);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
using Mine2d.engine;
|
||||
using Mine2d.engine.system.annotations;
|
||||
using Mine2d.game.core.data.entities;
|
||||
|
||||
|
|
@ -20,6 +21,7 @@ public class ItemPhysics
|
|||
if (entity is ItemEntity itemEntity)
|
||||
{
|
||||
itemEntity.Velocity += new Vector2(0, 0.1f);
|
||||
itemEntity.Velocity *= new Vector2(0.7f, 1f);
|
||||
var nextPos = itemEntity.Position + itemEntity.Velocity;
|
||||
if (world.HasChunkAt(nextPos) && world.GetChunkAt(nextPos).HasTileAt(nextPos))
|
||||
{
|
||||
|
|
@ -31,4 +33,26 @@ public class ItemPhysics
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Interaction(InteractorKind.Hybrid, "tick")]
|
||||
public static void Pickup()
|
||||
{
|
||||
var gameState = Context.Get().GameState;
|
||||
var world = gameState.World;
|
||||
foreach (var chunk in world.Chunks)
|
||||
{
|
||||
foreach (var player in gameState.Players)
|
||||
{
|
||||
|
||||
var items = chunk.Value.Entities.RemoveAll(e =>
|
||||
{
|
||||
return e is ItemEntity itemEntity && (player.Position + new Vector2(7, 3) - itemEntity.Position).LengthSquared() < 8 * 8;
|
||||
});
|
||||
if (items > 0)
|
||||
{
|
||||
Context.Get().GameAudio.Play(Sound.ItemPickup);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ public class Item
|
|||
var renderer = ctx.Renderer;
|
||||
var scale = ctx.FrontendGameState.Settings.GameScale;
|
||||
var targetPos = (position - ctx.FrontendGameState.Camera.Position) * scale -
|
||||
new Vector2(6 * scale);
|
||||
new Vector2(4 * scale, 6 * scale);
|
||||
renderer.DrawTexture(this.texture, (int)targetPos.X, (int)targetPos.Y, 8 * scale, 8 * scale);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
using Mine2d.engine;
|
||||
using Mine2d.game.core.data;
|
||||
|
||||
namespace Mine2d.game.core.tiles;
|
||||
|
||||
public class OreTile : Tile
|
||||
{
|
||||
public OreTile(string name, string[] texturePath, int hardness) : base(name, Context.Get().TextureFactory.CreateTexture(texturePath), hardness)
|
||||
public OreTile(string name, string[] texturePath, int hardness) : base(name, Context.Get().TextureFactory.CreateTexture(texturePath), hardness, ItemId.Air)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
using Mine2d.engine;
|
||||
using Mine2d.game.core.data;
|
||||
using Mine2d.game.core.data.entities;
|
||||
|
||||
namespace Mine2d.game.core.tiles;
|
||||
|
||||
|
|
@ -9,11 +10,13 @@ public class Tile
|
|||
public int Hardness { get; set; }
|
||||
private readonly IntPtr texture;
|
||||
private static IntPtr breakingTexture;
|
||||
public ItemId Drop { get; set; }
|
||||
|
||||
public Tile(string name, string textureName, int hardness)
|
||||
public Tile(string name, string textureName, int hardness, ItemId drop)
|
||||
{
|
||||
this.Name = name;
|
||||
this.Hardness = hardness;
|
||||
this.Drop = drop;
|
||||
|
||||
var rl = Context.Get().ResourceLoader;
|
||||
var (ptr, size) = rl.LoadToIntPtr("assets." + textureName + ".png");
|
||||
|
|
@ -27,9 +30,10 @@ public class Tile
|
|||
SDL_FreeSurface(surface);
|
||||
}
|
||||
|
||||
public Tile(string name, IntPtr texture, int hardness)
|
||||
public Tile(string name, IntPtr texture, int hardness, ItemId drop)
|
||||
{
|
||||
this.Name = name;
|
||||
this.Drop = drop;
|
||||
this.Hardness = hardness;
|
||||
this.texture = texture;
|
||||
}
|
||||
|
|
@ -63,6 +67,22 @@ public class Tile
|
|||
}
|
||||
}
|
||||
|
||||
public void DropItem(Vector2 position)
|
||||
{
|
||||
if (this.Drop == ItemId.Air)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var world = Context.Get().GameState.World;
|
||||
var itemEntity = new ItemEntity
|
||||
{
|
||||
ItemId = this.Drop,
|
||||
Position = position + new Vector2(8, 8),
|
||||
Velocity = new Vector2((float)(new Random().NextDouble() - 0.5), 0),
|
||||
};
|
||||
world.GetChunkAt(position).Entities.Add(itemEntity);
|
||||
}
|
||||
|
||||
private static void LoadBreakingTexture()
|
||||
{
|
||||
var rl = Context.Get().ResourceLoader;
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
using Mine2d.game.core.data;
|
||||
|
||||
namespace Mine2d.game.core.tiles;
|
||||
|
||||
public enum Tiles
|
||||
|
|
@ -15,7 +17,7 @@ public class TileRegistry
|
|||
|
||||
public void RegisterTile()
|
||||
{
|
||||
this.Tiles.Add(1, new Tile("stone", "stone", 5));
|
||||
this.Tiles.Add(1, new Tile("stone", "stone", 5, ItemId.Stone));
|
||||
this.Tiles.Add(2, new OreTile("ore1", new[] { "stone", "ore1" }, 5));
|
||||
this.Tiles.Add(3, new OreTile("ore2", new[] { "stone", "ore2" }, 7));
|
||||
this.Tiles.Add(4, new OreTile("ore3", new[] { "stone", "ore3" }, 8));
|
||||
|
|
|
|||
|
|
@ -0,0 +1,21 @@
|
|||
using Mine2d.engine;
|
||||
|
||||
namespace Mine2d.game.frontend;
|
||||
|
||||
public class GameAudio
|
||||
{
|
||||
private readonly AudioPlayer audioPlayer;
|
||||
|
||||
public GameAudio()
|
||||
{
|
||||
this.audioPlayer = new();
|
||||
this.audioPlayer.Register(Sound.BlockBreak, "assets.audio.block_break.wav");
|
||||
this.audioPlayer.Register(Sound.BlockHit, "assets.audio.block_hit.wav");
|
||||
this.audioPlayer.Register(Sound.ItemPickup, "assets.audio.item_pickup.wav");
|
||||
}
|
||||
|
||||
public void Play(Sound sound)
|
||||
{
|
||||
this.audioPlayer.Play(sound);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue