added placement
This commit is contained in:
parent
d299222c09
commit
73daa75c5f
|
|
@ -22,11 +22,11 @@ public class EventService
|
||||||
{
|
{
|
||||||
EventPublisher.Publish(EventType.MouseMotion, e);
|
EventPublisher.Publish(EventType.MouseMotion, e);
|
||||||
}
|
}
|
||||||
if (e.type == SDL_EventType.SDL_MOUSEBUTTONDOWN && e.button.button == SDL_BUTTON_LEFT)
|
if (e.type == SDL_EventType.SDL_MOUSEBUTTONDOWN)
|
||||||
{
|
{
|
||||||
EventPublisher.Publish(EventType.MouseButtonDown, e);
|
EventPublisher.Publish(EventType.MouseButtonDown, e);
|
||||||
}
|
}
|
||||||
if (e.type == SDL_EventType.SDL_MOUSEBUTTONUP && e.button.button == SDL_BUTTON_LEFT)
|
if (e.type == SDL_EventType.SDL_MOUSEBUTTONUP)
|
||||||
{
|
{
|
||||||
EventPublisher.Publish(EventType.MouseButtonUp, e);
|
EventPublisher.Publish(EventType.MouseButtonUp, e);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,43 @@
|
||||||
|
using Mine2d.engine.system.annotations;
|
||||||
|
using Mine2d.game.backend.network.packets;
|
||||||
|
using Mine2d.game.core;
|
||||||
|
using Mine2d.game.core.data;
|
||||||
|
|
||||||
|
namespace Mine2d.game.backend.interactor;
|
||||||
|
|
||||||
|
[Interactor]
|
||||||
|
public class Place
|
||||||
|
{
|
||||||
|
[Interaction(InteractorKind.Server, PacketType.Place)]
|
||||||
|
public static void PlaceServer(PlacePacket packet)
|
||||||
|
{
|
||||||
|
var ctx = Context.Get();
|
||||||
|
var player = ctx.GameState.Players.Find(p => p.Id == packet.PlayerGuid);
|
||||||
|
if (player == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if ((player.GetCenter() - packet.Target).LengthSquared() > Constants.BreakDistance * Constants.BreakDistance)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (ctx.GameState.World.HasChunkAt(packet.Target))
|
||||||
|
{
|
||||||
|
var chunk = ctx.GameState.World.GetChunkAt(packet.Target);
|
||||||
|
var tile = chunk.GetTileAt(packet.Target);
|
||||||
|
var tileId = tile.Id;
|
||||||
|
if (tileId != 0 || player.Inventory.Hotbar[packet.Slot]?.Count <= 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
player.Inventory.Hotbar[packet.Slot].Count--;
|
||||||
|
var itemId = player.Inventory.Hotbar[packet.Slot].Id;
|
||||||
|
if (player.Inventory.Hotbar[packet.Slot].Count <= 0)
|
||||||
|
{
|
||||||
|
player.Inventory.Hotbar[packet.Slot] = null;
|
||||||
|
}
|
||||||
|
var itemTileId = ctx.TileRegistry.GetTileIdByItemId(itemId);
|
||||||
|
chunk.SetTileAt(packet.Target, STile.From(itemTileId));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -9,5 +9,6 @@ public enum PacketType
|
||||||
SelfMoved,
|
SelfMoved,
|
||||||
Break,
|
Break,
|
||||||
PlayerMoved,
|
PlayerMoved,
|
||||||
BlockBroken
|
BlockBroken,
|
||||||
|
Place
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
namespace Mine2d.game.backend.network.packets;
|
||||||
|
|
||||||
|
public class PlacePacket : Packet
|
||||||
|
{
|
||||||
|
public override PacketType Type => PacketType.Place;
|
||||||
|
|
||||||
|
public Guid PlayerGuid { get; init; }
|
||||||
|
public Vector2 Target { get; init; }
|
||||||
|
public int Slot { get; init; }
|
||||||
|
}
|
||||||
|
|
@ -35,6 +35,8 @@ public class PlayerEntity
|
||||||
{
|
{
|
||||||
var world = Context.Get().GameState.World;
|
var world = Context.Get().GameState.World;
|
||||||
bool hasCollision;
|
bool hasCollision;
|
||||||
|
bool hasSetGround = false;
|
||||||
|
bool ground = false;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
var pL = p.Position + new Vector2(0, -8);
|
var pL = p.Position + new Vector2(0, -8);
|
||||||
|
|
|
||||||
|
|
@ -28,4 +28,9 @@ public class TileRegistry
|
||||||
{
|
{
|
||||||
return this.Tiles[id];
|
return this.Tiles[id];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int GetTileIdByItemId(ItemId itemId)
|
||||||
|
{
|
||||||
|
return this.Tiles.FirstOrDefault(x => x.Value.Drop == itemId).Key;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,30 @@
|
||||||
|
using Mine2d.engine.system;
|
||||||
|
using Mine2d.engine.system.annotations;
|
||||||
|
using Mine2d.game.backend.network.packets;
|
||||||
|
|
||||||
|
namespace Mine2d.game.frontend.events;
|
||||||
|
|
||||||
|
public class PlayerPlaceInput
|
||||||
|
{
|
||||||
|
[EventListener(EventType.MouseButtonDown)]
|
||||||
|
public static void OnMouseDown(SDL_Event e)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Mouse button down");
|
||||||
|
if (e.button.button != SDL_BUTTON_RIGHT)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var ctx = Context.Get();
|
||||||
|
var amp = ctx.FrontendGameState.MousePosition
|
||||||
|
/ ctx.FrontendGameState.Settings.GameScale
|
||||||
|
+ ctx.FrontendGameState.Camera.Position;
|
||||||
|
|
||||||
|
ctx.Backend.ProcessPacket(new PlacePacket
|
||||||
|
{
|
||||||
|
PlayerGuid = ctx.FrontendGameState.PlayerGuid,
|
||||||
|
Target = amp,
|
||||||
|
Slot = ctx.FrontendGameState.HotbarIndex
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue