Skip to content

Commit

Permalink
Basic Collision
Browse files Browse the repository at this point in the history
  • Loading branch information
XYZLassi committed Oct 24, 2017
1 parent b2f7a5a commit 90379f1
Show file tree
Hide file tree
Showing 13 changed files with 240 additions and 33 deletions.
6 changes: 6 additions & 0 deletions BigWorld/BigWorld.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,16 @@
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Compile Include="Entity.cs" />
<Compile Include="GameState.cs" />
<Compile Include="Map\Layer.cs" />
<Compile Include="Map\Room.cs" />
<Compile Include="Map\World.cs" />
<Compile Include="Player.cs" />
<Compile Include="Services\BaseService.cs" />
<Compile Include="Services\BlockCollisionService.cs" />
<Compile Include="Services\RoomCollisionService.cs" />
<Compile Include="Simulation.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\engenious.0.1.10\build\engenious.targets" />
Expand Down
20 changes: 20 additions & 0 deletions BigWorld/Entity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using BigWorld.Map;
using engenious;

namespace BigWorld
{
public abstract class Entity
{
public Vector2 CmdMoveDirection { get; set; }

public Point CurrentRoom { get; set; }
public Vector2 RoomPosition { get; set; }

public virtual void Update(GameTime gameTime)
{
RoomPosition = RoomPosition + CmdMoveDirection * (float)gameTime.ElapsedGameTime.TotalSeconds * 4;


}
}
}
27 changes: 25 additions & 2 deletions BigWorld/Map/Layer.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using engenious;

namespace BigWorld.Map
{
public class Layer<T>
where T : struct
public class Layer<T> where T : struct
{
public readonly T?[] Values = new T?[Room.SizeX* Room.SizeY];

Expand Down Expand Up @@ -42,5 +44,26 @@ public int GetFlatIndex(int x, int y)

return y * Room.SizeY + x;
}

public Point GetPointByIndex(int index)
{
var x = index % Room.SizeY;
var y = index / Room.SizeY;

return new Point(x,y);
}

public IEnumerable<KeyValuePair<Point,T>> GetPositivValues()
{
for (int i = 0; i < Values.Length; i++)
{
var value = Values[i];
if (value.HasValue)
{
yield return new KeyValuePair<Point, T>(GetPointByIndex(i),value.Value);
}
}
}

}
}
6 changes: 6 additions & 0 deletions BigWorld/Player.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace BigWorld
{
public class Player :Entity
{
}
}
10 changes: 10 additions & 0 deletions BigWorld/Services/BaseService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using BigWorld.Map;
using engenious;

namespace BigWorld.Services
{
public abstract class BaseService
{
public abstract void Update(Entity entity,World world, GameTime gameTime);
}
}
33 changes: 33 additions & 0 deletions BigWorld/Services/BlockCollisionService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Runtime.ExceptionServices;
using BigWorld.Map;
using engenious;

namespace BigWorld.Services
{
public class BlockCollisionService : BaseService
{
public override void Update(Entity entity, World world, GameTime gameTime)
{
Room room;
if (!world.TryGetRoom(entity.CurrentRoom,out room))
return;

foreach (var value in room.BlockLayer.GetPositivValues())
{
if (value.Value)
{
var distance = entity.RoomPosition - value.Key.ToVector2();

if ((distance.X > -1 && distance.X < 1) && (distance.Y > -1 && distance.Y < 1))
{
float x = distance.X < 0 ? distance.X : 1 - distance.X;
float y = distance.Y < 0 ? 1 + distance.Y : 1 - distance.Y;

var corr = new Vector2(x, y);
entity.RoomPosition += corr;
}
}
}
}
}
}
29 changes: 29 additions & 0 deletions BigWorld/Services/RoomCollisionService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using BigWorld.Map;
using engenious;

namespace BigWorld.Services
{
public class RoomCollisionService : BaseService
{
public override void Update(Entity entity, World world, GameTime gameTime)
{
if (entity.RoomPosition.X < 0)
{
entity.RoomPosition = new Vector2(0,entity.RoomPosition.Y);
}
else if (entity.RoomPosition.X > Room.SizeX -1)
{
entity.RoomPosition = new Vector2(Room.SizeX -1,entity.RoomPosition.Y);
}

if (entity.RoomPosition.Y < 0)
{
entity.RoomPosition = new Vector2(entity.RoomPosition.X,0);
}
else if (entity.RoomPosition.Y > Room.SizeY -1)
{
entity.RoomPosition = new Vector2(entity.RoomPosition.X,Room.SizeY -1);
}
}
}
}
61 changes: 61 additions & 0 deletions BigWorld/Simulation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System.Collections.Generic;
using BigWorld.Map;
using BigWorld.Services;
using engenious;

namespace BigWorld
{
public class Simulation
{
public World CurrentWorld { get; private set; }

public bool IsRunning { get; private set; }

private readonly List<Entity> entities = new List<Entity>();
public IEnumerable<Entity> Entities => entities;

private readonly List<BaseService> services = new List<BaseService>()
{
new RoomCollisionService(),
new BlockCollisionService(),
};

public void Start(World world)
{
CurrentWorld = world ;
entities.Clear();


IsRunning = true;
}

public Player AddPlayer()
{
Player player = new Player();

entities.Add(player);

return player;
}

public void Stop()
{
IsRunning = false;
}

public void Update(GameTime gameTime)
{
if(!IsRunning)
return;

foreach (var entity in entities)
{
entity.Update(gameTime);
foreach (var service in services)
{
service.Update(entity,CurrentWorld,gameTime);
}
}
}
}
}
27 changes: 18 additions & 9 deletions BigWorldGame/Components/EntityRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using BigWorld;
using BigWorld.Map;
using BigWorldGame.Graphics;
Expand Down Expand Up @@ -40,19 +41,27 @@ public override void Update(GameTime gameTime)
{
List<CharacterVertex> vertices = new List<CharacterVertex>(Room.SizeX * Room.SizeY * 4);

float x = Game.SimulationComponent.PlayerRoomPosition.X;
float y = Game.SimulationComponent.PlayerRoomPosition.Y;

uint index = 0;
var entities = Game.CurrentGameState == GameState.Running
? Game.SimulationComponent.Simulation.Entities
: null;

if (entities != null)
{
foreach (var entity in entities)
{
float x = entity.RoomPosition.X;
float y = entity.RoomPosition.Y;

vertices.Add(new CharacterVertex(x + 0, y + 0, 0, 0, 0, index));
vertices.Add(new CharacterVertex(x + 1, y + 0, 0, 1, 0, index));
uint index = 0;

vertices.Add(new CharacterVertex(x + 0, y + 1, 0, 0, 1, index));
vertices.Add(new CharacterVertex(x + 1, y + 1, 0, 1, 1, index));
vertices.Add(new CharacterVertex(x + 0, y + 0, 0, 0, 0, index));
vertices.Add(new CharacterVertex(x + 1, y + 0, 0, 1, 0, index));

vertices.Add(new CharacterVertex(x + 0, y + 1, 0, 0, 1, index));
vertices.Add(new CharacterVertex(x + 1, y + 1, 0, 1, 1, index));
}
}


if (vertexBuffer == null)
vertexBuffer = new VertexBuffer(Game.GraphicsDevice, CharacterVertex.VertexDeclaration, vertices.Count);
else if (vertexBuffer.VertexCount != vertices.Count)
Expand Down
8 changes: 4 additions & 4 deletions BigWorldGame/Components/Gui/BuildGuiRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public override void Update(GameTime gameTime)

if (mouseMapPoint.HasValue && mouseState.LeftButton == ButtonState.Pressed)
{
var room = Game.SimulationComponent.CurrentWorld.LoadOrCreateRoom(Game.SimulationComponent.CurrentRoomCoordinate);
var room = Game.SimulationComponent.BuildWorld.LoadOrCreateRoom(Game.SimulationComponent.CurrentRoomCoordinate);

if (!isBlockMode)
{
Expand All @@ -89,7 +89,7 @@ public override void Update(GameTime gameTime)
}
else if (mouseMapPoint.HasValue && mouseState.RightButton == ButtonState.Pressed)
{
var room = Game.SimulationComponent.CurrentWorld.LoadOrCreateRoom(Game.SimulationComponent.CurrentRoomCoordinate);
var room = Game.SimulationComponent.BuildWorld.LoadOrCreateRoom(Game.SimulationComponent.CurrentRoomCoordinate);

if (!isBlockMode)
{
Expand Down Expand Up @@ -125,7 +125,7 @@ public override void Update(GameTime gameTime)

if (groundFill.IsChanged((keyState.IsKeyDown(Keys.F)),i => i))
{
var room = Game.SimulationComponent.CurrentWorld.LoadOrCreateRoom(Game.SimulationComponent.CurrentRoomCoordinate);
var room = Game.SimulationComponent.BuildWorld.LoadOrCreateRoom(Game.SimulationComponent.CurrentRoomCoordinate);

for (int x = 0; x < Room.SizeX; x++)
{
Expand Down Expand Up @@ -161,7 +161,7 @@ public override void Draw(GameTime gameTime)
batch.Draw(pixelTexture,new Rectangle(mouseMapPoint.Value.X*RenderSettings.TileSize+height,mouseMapPoint.Value.Y*RenderSettings.TileSize+height,RenderSettings.TileSize,RenderSettings.TileSize),mouseColor );
}

var room = Game.SimulationComponent.CurrentWorld.LoadOrCreateRoom(Game.SimulationComponent.CurrentRoomCoordinate);
var room = Game.SimulationComponent.BuildWorld.LoadOrCreateRoom(Game.SimulationComponent.CurrentRoomCoordinate);

for (int x = 0; x < Room.SizeX; x++)
{
Expand Down
2 changes: 1 addition & 1 deletion BigWorldGame/Components/MapRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public override void Update(GameTime gameTime)
var x = i % 3 - 1;
var y = i / 3 - 1;

var room = Game.SimulationComponent.CurrentWorld.LoadOrCreateRoom(new Point(x, y) + Game.SimulationComponent.CurrentRoomCoordinate);
var room = Game.SimulationComponent.BuildWorld.LoadOrCreateRoom(new Point(x, y) + Game.SimulationComponent.CurrentRoomCoordinate);
renderers[i].ReloadChunk(room);
}
}
Expand Down
Loading

0 comments on commit 90379f1

Please sign in to comment.