added gravity
This commit is contained in:
parent
7213239038
commit
2574981d43
|
|
@ -0,0 +1 @@
|
|||
global using System.Numerics;
|
||||
|
|
@ -29,7 +29,7 @@ class Backend : IBackend
|
|||
public void Init()
|
||||
{
|
||||
Task.Run(Run);
|
||||
this.publisher = new Publisher(InteractorKind.Server);
|
||||
this.publisher = new Publisher(InteractorKind.Hybrid);
|
||||
}
|
||||
|
||||
public void Run()
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ class Publisher
|
|||
continue;
|
||||
}
|
||||
var attr = (Interaction)attrs2[0];
|
||||
if (attr.Kind == InteractorKind.Server && this.kind == InteractorKind.Client)
|
||||
if (attr.Kind != this.kind && this.kind != InteractorKind.Hybrid)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,16 +7,16 @@ class Connect
|
|||
public static void ConnectServer(ConnectPacket packet)
|
||||
{
|
||||
var ctx = Context.Get();
|
||||
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)
|
||||
{
|
||||
ctx.GameState.Players.Add(
|
||||
new Player
|
||||
{
|
||||
name = packet.playerName,
|
||||
guid = packet.playerGuid,
|
||||
position = new Vector2(0, 0),
|
||||
movement = new Vector2(0, 0)
|
||||
Name = packet.playerName,
|
||||
Guid = packet.playerGuid,
|
||||
Position = new Vector2(20, 0),
|
||||
Movement = new Vector2(0, 0)
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,41 +1,29 @@
|
|||
using System.Numerics;
|
||||
|
||||
[Interactor]
|
||||
class Move
|
||||
{
|
||||
[Interaction(InteractorKind.Client, "move")]
|
||||
public static void MoveClient(MovePacket packet)
|
||||
[Interaction(InteractorKind.Hybrid, "move")]
|
||||
public static void MoveHybrid(MovePacket packet)
|
||||
{
|
||||
var ctx = Context.Get();
|
||||
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)
|
||||
{
|
||||
player.movement = packet.movement * 4;
|
||||
player.Movement = packet.movement * 4;
|
||||
}
|
||||
}
|
||||
|
||||
[Interaction(InteractorKind.Hybrid, "tick")]
|
||||
public static void TickHybrid(TickPacket packet)
|
||||
{
|
||||
var ctx = Context.Get();
|
||||
ctx.GameState.Players.ForEach(PlayerEntity.Move);
|
||||
ctx.GameState.Players.ForEach(PlayerEntity.Collide);
|
||||
}
|
||||
|
||||
[Interaction(InteractorKind.Client, "tick")]
|
||||
public static void TickClient(TickPacket packet)
|
||||
public static void SelfMovedClient(TickPacket packet)
|
||||
{
|
||||
var ctx = Context.Get();
|
||||
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);
|
||||
var camera = Context.Get().FrontendGameState.Camera;
|
||||
camera.CenterOn(PlayerEntity.GetSelf().Position);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,6 @@
|
|||
class Constants
|
||||
{
|
||||
public const int ChunkSize = 32;
|
||||
public const int TileSize = 16;
|
||||
public static Vector2 gravity = new Vector2(0, 0.1f);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,38 @@
|
|||
class PlayerEntity
|
||||
{
|
||||
public static bool isSelf(Player p)
|
||||
{
|
||||
return p.Guid == GetSelf().Guid;
|
||||
}
|
||||
|
||||
public static Player GetSelf()
|
||||
{
|
||||
var ctx = Context.Get();
|
||||
var player = ctx.GameState.Players.FirstOrDefault(
|
||||
p => p.Guid == ctx.FrontendGameState.PlayerGuid
|
||||
);
|
||||
return player;
|
||||
}
|
||||
|
||||
public static void Move(Player p)
|
||||
{
|
||||
var ctx = Context.Get();
|
||||
p.Movement = p.Movement + Constants.gravity;
|
||||
p.Position += p.Movement;
|
||||
}
|
||||
|
||||
public static void Collide(Player p)
|
||||
{
|
||||
var world = Context.Get().GameState.World;
|
||||
if (!world.HasChunkAt(p.Position))
|
||||
{
|
||||
return;
|
||||
}
|
||||
var chunk = world.GetChunkAt(p.Position);
|
||||
while (chunk.hasTileAt(p.Position))
|
||||
{
|
||||
p.Movement = p.Movement with { Y = 0 };
|
||||
p.Position += new Vector2(0, -0.1f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
class Chunk
|
||||
{
|
||||
public int[][] Tiles { get; set; } = new int[Constants.ChunkSize][];
|
||||
public int[,] Tiles { get; set; } = new int[Constants.ChunkSize, Constants.ChunkSize];
|
||||
public int X { get; set; }
|
||||
public int Y { get; set; }
|
||||
|
||||
|
|
@ -8,14 +8,49 @@ class Chunk
|
|||
{
|
||||
X = x;
|
||||
Y = y;
|
||||
for (int i = 0; i < Constants.ChunkSize; i++)
|
||||
{
|
||||
Tiles[i] = new int[Constants.ChunkSize];
|
||||
}
|
||||
}
|
||||
|
||||
public void SetTile(int x, int y, int tile)
|
||||
{
|
||||
Tiles[x][y] = tile;
|
||||
Tiles[x, y] = tile;
|
||||
}
|
||||
|
||||
public int GetTile(int x, int y)
|
||||
{
|
||||
return Tiles[x, y];
|
||||
}
|
||||
|
||||
public bool hasTileAt(Vector2 pos)
|
||||
{
|
||||
return hasTileAt((int)pos.X, (int)pos.Y);
|
||||
}
|
||||
|
||||
public bool hasTileAt(int x, int y)
|
||||
{
|
||||
var tileX = x / Constants.TileSize;
|
||||
var tileY = y / Constants.TileSize;
|
||||
return hasTile(tileX, tileY);
|
||||
}
|
||||
|
||||
public int GetTileAt(Vector2 pos)
|
||||
{
|
||||
return GetTileAt((int)pos.X, (int)pos.Y);
|
||||
}
|
||||
|
||||
public int GetTileAt(int x, int y)
|
||||
{
|
||||
var tileX = x / Constants.TileSize;
|
||||
var tileY = y / Constants.TileSize;
|
||||
return GetTile(tileX, tileY);
|
||||
}
|
||||
|
||||
public bool hasTile(int x, int y)
|
||||
{
|
||||
return x >= 0 && x < Tiles.Length && y >= 0 && y < Tiles.Length;
|
||||
}
|
||||
|
||||
public bool hasTile(Vector2 pos)
|
||||
{
|
||||
return hasTile((int)pos.X, (int)pos.Y);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,11 +7,40 @@ class World
|
|||
Chunks.Add(chunk.X + "," + chunk.Y, chunk);
|
||||
}
|
||||
|
||||
public Chunk GetChunk(Vector2 pos)
|
||||
{
|
||||
return GetChunk((int)pos.X, (int)pos.Y);
|
||||
}
|
||||
|
||||
public Chunk GetChunk(int x, int y)
|
||||
{
|
||||
return Chunks[x + "," + y];
|
||||
}
|
||||
|
||||
public Chunk GetChunkAt(Vector2 pos)
|
||||
{
|
||||
return GetChunkAt((int)pos.X, (int)pos.Y);
|
||||
}
|
||||
|
||||
public Chunk GetChunkAt(int x, int y)
|
||||
{
|
||||
var chunkX = x / Constants.ChunkSize;
|
||||
var chunkY = y / Constants.ChunkSize;
|
||||
return Chunks[chunkX + "," + chunkY];
|
||||
}
|
||||
|
||||
public bool HasChunkAt(Vector2 pos)
|
||||
{
|
||||
return HasChunkAt((int)pos.X, (int)pos.Y);
|
||||
}
|
||||
|
||||
public bool HasChunkAt(int x, int y)
|
||||
{
|
||||
var chunkX = x / Constants.ChunkSize;
|
||||
var chunkY = y / Constants.ChunkSize;
|
||||
return Chunks.ContainsKey(chunkX + "," + chunkY);
|
||||
}
|
||||
|
||||
public bool ChunkExists(int x, int y)
|
||||
{
|
||||
return Chunks.ContainsKey(x + "," + y);
|
||||
|
|
|
|||
|
|
@ -37,4 +37,9 @@ class Tile
|
|||
16 * scale
|
||||
);
|
||||
}
|
||||
|
||||
public bool IsSolid()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ enum InteractorKind
|
|||
{
|
||||
Client,
|
||||
Server,
|
||||
Hybrid
|
||||
}
|
||||
|
||||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
|
||||
|
|
|
|||
|
|
@ -37,9 +37,9 @@ class Frontend : IFrontend
|
|||
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
|
||||
p => p.Guid == ctx.FrontendGameState.PlayerGuid
|
||||
);
|
||||
ctx.FrontendGameState.Camera.CenterOn(player.position);
|
||||
ctx.FrontendGameState.Camera.CenterOn(player.Position);
|
||||
}
|
||||
}
|
||||
if (e.type == SDL_EventType.SDL_KEYDOWN && e.key.repeat == 0)
|
||||
|
|
@ -128,13 +128,13 @@ class Frontend : IFrontend
|
|||
new WorldRenderer().Render();
|
||||
ctx.GameState.Players.ForEach(player =>
|
||||
{
|
||||
if (player.name == playerName)
|
||||
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 - (int)camera.position.X) * scale,
|
||||
(player.position.Y - (int)camera.position.Y) * scale,
|
||||
(player.Position.X - (int)camera.position.X) * scale,
|
||||
(player.Position.Y - (int)camera.position.Y) * scale - 10,
|
||||
10,
|
||||
10
|
||||
);
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ class WorldRenderer : IRenderer
|
|||
{
|
||||
for (int x = 0; x < 16; x++)
|
||||
{
|
||||
var tileId = chunk.Tiles[x][y];
|
||||
var tileId = chunk.GetTile(x, y);
|
||||
var tile = tileRegistry.GetTile(tileId);
|
||||
tile.Render(x * 16, y * 16);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,3 @@
|
|||
using System.Numerics;
|
||||
|
||||
class Player
|
||||
{
|
||||
public string name;
|
||||
public Vector2 position;
|
||||
public Vector2 movement;
|
||||
public Guid guid;
|
||||
}
|
||||
|
||||
class FrontendGameState
|
||||
{
|
||||
public Vector2 MovementInput;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,9 @@
|
|||
using System.Numerics;
|
||||
|
||||
class Player
|
||||
{
|
||||
public string Name;
|
||||
public Vector2 Position;
|
||||
public Vector2 Movement;
|
||||
public Guid Guid;
|
||||
}
|
||||
Loading…
Reference in New Issue