added cursor
This commit is contained in:
parent
25837ba947
commit
03610b157d
|
|
@ -1,13 +1,12 @@
|
||||||
class Mine2d : Game
|
class Mine2d : Game
|
||||||
{
|
{
|
||||||
private Context ctx;
|
private readonly Context ctx;
|
||||||
|
|
||||||
public Mine2d(bool isHost)
|
public Mine2d(bool isHost)
|
||||||
{
|
{
|
||||||
var window = new Window("MultiPlayerGame" + (isHost ? " - host" : ""), 1200, 800);
|
var window = new Window("MultiPlayerGame" + (isHost ? " - host" : ""), 1200, 800);
|
||||||
if (isHost)
|
this.ctx = isHost
|
||||||
{
|
? new Context(
|
||||||
this.ctx = new Context(
|
|
||||||
isHost,
|
isHost,
|
||||||
new Backend(),
|
new Backend(),
|
||||||
new Frontend(),
|
new Frontend(),
|
||||||
|
|
@ -15,11 +14,8 @@ class Mine2d : Game
|
||||||
new FrontendGameState(),
|
new FrontendGameState(),
|
||||||
new Renderer(window),
|
new Renderer(window),
|
||||||
window
|
window
|
||||||
);
|
)
|
||||||
}
|
: new Context(
|
||||||
else
|
|
||||||
{
|
|
||||||
this.ctx = new Context(
|
|
||||||
isHost,
|
isHost,
|
||||||
new RemoteBackend(),
|
new RemoteBackend(),
|
||||||
new Frontend(),
|
new Frontend(),
|
||||||
|
|
@ -28,19 +24,18 @@ class Mine2d : Game
|
||||||
new Renderer(window),
|
new Renderer(window),
|
||||||
window
|
window
|
||||||
);
|
);
|
||||||
}
|
|
||||||
Bootstrapper.Bootstrap();
|
Bootstrapper.Bootstrap();
|
||||||
ctx.Backend.Init();
|
this.ctx.Backend.Init();
|
||||||
ctx.Frontend.Init();
|
this.ctx.Frontend.Init();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void draw()
|
protected override void draw()
|
||||||
{
|
{
|
||||||
ctx.Frontend.Process();
|
this.ctx.Frontend.Process();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void update(double dt)
|
protected override void update(double dt)
|
||||||
{
|
{
|
||||||
ctx.Backend.Process(dt);
|
this.ctx.Backend.Process(dt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -65,7 +65,11 @@ class World
|
||||||
|
|
||||||
public int GetTileAt(int x, int y)
|
public int GetTileAt(int x, int y)
|
||||||
{
|
{
|
||||||
var chunk = this.GetChunkAt(x, y);
|
return this.GetChunkAt(x, y).GetTileAt(x, y);
|
||||||
return 0;
|
}
|
||||||
|
|
||||||
|
public bool HasTileAt(int x, int y)
|
||||||
|
{
|
||||||
|
return this.HasChunkAt(x, y) && this.GetChunkAt(x, y).hasTileAt(new Vector2(x, y));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,8 +8,8 @@ class Tile
|
||||||
this.Name = name;
|
this.Name = name;
|
||||||
|
|
||||||
var rl = Context.Get().ResourceLoader;
|
var rl = Context.Get().ResourceLoader;
|
||||||
var res = rl.LoadToIntPtr("assets." + textureName + ".png");
|
var (ptr, size) = rl.LoadToIntPtr("assets." + textureName + ".png");
|
||||||
var sdlBuffer = SDL_RWFromMem(res.ptr, res.size);
|
var sdlBuffer = SDL_RWFromMem(ptr, size);
|
||||||
var surface = IMG_Load_RW(sdlBuffer, 1);
|
var surface = IMG_Load_RW(sdlBuffer, 1);
|
||||||
var texture = Context.Get().Renderer.CreateTextureFromSurface(surface);
|
var texture = Context.Get().Renderer.CreateTextureFromSurface(surface);
|
||||||
this.Texture = texture;
|
this.Texture = texture;
|
||||||
|
|
@ -34,15 +34,4 @@ class Tile
|
||||||
Constants.TileSize * scale
|
Constants.TileSize * scale
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SDL_Rect GetCollisionRect(int x, int y)
|
|
||||||
{
|
|
||||||
return new SDL_Rect()
|
|
||||||
{
|
|
||||||
x = x,
|
|
||||||
y = y,
|
|
||||||
w = Constants.TileSize,
|
|
||||||
h = Constants.TileSize
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,11 +9,11 @@ class TileRegistry
|
||||||
|
|
||||||
public void RegisterTile()
|
public void RegisterTile()
|
||||||
{
|
{
|
||||||
Tiles.Add(1, new Tile("stone", "stone"));
|
this.Tiles.Add(1, new Tile("stone", "stone"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Tile GetTile(int id)
|
public Tile GetTile(int id)
|
||||||
{
|
{
|
||||||
return Tiles[id];
|
return this.Tiles[id];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,19 @@ class Renderer
|
||||||
SDL_RenderFillRect(renderer, ref rect);
|
SDL_RenderFillRect(renderer, ref rect);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void DrawOutline(int x, int y, int w, int h)
|
||||||
|
{
|
||||||
|
var rect = new SDL_Rect
|
||||||
|
{
|
||||||
|
x = x,
|
||||||
|
y = y,
|
||||||
|
w = w,
|
||||||
|
h = h
|
||||||
|
};
|
||||||
|
|
||||||
|
_ = SDL_RenderDrawRect(renderer, ref rect);
|
||||||
|
}
|
||||||
|
|
||||||
public void DrawLines(double[][] points)
|
public void DrawLines(double[][] points)
|
||||||
{
|
{
|
||||||
SDL_Point[] sdlPoints = new SDL_Point[points.Length];
|
SDL_Point[] sdlPoints = new SDL_Point[points.Length];
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,12 @@
|
||||||
class Frontend : IFrontend
|
class Frontend : IFrontend
|
||||||
{
|
{
|
||||||
private string playerName = "Player";
|
|
||||||
private bool fullscreen = false;
|
|
||||||
|
|
||||||
public void Init()
|
public void Init()
|
||||||
{
|
{
|
||||||
var ctx = Context.Get();
|
var ctx = Context.Get();
|
||||||
this.playerName = Context.Get().IsHost ? "Host" : "Client";
|
ctx.FrontendGameState.PlayerName = ctx.IsHost ? "Host" : "Client";
|
||||||
var guid = Guid.NewGuid();
|
var guid = Guid.NewGuid();
|
||||||
ctx.FrontendGameState.PlayerGuid = guid;
|
ctx.FrontendGameState.PlayerGuid = guid;
|
||||||
var connectPacket = new ConnectPacket(playerName, guid);
|
var connectPacket = new ConnectPacket(ctx.FrontendGameState.PlayerName, guid);
|
||||||
ctx.Backend.ProcessPacket(connectPacket);
|
ctx.Backend.ProcessPacket(connectPacket);
|
||||||
ctx.TileRegistry.RegisterTile();
|
ctx.TileRegistry.RegisterTile();
|
||||||
var (width, height) = ctx.Window.GetSize();
|
var (width, height) = ctx.Window.GetSize();
|
||||||
|
|
@ -39,23 +36,29 @@ class Frontend : IFrontend
|
||||||
ctx.FrontendGameState.Camera.CenterOn(player.Position);
|
ctx.FrontendGameState.Camera.CenterOn(player.Position);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (e.type == SDL_EventType.SDL_MOUSEMOTION)
|
||||||
|
{
|
||||||
|
var mousePos = new Vector2(e.motion.x, e.motion.y);
|
||||||
|
Console.WriteLine($"Mouse moved to {mousePos}");
|
||||||
|
ctx.FrontendGameState.MousePosition = mousePos;
|
||||||
|
}
|
||||||
if (e.type == SDL_EventType.SDL_KEYDOWN && e.key.repeat == 0)
|
if (e.type == SDL_EventType.SDL_KEYDOWN && e.key.repeat == 0)
|
||||||
{
|
{
|
||||||
var movementInput = ctx.FrontendGameState.MovementInput;
|
var movementInput = ctx.FrontendGameState.MovementInput;
|
||||||
if (e.key.keysym.scancode == SDL_Scancode.SDL_SCANCODE_F11)
|
if (e.key.keysym.scancode == SDL_Scancode.SDL_SCANCODE_F11)
|
||||||
{
|
{
|
||||||
if (!fullscreen)
|
if (!ctx.FrontendGameState.Settings.Fullscreen)
|
||||||
{
|
{
|
||||||
SDL_SetWindowFullscreen(
|
_ = SDL_SetWindowFullscreen(
|
||||||
ctx.Window.GetRaw(),
|
ctx.Window.GetRaw(),
|
||||||
(uint)SDL_WindowFlags.SDL_WINDOW_FULLSCREEN_DESKTOP
|
(uint)SDL_WindowFlags.SDL_WINDOW_FULLSCREEN_DESKTOP
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
SDL_SetWindowFullscreen(ctx.Window.GetRaw(), 0);
|
_ = SDL_SetWindowFullscreen(ctx.Window.GetRaw(), 0);
|
||||||
}
|
}
|
||||||
fullscreen = !fullscreen;
|
ctx.FrontendGameState.Settings.Fullscreen = !ctx.FrontendGameState.Settings.Fullscreen;
|
||||||
}
|
}
|
||||||
if (e.key.keysym.scancode == SDL_Scancode.SDL_SCANCODE_A)
|
if (e.key.keysym.scancode == SDL_Scancode.SDL_SCANCODE_A)
|
||||||
{
|
{
|
||||||
|
|
@ -107,11 +110,17 @@ class Frontend : IFrontend
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
if (e.key.repeat == 1)
|
if (e.key.repeat == 1)
|
||||||
|
{
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
var movement = ctx.FrontendGameState.MovementInput;
|
var movement = ctx.FrontendGameState.MovementInput;
|
||||||
if (movement.Length() > 0)
|
if (movement.Length() > 0)
|
||||||
|
{
|
||||||
movement = Vector2.Normalize(movement);
|
movement = Vector2.Normalize(movement);
|
||||||
ctx.Backend.ProcessPacket(new MovePacket(playerName, movement));
|
}
|
||||||
|
|
||||||
|
ctx.Backend.ProcessPacket(new MovePacket(ctx.FrontendGameState.PlayerName, movement));
|
||||||
}
|
}
|
||||||
if (e.key.keysym.scancode == SDL_Scancode.SDL_SCANCODE_ESCAPE)
|
if (e.key.keysym.scancode == SDL_Scancode.SDL_SCANCODE_ESCAPE)
|
||||||
{
|
{
|
||||||
|
|
@ -125,10 +134,15 @@ class Frontend : IFrontend
|
||||||
new WorldRenderer().Render();
|
new WorldRenderer().Render();
|
||||||
ctx.GameState.Players.ForEach(player =>
|
ctx.GameState.Players.ForEach(player =>
|
||||||
{
|
{
|
||||||
if (player.Name == playerName)
|
if (player.Name == ctx.FrontendGameState.PlayerName)
|
||||||
|
{
|
||||||
ctx.Renderer.SetColor(0, 0, 255, 255);
|
ctx.Renderer.SetColor(0, 0, 255, 255);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
ctx.Renderer.SetColor(255, 0, 0, 255);
|
ctx.Renderer.SetColor(255, 0, 0, 255);
|
||||||
|
}
|
||||||
|
|
||||||
ctx.Renderer.DrawRect(
|
ctx.Renderer.DrawRect(
|
||||||
(player.Position.X - (int)camera.position.X) * scale,
|
(player.Position.X - (int)camera.position.X) * scale,
|
||||||
(player.Position.Y - (int)camera.position.Y) * scale - 32 * scale,
|
(player.Position.Y - (int)camera.position.Y) * scale - 32 * scale,
|
||||||
|
|
@ -136,6 +150,20 @@ class Frontend : IFrontend
|
||||||
32 * scale
|
32 * scale
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
var absoluteMousePos = ctx.FrontendGameState.MousePosition / ctx.FrontendGameState.Settings.GameScale + camera.position;
|
||||||
|
if (ctx.GameState.World.HasTileAt((int)absoluteMousePos.X, (int)absoluteMousePos.Y))
|
||||||
|
{
|
||||||
|
var a = Constants.TileSize;
|
||||||
|
var tilePos = new Vector2(absoluteMousePos.X - absoluteMousePos.X % a, absoluteMousePos.Y - absoluteMousePos.Y % a);
|
||||||
|
ctx.Renderer.SetColor(255, 255, 255, 255);
|
||||||
|
ctx.Renderer.DrawOutline(
|
||||||
|
(int)tilePos.X * scale - (int)camera.position.X * scale,
|
||||||
|
(int)tilePos.Y * scale - (int)camera.position.Y * scale,
|
||||||
|
16 * scale,
|
||||||
|
16 * scale
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
ctx.Renderer.Present();
|
ctx.Renderer.Present();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,9 @@ class FrontendGameState
|
||||||
public int WindowHeight;
|
public int WindowHeight;
|
||||||
public Guid PlayerGuid;
|
public Guid PlayerGuid;
|
||||||
public Camera Camera = new Camera();
|
public Camera Camera = new Camera();
|
||||||
|
public Vector2 MousePosition;
|
||||||
public Settings Settings { get; set; } = new Settings();
|
public Settings Settings { get; set; } = new Settings();
|
||||||
|
public string PlayerName { get; set; } = "Player";
|
||||||
}
|
}
|
||||||
|
|
||||||
class Settings
|
class Settings
|
||||||
|
|
@ -14,6 +16,7 @@ class Settings
|
||||||
public int GameScale = 4;
|
public int GameScale = 4;
|
||||||
public int UIScale = 4;
|
public int UIScale = 4;
|
||||||
public bool ShowCollision = true;
|
public bool ShowCollision = true;
|
||||||
|
public bool Fullscreen = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
class GameState
|
class GameState
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,11 @@ class Player
|
||||||
public Vector2 Position;
|
public Vector2 Position;
|
||||||
public Vector2 Movement;
|
public Vector2 Movement;
|
||||||
public Guid Guid;
|
public Guid Guid;
|
||||||
|
public Vector2 Mining;
|
||||||
|
public int MiningCooldown;
|
||||||
|
|
||||||
public Line GetBottomCollisionLine()
|
public Line GetBottomCollisionLine()
|
||||||
{
|
{
|
||||||
return new Line(Position, Position + new Vector2(16, 0));
|
return new Line(this.Position, this.Position + new Vector2(16, 0));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue