Skip to content

Commit

Permalink
Merge pull request #112 from daud-io/masonry
Browse files Browse the repository at this point in the history
Masonry
  • Loading branch information
andylippitt authored Feb 12, 2019
2 parents 4dcaf78 + 0a676fc commit a3e7adb
Show file tree
Hide file tree
Showing 13 changed files with 247 additions and 149 deletions.
4 changes: 2 additions & 2 deletions Game.API.Client/WorldMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public async Task<string> DeleteWorldAsync(string worldKey)
});
}

public async Task<bool> SetMapTiles(string worldKey, IEnumerable<MapTileModel> tiles, CancellationToken cancellationToken = default(CancellationToken))
public async Task<bool> SetMap(string worldKey, MapModel mapModel, CancellationToken cancellationToken = default(CancellationToken))
{
return await APIClient.APICallAsync<bool>(
HttpMethod.Post,
Expand All @@ -54,7 +54,7 @@ public async Task<string> DeleteWorldAsync(string worldKey)
{
worldKey
},
bodyContent: tiles,
bodyContent: mapModel,
cancellationToken: cancellationToken
);
}
Expand Down
15 changes: 15 additions & 0 deletions Game.API.Common/Models/MapModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Game.API.Common.Models
{
using System.Collections.Generic;
using System.Numerics;

public class MapModel
{
public Vector2 Position { get; set; }
public int TileSize { get; set; }
public string Type { get; set; }
public int Rows { get; set; }
public int Columns { get; set; }
public List<MapTileModel> Tiles { get; set; }
}
}
6 changes: 2 additions & 4 deletions Game.API.Common/Models/MapTileModel.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
namespace Game.API.Common.Models
{
using System.Numerics;

public class MapTileModel
{
public Vector2 Position { get; set; }
public int Row { get; set; }
public int Column { get; set; }
public int TileGridID { get; set; }
public int Size { get; set; }
public string Type { get; set; }
public float Drag { get; set; } = 0;
}
Expand Down
4 changes: 2 additions & 2 deletions Game.Engine/Controllers/WorldController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ public WorldController(ISecurityContext securityContext,
}

[HttpPost, Route("map")]
public bool SetMap([FromBody] IEnumerable<MapTileModel> tiles, string worldKey)
public bool SetMap([FromBody] MapModel mapModel, string worldKey)
{
var world = Worlds.Find(worldKey);
if (world != null)
{
world.MapActor.SetTiles(tiles);
world.MapActor.SetMap(mapModel);
return true;
}
else
Expand Down
86 changes: 63 additions & 23 deletions Game.Engine/Core/Maps/MapActor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,37 @@
using Game.API.Common;
using Game.API.Common.Models;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;

public class MapActor : IActor
{
private World World = null;
private WorldMap Map { get; set; } = null;

private IEnumerable<MapTileModel> NewTileSet = null;
private MapModel NewMapModel = null;

public void Think()
{
if (Map != null)
{
foreach (var ship in AllShips().ToList())
{
var tile = Map.TileFromPosition(ship.Position);
if (tile != null)
tile.InteractWithShip(ship);
}
}
}

private IEnumerable<Ship> AllShips()
{
return Player.GetWorldPlayers(World)
.Where(p => p.IsAlive)
.Where(p => p.Fleet != null)
.Where(p => p.Fleet.Ships.Any())
.SelectMany(p => p.Fleet.Ships);
}

void IActor.CreateDestroy()
{
Expand All @@ -25,9 +49,9 @@ void IActor.CreateDestroy()
Map = null;
}

if (NewTileSet != null && Map != null)
if (NewMapModel != null && Map != null)
{
LoadTiles(NewTileSet);
LoadTiles(NewMapModel);
}
}

Expand All @@ -46,35 +70,51 @@ void IActor.Init(World world)
World.Actors.Add(this);
}

public void SetTiles(IEnumerable<MapTileModel> tiles)
public void SetMap(MapModel map)
{
this.NewTileSet = tiles;
this.NewMapModel = map;
}

private void LoadTiles(IEnumerable<MapTileModel> tiles)
private void LoadTiles(MapModel mapModel)
{
if (Map != null)
{
Map.DestroyTiles();
foreach (var tile in tiles)
Map.AddTile(new Tile
Map.Size = mapModel.TileSize;
Map.Columns = mapModel.Columns;
Map.Rows = mapModel.Rows;

Map.Offset = new Vector2(-mapModel.TileSize * mapModel.Columns / 2f, -mapModel.TileSize * mapModel.Rows / 2f);
var halfTile = new Vector2(mapModel.TileSize / 2, mapModel.TileSize / 2);
if (mapModel.Tiles != null)
{
foreach (var tile in mapModel.Tiles)
{
Sprite = (Sprites)(ushort)(1000 + tile.TileGridID),
Position = tile.Position,
Size = tile.Size,
IsDeadly = (tile.Type == "deadly"),
IsObstacle = (tile.Type == "obstacle"),
IsBouncy = (tile.Type == "bouncy"),
IsTurret = (tile.Type == "turret"),
Drag = tile.Drag
});

NewTileSet = null;
}
}
if (tile.TileGridID != -1)
{
TileBase tileObject;
if (tile.Type == "turret")
tileObject = new TileTurret();
else
tileObject = new TileBase();

public void Think()
{
tileObject.Sprite = (Sprites)(ushort)(1000 + tile.TileGridID);
tileObject.Position =
new Vector2(tile.Column * mapModel.TileSize, tile.Row * mapModel.TileSize) // top left
+ halfTile // center
+ Map.Offset; // offset map
tileObject.Size = mapModel.TileSize / 2;
tileObject.Drag = tile.Drag;

Map.AddTile(tileObject);
}
else
Map.AddTile(null);
}
}

NewMapModel = null;
}
}
}
}
20 changes: 20 additions & 0 deletions Game.Engine/Core/Maps/TileBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Game.Engine.Core.Maps
{
public class TileBase : ActorBody
{
public WorldMap WorldMap { get; internal set; }
public float Drag { get; set; }

public TileBase()
{
MaximumCleanTime = 100000;
IsStatic = true;
}

public virtual void InteractWithShip(Ship ship)
{
if (Drag != 0 && ship.Momentum.Length() < 5)
ship.Momentum *= Drag;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,14 @@

namespace Game.Engine.Core.Maps
{
public class Tile : ActorBody, ICollide
public class TileMisc : TileBase, ICollide
{
public bool IsDeadly { get; set; } = false;
public bool IsObstacle { get; set; } = false;
public bool IsBouncy { get; set; } = false;
public float Drag { get; set; } = 0;
public bool IsTurret { get; set; } = false;
private long ShotCooldown = 0;

private List<ShipWeaponBullet> NewBullets = new List<ShipWeaponBullet>();

public Tile()
public TileMisc()
{
MaximumCleanTime = 100000;
IsStatic = true;
Expand Down Expand Up @@ -60,38 +56,9 @@ public void CollisionExecute(Body projectedBody)
}


public override void Think()
{
//base.Think();

if (IsTurret && ShotCooldown < World.Time)
{
var target = World.BodiesNear(this.Position, 2500)
.OfType<Ship>()
.Where(s => s.Sprite != API.Common.Sprites.fish)
.FirstOrDefault();

if (target != null)
{
var bullet = new ShipWeaponBullet();
var toTarget = target.Position - Position;
bullet.FireFrom(this, MathF.Atan2(toTarget.Y, toTarget.X));

NewBullets.Add(bullet);

ShotCooldown = World.Time + 300;
}
}
}

public override void CreateDestroy()
{
base.CreateDestroy();

foreach (var bullet in NewBullets)
bullet.Init(World);

NewBullets.Clear();
}

public void Collide(Body bthis, Body ball)
Expand Down
64 changes: 64 additions & 0 deletions Game.Engine/Core/Maps/TileTurret.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
namespace Game.Engine.Core.Maps
{
using Game.Engine.Core.Weapons;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;

public class TileTurret : TileBase, ICollide
{
private long ShotCooldown = 0;
private List<ShipWeaponBullet> NewBullets = new List<ShipWeaponBullet>();

public bool IsCollision(Body projectedBody)
{
if (projectedBody is ShipWeaponBullet bullet)
if (bullet.Group != WorldMap.WeaponGroup)
return Vector2.Distance(projectedBody.Position, Position) < projectedBody.Size + Size;

return false;
}

public void CollisionExecute(Body projectedBody)
{
if (projectedBody is ShipWeaponBullet bullet)
{
this.Sprite += 1;
}
}

public override void Think()
{
if (ShotCooldown < World.Time)
{
var target = World.BodiesNear(this.Position, 2500)
.OfType<Ship>()
.Where(s => s.Abandoned == false)
.Where(s => s.Sprite != API.Common.Sprites.fish)
.FirstOrDefault();

if (target != null)
{
var bullet = new ShipWeaponBullet();
var toTarget = target.Position - Position;
bullet.FireFrom(this, MathF.Atan2(toTarget.Y, toTarget.X));

NewBullets.Add(bullet);

ShotCooldown = World.Time + 300;
}
}
}

public override void CreateDestroy()
{
base.CreateDestroy();

foreach (var bullet in NewBullets)
bullet.Init(World);

NewBullets.Clear();
}
}
}
Loading

0 comments on commit a3e7adb

Please sign in to comment.