Created
January 17, 2017 17:37
-
-
Save kalsamies/6dd3c3f42af9bfa177908d4fa46688f7 to your computer and use it in GitHub Desktop.
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
////Dungeonmap class: | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using RLNET; | |
using RogueSharp; | |
namespace Roisto.Core | |
{ | |
// Our custom DungeonMap class extends the base RogueSharp Map class | |
public class Dungeonmap : Map | |
{ | |
// Returns true when able to place the Actor on the cell or false otherwise | |
public bool SetActorPosition(Actor actor, int x, int y) | |
{ | |
// Only allow actor placement if the cell is walkable | |
if (GetCell(x, y).IsWalkable) | |
{ | |
// The cell the actor was previously on is now walkable | |
SetIsWalkable(actor.X, actor.Y, true); | |
// Update the actor's position | |
actor.X = x; | |
actor.Y = y; | |
// The new cell the actor is on is now not walkable | |
SetIsWalkable(actor.X, actor.Y, false); | |
// Don't forget to update the field of view if we just repositioned the player | |
if (actor is Player) | |
{ | |
UpdatePlayerFieldOfView(); | |
} | |
return true; | |
} | |
return false; | |
} | |
// A helper method for setting the IsWalkable property on a Cell | |
public void SetIsWalkable(int x, int y, bool isWalkable) | |
{ | |
Cell cell = GetCell(x, y); | |
SetCellProperties(cell.X, cell.Y, cell.IsTransparent, isWalkable, cell.IsExplored); | |
} | |
// This method will be called any time we move the player to update field-of-view | |
public void UpdatePlayerFieldOfView() | |
{ | |
Player player = Game.Player; | |
// Compute the field-of-view based on the player's location and awareness | |
ComputeFov(player.X, player.Y, player.Awareness, true); | |
// Mark all cells in field-of-view as having been explored | |
foreach (Cell cell in GetAllCells()) | |
{ | |
if (IsInFov(cell.X, cell.Y)) | |
{ | |
SetCellProperties(cell.X, cell.Y, cell.IsTransparent, cell.IsWalkable, true); | |
} | |
} | |
} | |
// The Draw method will be called each time the map is updated | |
// It will render all of the symbols/colors for each cell to the map sub console | |
public void Draw(RLConsole mapConsole) | |
{ | |
mapConsole.Clear(); | |
foreach (Cell cell in GetAllCells()) | |
{ | |
SetConsoleSymbolForCell(mapConsole, cell); | |
} | |
} | |
private void SetConsoleSymbolForCell(RLConsole console, Cell cell) | |
{ | |
// When we haven't explored a cell yet, we don't want to draw anything | |
if (!cell.IsExplored) | |
{ | |
return; | |
} | |
// When a cell is currently in the field-of-view it should be drawn with ligher colors | |
if (IsInFov(cell.X, cell.Y)) | |
{ | |
// Choose the symbol to draw based on if the cell is walkable or not | |
// '.' for floor and '#' for walls | |
if (cell.IsWalkable) | |
{ | |
console.Set(cell.X, cell.Y, Colors.FloorFov, Colors.FloorBackgroundFov, '.'); | |
} | |
else | |
{ | |
console.Set(cell.X, cell.Y, Colors.WallFov, Colors.WallBackgroundFov, '#'); | |
} | |
} | |
// When a cell is outside of the field of view draw it with darker colors | |
else | |
{ | |
if (cell.IsWalkable) | |
{ | |
console.Set(cell.X, cell.Y, Colors.Floor, Colors.FloorBackground, '.'); | |
} | |
else | |
{ | |
console.Set(cell.X, cell.Y, Colors.Wall, Colors.WallBackground, '#'); | |
} | |
} | |
} | |
} | |
} | |
/////End of this class | |
/////CommandSystemClass: | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Roisto.Core; | |
namespace Roisto.System | |
{ | |
public class CommandSystem | |
{ | |
// Return value is true if the player was able to move | |
// false when the player couldn't move, such as trying to move into a wall | |
public bool MovePlayer(Direction direction) | |
{ | |
int x = Game.Player.X; | |
int y = Game.Player.Y; | |
switch (direction) | |
{ | |
case Direction.Up: | |
{ | |
y = Game.Player.Y - 1; | |
break; | |
} | |
case Direction.Down: | |
{ | |
y = Game.Player.Y + 1; | |
break; | |
} | |
case Direction.Left: | |
{ | |
x = Game.Player.X - 1; | |
break; | |
} | |
case Direction.Right: | |
{ | |
x = Game.Player.X + 1; | |
break; | |
} | |
default: | |
{ | |
return false; | |
} | |
} | |
if (Game.DungeonMap.SetActorPosition(Game.Player, x, y)) | |
{ | |
return true; | |
} | |
return false; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment