added camera
This commit is contained in:
parent
74245c8409
commit
7213239038
|
|
@ -16,14 +16,14 @@ class Backend : IBackend
|
|||
while (pendingPackets.Count > 0)
|
||||
{
|
||||
var packet = pendingPackets.Dequeue();
|
||||
this.ProcessPacket(packet);
|
||||
this.publisher.Publish(packet);
|
||||
}
|
||||
this.sendGameState();
|
||||
}
|
||||
|
||||
public void ProcessPacket(ValueType packet)
|
||||
{
|
||||
this.publisher.Publish(packet);
|
||||
this.sendGameState();
|
||||
pendingPackets.Enqueue(packet);
|
||||
}
|
||||
|
||||
public void Init()
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ class Publisher
|
|||
continue;
|
||||
}
|
||||
var attr = (Interaction)attrs2[0];
|
||||
if (attr.Kind != this.kind && attr.Kind != InteractorKind.Hybrid)
|
||||
if (attr.Kind == InteractorKind.Server && this.kind == InteractorKind.Client)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
|
@ -67,8 +67,12 @@ class Publisher
|
|||
public void Publish(ValueType packet)
|
||||
{
|
||||
var type = PacketUtils.GetType(packet);
|
||||
if (type != "tick")
|
||||
Console.WriteLine("Publishing packet: " + type);
|
||||
if (subscribers.ContainsKey(type))
|
||||
{
|
||||
if (type != "tick")
|
||||
Console.WriteLine("Found " + subscribers[type].Count + " subscribers");
|
||||
foreach (var del in subscribers[type])
|
||||
{
|
||||
del.DynamicInvoke(packet);
|
||||
|
|
|
|||
|
|
@ -36,3 +36,14 @@ readonly struct TickPacket
|
|||
this.tick = tick;
|
||||
}
|
||||
}
|
||||
|
||||
readonly struct SelfMovedPacket
|
||||
{
|
||||
public readonly string type = "selfMoved";
|
||||
public readonly Vector2 target;
|
||||
|
||||
public SelfMovedPacket(Vector2 target)
|
||||
{
|
||||
this.target = target;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,10 +7,10 @@ class Connect
|
|||
public static void ConnectServer(ConnectPacket packet)
|
||||
{
|
||||
var ctx = Context.Get();
|
||||
var player = ctx.GameState.PlayerPositions.Find(p => p.name == packet.playerName);
|
||||
var player = ctx.GameState.Players.Find(p => p.name == packet.playerName);
|
||||
if (player == null)
|
||||
{
|
||||
ctx.GameState.PlayerPositions.Add(
|
||||
ctx.GameState.Players.Add(
|
||||
new Player
|
||||
{
|
||||
name = packet.playerName,
|
||||
|
|
|
|||
|
|
@ -1,24 +1,41 @@
|
|||
using System.Numerics;
|
||||
|
||||
[Interactor]
|
||||
class Move
|
||||
{
|
||||
[Interaction(InteractorKind.Hybrid, "move")]
|
||||
public static void MoveHybrid(MovePacket packet)
|
||||
[Interaction(InteractorKind.Client, "move")]
|
||||
public static void MoveClient(MovePacket packet)
|
||||
{
|
||||
var ctx = Context.Get();
|
||||
var player = ctx.GameState.PlayerPositions.Find(p => p.name == packet.playerName);
|
||||
var player = ctx.GameState.Players.Find(p => p.name == packet.playerName);
|
||||
if (player != null)
|
||||
{
|
||||
player.movement = packet.movement * 4;
|
||||
}
|
||||
}
|
||||
|
||||
[Interaction(InteractorKind.Hybrid, "tick")]
|
||||
public static void TickHybrid(TickPacket packet)
|
||||
[Interaction(InteractorKind.Client, "tick")]
|
||||
public static void TickClient(TickPacket packet)
|
||||
{
|
||||
var ctx = Context.Get();
|
||||
ctx.GameState.PlayerPositions.ForEach(player =>
|
||||
foreach (var player in ctx.GameState.Players)
|
||||
{
|
||||
if (player.movement == Vector2.Zero)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
player.position += player.movement;
|
||||
});
|
||||
if (player.guid == ctx.FrontendGameState.PlayerGuid)
|
||||
{
|
||||
ctx.Backend.ProcessPacket(new SelfMovedPacket(player.position));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Interaction(InteractorKind.Client, "selfMoved")]
|
||||
public static void SelfMovedClient(SelfMovedPacket packet)
|
||||
{
|
||||
var ctx = Context.Get();
|
||||
ctx.FrontendGameState.Camera.CenterOn(packet.target);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,21 @@
|
|||
using System.Numerics;
|
||||
|
||||
class Camera
|
||||
{
|
||||
public Vector2 position;
|
||||
|
||||
public Camera()
|
||||
{
|
||||
position = Vector2.Zero;
|
||||
}
|
||||
|
||||
public void CenterOn(Vector2 target)
|
||||
{
|
||||
Console.WriteLine("Centering camera on " + target);
|
||||
var ctx = Context.Get();
|
||||
var scale = ctx.GameState.Settings.GameScale;
|
||||
var windowWidth = ctx.FrontendGameState.WindowWidth;
|
||||
var windowHeight = ctx.FrontendGameState.WindowHeight;
|
||||
position = target - (new Vector2(windowWidth / 2, windowHeight / 2)) / scale;
|
||||
}
|
||||
}
|
||||
|
|
@ -28,6 +28,13 @@ class Tile
|
|||
{
|
||||
var renderer = Context.Get().Renderer;
|
||||
var scale = Context.Get().GameState.Settings.GameScale;
|
||||
renderer.DrawTexture(this.Texture, x * scale, y * scale, 16 * scale, 16 * scale);
|
||||
var camera = Context.Get().FrontendGameState.Camera;
|
||||
renderer.DrawTexture(
|
||||
this.Texture,
|
||||
(x - (int)camera.position.X) * scale,
|
||||
(y - (int)camera.position.Y) * scale,
|
||||
16 * scale,
|
||||
16 * scale
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ enum InteractorKind
|
|||
{
|
||||
Client,
|
||||
Server,
|
||||
Hybrid
|
||||
}
|
||||
|
||||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
|
||||
|
|
|
|||
|
|
@ -8,12 +8,16 @@ class Frontend : IFrontend
|
|||
|
||||
public void Init()
|
||||
{
|
||||
var ctx = Context.Get();
|
||||
this.playerName = Context.Get().IsHost ? "Host" : "Client";
|
||||
var guid = Guid.NewGuid();
|
||||
Context.Get().FrontendGameState.PlayerGuid = guid;
|
||||
ctx.FrontendGameState.PlayerGuid = guid;
|
||||
var connectPacket = new ConnectPacket(playerName, guid);
|
||||
Context.Get().Backend.ProcessPacket(connectPacket);
|
||||
Context.Get().TileRegistry.RegisterTile();
|
||||
ctx.Backend.ProcessPacket(connectPacket);
|
||||
ctx.TileRegistry.RegisterTile();
|
||||
var windowSize = ctx.Window.GetSize();
|
||||
ctx.FrontendGameState.WindowWidth = windowSize.width;
|
||||
ctx.FrontendGameState.WindowHeight = windowSize.height;
|
||||
}
|
||||
|
||||
public void Process()
|
||||
|
|
@ -32,6 +36,10 @@ class Frontend : IFrontend
|
|||
ctx.FrontendGameState.WindowWidth = e.window.data1;
|
||||
ctx.FrontendGameState.WindowHeight = e.window.data2;
|
||||
Console.WriteLine($"Window resized to {e.window.data1}x{e.window.data2}");
|
||||
var player = ctx.GameState.Players.Find(
|
||||
p => p.guid == ctx.FrontendGameState.PlayerGuid
|
||||
);
|
||||
ctx.FrontendGameState.Camera.CenterOn(player.position);
|
||||
}
|
||||
}
|
||||
if (e.type == SDL_EventType.SDL_KEYDOWN && e.key.repeat == 0)
|
||||
|
|
@ -115,14 +123,21 @@ class Frontend : IFrontend
|
|||
}
|
||||
|
||||
ctx.Renderer.Clear();
|
||||
var scale = ctx.GameState.Settings.GameScale;
|
||||
var camera = Context.Get().FrontendGameState.Camera;
|
||||
new WorldRenderer().Render();
|
||||
ctx.GameState.PlayerPositions.ForEach(player =>
|
||||
ctx.GameState.Players.ForEach(player =>
|
||||
{
|
||||
if (player.name == playerName)
|
||||
ctx.Renderer.SetColor(0, 0, 255, 255);
|
||||
else
|
||||
ctx.Renderer.SetColor(255, 0, 0, 255);
|
||||
ctx.Renderer.DrawRect(player.position.X, player.position.Y, 10, 10);
|
||||
ctx.Renderer.DrawRect(
|
||||
(player.position.X - (int)camera.position.X) * scale,
|
||||
(player.position.Y - (int)camera.position.Y) * scale,
|
||||
10,
|
||||
10
|
||||
);
|
||||
});
|
||||
ctx.Renderer.Present();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ class FrontendGameState
|
|||
public int WindowWidth;
|
||||
public int WindowHeight;
|
||||
public Guid PlayerGuid;
|
||||
public Camera Camera = new Camera();
|
||||
}
|
||||
|
||||
class Settings
|
||||
|
|
@ -25,7 +26,7 @@ class Settings
|
|||
|
||||
class GameState
|
||||
{
|
||||
public List<Player> PlayerPositions { get; set; } = new List<Player>();
|
||||
public List<Player> Players { get; set; } = new List<Player>();
|
||||
public World World { get; set; }
|
||||
public Settings Settings { get; set; } = new Settings();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue