-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from SneakyTactician/TileBasedDev
[Version 0.0.0.3] ### Features *End turn button is now rendered #### API *Support for creature movement ### Tweaks *Changed many data structures under the hood
- Loading branch information
Showing
44 changed files
with
1,154 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,13 @@ | ||
SneakyTactician <[email protected]> | ||
[Version 0.0.0.3] | ||
### Features | ||
*End turn button is now rendered | ||
#### API | ||
*Support for creature movement | ||
### Tweaks | ||
*Changed many data structures under the hood | ||
### Bugs | ||
* | ||
|
||
[Version 0.0.0.2] | ||
### Features | ||
|
@@ -7,22 +16,10 @@ | |
#### API | ||
*Living creatures are now supported | ||
|
||
### Tweaks | ||
* | ||
|
||
### Bugs | ||
* | ||
|
||
[Version 0.0.0.1] | ||
|
||
### Features | ||
*The world displays when told to generate a new world | ||
|
||
#### API | ||
*World generates properly | ||
|
||
### Tweaks | ||
* | ||
|
||
### Bugs | ||
* | ||
*World generates properly |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Credit for things I used that I didn't make: | ||
|
||
Libraries: | ||
https://github.com/agabani/DijkstraAlgorithm optimal pathfinding algorithm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System.Drawing; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace MagicalLifeAPI.DataTypes | ||
{ | ||
/// <summary> | ||
/// A 3 dimensional point class. | ||
/// </summary> | ||
public class Point3D : Object | ||
{ | ||
public int X { get; set; } | ||
public int Y { get; set; } | ||
public int Z { get; set; } | ||
|
||
public Point3D(int x, int y, int z) | ||
{ | ||
this.X = x; | ||
this.Y = y; | ||
this.Z = z; | ||
} | ||
|
||
public Point3D(string str) | ||
{ | ||
string[] delimiter = new string[] { ", " }; | ||
string[] numbers = str.Split(delimiter, 3, StringSplitOptions.RemoveEmptyEntries); | ||
|
||
int x; | ||
int y; | ||
int z; | ||
|
||
int.TryParse(numbers[0], out x); | ||
int.TryParse(numbers[1], out y); | ||
int.TryParse(numbers[2], out z); | ||
|
||
this.X = x; | ||
this.Y = y; | ||
this.Z = z; | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return this.X.ToString() + ", " + this.Y.ToString() + ", " + this.Z.ToString(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System.Security.Cryptography; | ||
using DijkstraAlgorithm.Pathing; | ||
using MagicalLifeAPI.World; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using MagicalLifeAPI.Entities.Util.Modifier_Remove_Conditions; | ||
|
||
namespace MagicalLifeAPI.Entities.Movement | ||
{ | ||
/// <summary> | ||
/// Used to move entities. | ||
/// </summary> | ||
public static class EntityWorldMovement | ||
{ | ||
/// <summary> | ||
/// Moves an entity from it's current position to as close to it's target destination as it can get. This will appear like teleporting. | ||
/// </summary> | ||
/// <param name="entity"></param> | ||
public static void MoveEntity(ref Living entity) | ||
{ | ||
Queue<PathSegment> path = entity.QueuedMovement; | ||
while (entity.MovementSpeed.GetValue() > 0 && path.Count > 0) | ||
{ | ||
PathSegment destination = path.Dequeue(); | ||
Tile sourceTile = WorldUtil.GetTileByID(World.World.mainWorld.Tiles, destination.Origin.Id); | ||
Tile destinationTile = WorldUtil.GetTileByID(World.World.mainWorld.Tiles, destination.Destination.Id); | ||
string modifierReason = "Moved onto a " + destinationTile.GetName() + " tile"; | ||
entity.MovementSpeed.AddModifier(new Tuple<long, IModifierRemoveCondition, string>(-1 * destinationTile.MovementCost, new TimeRemoveCondition(1), modifierReason)); | ||
World.World.mainWorld.Tiles[sourceTile.Location.X, sourceTile.Location.Y, sourceTile.Location.Z].Living.RemoveAt(EntityWorldMovement.GetIndexOfEntity(sourceTile.Living, entity)); | ||
World.World.mainWorld.Tiles[destinationTile.Location.X, destinationTile.Location.Y, destinationTile.Location.Z].Living.Add(entity); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Finds the index of the living creature in the list. Returns -1 if it doesn't exist anymore. | ||
/// </summary> | ||
/// <param name="living"></param> | ||
/// <param name="target"></param> | ||
/// <returns></returns> | ||
private static int GetIndexOfEntity(List<Living> living, Living target) | ||
{ | ||
int length = living.Count; | ||
for (int i = 0; i < length; i++) | ||
{ | ||
if (living[i].ID == target.ID) | ||
{ | ||
return i; | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
using DijkstraAlgorithm.Graphing; | ||
using DijkstraAlgorithm.Pathing; | ||
using MagicalLifeAPI.World; | ||
using System.Linq; | ||
|
||
namespace MagicalLifeAPI.Entities.Movement | ||
{ | ||
/// <summary> | ||
/// A class that handles the construction of the graph used to do optimal pathfinding. | ||
/// </summary> | ||
public static class StandardPathFinder | ||
{ | ||
/// <summary> | ||
/// Holds data that describes which tiles connect to which tiles. | ||
/// This graph contains data used to pathfind for the standard movement. | ||
/// </summary> | ||
private static GraphBuilder tileConnectionGraph = new GraphBuilder(); | ||
|
||
private static Graph builtGraph; | ||
|
||
/// <summary> | ||
/// Used to determine the fastest route between two points. | ||
/// </summary> | ||
private static PathFinder pathFinder; | ||
|
||
/// <summary> | ||
/// Returns the fastest route between the source and destination tiles. | ||
/// </summary> | ||
/// <param name="source"></param> | ||
/// <param name="destination"></param> | ||
/// <returns></returns> | ||
public static Path GetFastestPath(Tile source, Tile destination) | ||
{ | ||
Path path = pathFinder.FindShortestPath( | ||
StandardPathFinder.builtGraph.Nodes.Single(node => node.Id == source.Location.ToString()), | ||
StandardPathFinder.builtGraph.Nodes.Single(node => node.Id == destination.Location.ToString())); | ||
return path; | ||
} | ||
|
||
/// <summary> | ||
/// Populates the <see cref="tileConnectionGraph"/> with data. | ||
/// This should be called once after the world is generated. | ||
/// </summary> | ||
/// <param name="world"></param> | ||
public static void BuildPathGraph(World.World world) | ||
{ | ||
StandardPathFinder.AddNodes(world); | ||
StandardPathFinder.AddLinkes(world); | ||
StandardPathFinder.builtGraph = StandardPathFinder.tileConnectionGraph.Build(); | ||
StandardPathFinder.pathFinder = new PathFinder(StandardPathFinder.builtGraph); | ||
} | ||
|
||
/// <summary> | ||
/// Creates connections between tiles in the <see cref="tileConnectionGraph"/>. | ||
/// </summary> | ||
/// <param name="world"></param> | ||
private static void AddLinkes(World.World world) | ||
{ | ||
Tile[,,] tiles = world.Tiles; | ||
int xSize = tiles.GetLength(0); | ||
int ySize = tiles.GetLength(1); | ||
int zSize = tiles.GetLength(2); | ||
|
||
int x = 0; | ||
int y = 0; | ||
int z = 0; | ||
|
||
//Iterate over each row. | ||
for (int i = 0; i < xSize; i++) | ||
{ | ||
//Iterate over each column | ||
for (int ii = 0; ii < ySize; ii++) | ||
{ | ||
//Iterate over the depth of each tile in the z axis. | ||
for (int iii = 0; iii < zSize; iii++) | ||
{ | ||
//Each tile can be accessed by the xyz coordinates from this inner loop properly. | ||
StandardPathFinder.AddNeighborLink(1, 0, 0, tiles, tiles[x, y, z]); | ||
StandardPathFinder.AddNeighborLink(-1, 0, 0, tiles, tiles[x, y, z]); | ||
StandardPathFinder.AddNeighborLink(0, 1, 0, tiles, tiles[x, y, z]); | ||
StandardPathFinder.AddNeighborLink(0, -1, 0, tiles, tiles[x, y, z]); | ||
StandardPathFinder.AddNeighborLink(1, 1, 0, tiles, tiles[x, y, z]); | ||
StandardPathFinder.AddNeighborLink(1, -1, 0, tiles, tiles[x, y, z]); | ||
StandardPathFinder.AddNeighborLink(-1, 1, 0, tiles, tiles[x, y, z]); | ||
StandardPathFinder.AddNeighborLink(-1, -1, 0, tiles, tiles[x, y, z]); | ||
z++; | ||
} | ||
y++; | ||
z = 0; | ||
} | ||
y = 0; | ||
x++; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Adds a link to a neighboring tile if the tile exists/is in bounds. | ||
/// </summary> | ||
/// <param name="xChange"></param> | ||
/// <param name="yChange"></param> | ||
/// <param name="zChange"></param> | ||
private static void AddNeighborLink(int xChange, int yChange, int zChange, Tile[,,] tiles, Tile source) | ||
{ | ||
int x = (int)source.Location.X; | ||
int y = (int)source.Location.Y; | ||
int z = (int)source.Location.Z; | ||
|
||
if (x + xChange > -1 && x + xChange < tiles.GetLength(0)) | ||
{ | ||
x += xChange; | ||
} | ||
else | ||
{ | ||
//The neighboring tile didn't exist. | ||
return; | ||
} | ||
|
||
if (y + yChange > -1 && y + yChange < tiles.GetLength(1)) | ||
{ | ||
y += yChange; | ||
} | ||
else | ||
{ | ||
//The neighboring tile didn't exist. | ||
return; | ||
} | ||
|
||
if (z + zChange > -1 && z + zChange < tiles.GetLength(2)) | ||
{ | ||
z += zChange; | ||
} | ||
else | ||
{ | ||
//The neighboring tile didn't exist. | ||
return; | ||
} | ||
|
||
StandardPathFinder.tileConnectionGraph.AddLink(source.Location.ToString(), tiles[x, y, z].Location.ToString(), 101 - tiles[x, y, z].MovementCost); | ||
} | ||
|
||
/// <summary> | ||
/// Adds tiles as nodes into the <see cref="tileConnectionGraph"/>. | ||
/// </summary> | ||
/// <param name="world"></param> | ||
private static void AddNodes(World.World world) | ||
{ | ||
Tile[,,] tiles = world.Tiles; | ||
int xSize = tiles.GetLength(0); | ||
int ySize = tiles.GetLength(1); | ||
int zSize = tiles.GetLength(2); | ||
|
||
int x = 0; | ||
int y = 0; | ||
int z = 0; | ||
|
||
//Iterate over each row. | ||
for (int i = 0; i < xSize; i++) | ||
{ | ||
//Iterate over each column | ||
for (int ii = 0; ii < ySize; ii++) | ||
{ | ||
//Iterate over the depth of each tile in the z axis. | ||
for (int iii = 0; iii < zSize; iii++) | ||
{ | ||
//Each tile can be accessed by the xyz coordinates from this inner loop properly. | ||
StandardPathFinder.tileConnectionGraph.AddNode(tiles[x, y, z].Location.ToString()); | ||
z++; | ||
} | ||
y++; | ||
z = 0; | ||
} | ||
y = 0; | ||
x++; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.