Skip to content

Commit

Permalink
Initial projects, files, sln for .NET Core version of Pyramid 2000
Browse files Browse the repository at this point in the history
  • Loading branch information
drcjt committed Mar 14, 2017
1 parent a83956a commit 9736975
Show file tree
Hide file tree
Showing 41 changed files with 4,358 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Pyramid.NetCore/Pyramid2000.Engine/Implementation/Achievement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Pyramid2000.Engine.Interfaces;

namespace Pyramid2000.Engine.Implementation
{
class Achievement : IAchievement
{
public string Title { get; set; }
public string Description { get; set; }

public Achievement(string title, string description = "")
{
Title = title;
Description = description;
}
}
}
49 changes: 49 additions & 0 deletions Pyramid.NetCore/Pyramid2000.Engine/Implementation/Achievements.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Collections.Generic;

using Pyramid2000.Engine.Interfaces;

namespace Pyramid2000.Engine.Implementation
{
public static class Achievements
{
public readonly static IAchievement EnterThePyramid = new Achievement("Enter the pyramid", "Try not to get lost in the desert");
public readonly static IAchievement GetTheScepterAndBirdBox = new Achievement("Get the bird", "Try using the box to keep it in, but maybe the bird doesn't like something else...");
public readonly static IAchievement DriveAwayTheSerpent = new Achievement("Drive away the serpent", "Serpent's don't like animals with feathers");
public readonly static IAchievement TreasureStolen = new Achievement("Treasure Stolen", "Having treasure stolen isn't always bad, try looking in the maze");
public readonly static IAchievement FindTreasure = new Achievement("Find some treasure", "Once found return it to the pyramid entrance");
public readonly static IAchievement UsePanel = new Achievement("Use the panel", "Panel's aren't in the pyramid to look nice - try and do something with it!");
public readonly static IAchievement MakeBridgeAppear = new Achievement("Bottomless pit", "Maybe waving something will help you to get across");

private static IDictionary<string, IAchievement> _achievements = new Dictionary<string, IAchievement>();

static Achievements()
{
_achievements.Add("EnterPyramid", EnterThePyramid);
_achievements.Add("GetBird", GetTheScepterAndBirdBox);
_achievements.Add("DefeatSerpent", DriveAwayTheSerpent);
_achievements.Add("TreasureStolen", TreasureStolen);
_achievements.Add("FindTreasure", FindTreasure);
_achievements.Add("UsePanel", UsePanel);
_achievements.Add("MakeBridgeAppear", MakeBridgeAppear);
}

public static IAchievement GetAchievement(string AchievementId)
{
return _achievements[AchievementId];
}

public static IList<IAchievement> GetAllAchievements()
{
var achievements = new List<IAchievement>();
achievements.Add(EnterThePyramid);
achievements.Add(GetTheScepterAndBirdBox);
achievements.Add(DriveAwayTheSerpent);
achievements.Add(TreasureStolen);
achievements.Add(FindTreasure);
achievements.Add(UsePanel);
achievements.Add(MakeBridgeAppear);

return achievements;
}
}
}
435 changes: 435 additions & 0 deletions Pyramid.NetCore/Pyramid2000.Engine/Implementation/DefaultScripter.cs

Large diffs are not rendered by default.

225 changes: 225 additions & 0 deletions Pyramid.NetCore/Pyramid2000.Engine/Implementation/Game.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
using System;

using Pyramid2000.Engine.Interfaces;
using Pyramid2000.Engine.Implementation;
using System.Text;

namespace Pyramid2000.Engine
{
public class Game : IGame
{
private readonly IPlayer _player;
private readonly IPrinter _printer;
private readonly IParser _parser;
private readonly IScripter _scripter;
private readonly IRooms _rooms;
private readonly IDefaultScripter _defaultScripter;
private readonly IItems _items;
private readonly IGameState _gameState;
private IResources Resources { get; set; }

public Game(IPlayer player, IPrinter printer, IParser parser, IScripter scripter, IRooms rooms, IDefaultScripter defaultScripter, IItems items, IGameState gameState, IResources resources = null)
{
_player = player;
_printer = printer;
_parser = parser;
_scripter = scripter;
_rooms = rooms;
_defaultScripter = defaultScripter;
_items = items;
_gameState = gameState;
if (resources != null)
{
Resources = resources;
}
else
{
Resources = new Resources();
}

_gameState.BatteryLife = 310;
}

public void ProcessPlayerInput(string input)
{
if (input == null)
{
return;
}

if (_gameState.AskToReincarnate)
{
_scripter.ProcessReincarnation(input);
return;
}

var commands = input.Split(';');
foreach (var command in commands)
{
var parsedCommand = _parser.ParseInput(command);
if (parsedCommand == null)
{
_printer.Print(": ");
return;
}

var handled = false;

var room = _rooms.GetRoom(_player.CurrentRoom);
if (room.Commands != null && room.Commands.ContainsKey(parsedCommand.Function))
{
var script = room.Commands[parsedCommand.Function];
handled = _scripter.ParseScript(script, parsedCommand.Item);
}

if (!handled)
{
var script = _defaultScripter.GetDefaultScript(parsedCommand.Function);
if (script != null)
{
handled = _scripter.ParseScript(script, parsedCommand.Item);
}
}

if (!handled)
{
_printer.PrintLn(Resources.DontKnowHowToApplyWord);
}

DoAfterPlayerTurn();
}

// Only print a prompt for more input if the player is not being asked to reincarnate and hasn't died
if (!_gameState.AskToReincarnate && !_gameState.GameOver)
{
_printer.Print(Resources.Prompt);
}
}

private void DoAfterPlayerTurn()
{
_gameState.TurnCount++;

var lamp = _items.GetExactItemByName("#LAMP_on");
if (!string.IsNullOrEmpty(lamp.Location))
{
_gameState.BatteryLife--;
if (_gameState.BatteryLife == 0)
{
if (!TryChangeBatteries())
{
var lampDead = _items.GetExactItemByName("#LAMP_dead");
lampDead.Location = lamp.Location;
lamp.Location = null;
_printer.PrintLn(Resources.LampOutOfPower);
}
}
else if (_gameState.BatteryLife == 20)
{
_printer.PrintLn(Resources.LampGettingDim);
}
}
if (_gameState.BatteryLife <= 10)
{
TryChangeBatteries();
}
}

private bool TryChangeBatteries()
{
var batteries = _items.GetExactItemByName("#BATTERIES_fresh");
if (batteries.Location == "pack")
{
_printer.PrintLn(Resources.LampGettingDimChangingBatteries);
_gameState.BatteryLife = 310;
batteries.Location = null;
batteries = _items.GetExactItemByName("#BATTERIES_worn");
batteries.Location = "pack";
return true;
}
return false;
}

public void Init()
{
_printer.PrintLn(Resources.Welcome);
_printer.PrintLn("");
_scripter.Look();
_printer.Print(Resources.Prompt);
}

public string State
{
get
{
return Save();
}
set
{
Load(value);
}
}

private string Save()
{
var state = new StringBuilder();
var items = _items.GetAllItems();
foreach (var item in items)
{
var locationOfItem = item.Location;
if (locationOfItem.IndexOf("room_", System.StringComparison.Ordinal) == 0)
{
locationOfItem = locationOfItem.Substring(4);
}
state.Append(locationOfItem);
state.Append(",");
}

return $"LOAD {state}{_player.CurrentRoom},{_gameState.LastRoom},{_gameState.TurnCount},{_gameState.GameOver},{_gameState.BatteryLife},{_gameState.ReincarnateCount}";
}

private void Load(string state)
{
// strip off the "LOAD " bit
state = state.Substring(5);

var splitstate = state.Split(',');

int index = 0;
var items = _items.GetAllItems();
foreach (var item in items)
{
var locationofitem = splitstate[index];
if (locationofitem.Length > 0 && locationofitem[0] == '_')
{
locationofitem = "room" + locationofitem;
}
items[index].Location = locationofitem;

index++;
}

_player.CurrentRoom = splitstate[index++];
if (splitstate[index].Length == 0)
{
_gameState.LastRoom = null;
}
else
{
_gameState.LastRoom = splitstate[index];
}
index++;

_gameState.TurnCount = Convert.ToInt32(splitstate[index++]);
_gameState.GameOver = Convert.ToBoolean(splitstate[index++]);
_gameState.BatteryLife = Convert.ToInt32(splitstate[index++]);
_gameState.ReincarnateCount = Convert.ToInt32(splitstate[index]);

_printer.Clear();

_printer.PrintLn("");
_scripter.Look();
_printer.Print(Resources.Prompt);
}
}
}
13 changes: 13 additions & 0 deletions Pyramid.NetCore/Pyramid2000.Engine/Implementation/GameSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Pyramid2000.Engine.Interfaces;

namespace Pyramid2000.Engine
{
public class GameSettings : IGameSettings
{
public bool Trs80Mode { get; set; } = true;
public bool AllCaps { get; set; }
public bool ClearDialogueOnRoomChange { get; set; }

public IItems Items { get; set; }
}
}
35 changes: 35 additions & 0 deletions Pyramid.NetCore/Pyramid2000.Engine/Implementation/GameState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Collections.Generic;

using Pyramid2000.Engine.Interfaces;

namespace Pyramid2000.Engine
{
public class GameState : IGameState
{
public string LastRoom { get; set; }
public int TurnCount { get; set; }
public bool GameOver { get; set; }
public bool AskToReincarnate { get; set; }
public int ReincarnateCount { get; set; }

public int BatteryLife { get; set; }

public IList<IAchievement> GetAchievements() { return _awardedAchievements; }

private readonly IList<IAchievement> _awardedAchievements = new List<IAchievement>();

public void AwardAchievement(IAchievement achievement)
{
if (!_awardedAchievements.Contains(achievement))
{
_awardedAchievements.Add(achievement);
if (AchievementAwarded != null)
{
AchievementAwarded(achievement);
}
}
}

public event AwardAchievementHandler AchievementAwarded;
}
}
19 changes: 19 additions & 0 deletions Pyramid.NetCore/Pyramid2000.Engine/Implementation/Item.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Pyramid2000.Engine.Interfaces;

namespace Pyramid2000.Engine
{
public class Item : IItem
{
public Item()
{
}

public string Name { get; set; }
public string Location { get; set; }
public bool Packable { get; set; }
public bool Treasure { get; set; }
public string LongDescription { get; set; }
public string ShortDescription { get; set; }
public int Time { get; set; }
}
}
Loading

0 comments on commit 9736975

Please sign in to comment.