Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Created October 29, 2017 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/8b91e55369da9d5eee0070e5b1f76f3b to your computer and use it in GitHub Desktop.
Save anonymous/8b91e55369da9d5eee0070e5b1f76f3b to your computer and use it in GitHub Desktop.
using Engine.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Engine.Factories;
using System.ComponentModel;
using Engine.EventArgs;
namespace Engine.ViewModels
{
public class GameSession : BaseNotificationClass
{
public EventHandler<GameMessageEventArgs> OnMessageRaised;
private Location _currentLocation;
private Monster _currentMonster;
public World CurrentWorld { get; set; }
public Player CurrentPlayer { get; set; }
public Location CurrentLocation
{
get { return _currentLocation; }
set
{
_currentLocation = value;
OnPropertyChanged(nameof(CurrentLocation));
OnPropertyChanged(nameof(HasLocationToNorth));
OnPropertyChanged(nameof(HasLocationToEast));
OnPropertyChanged(nameof(HasLocationToWest));
OnPropertyChanged(nameof(HasLocationToSouth));
GivePlayerQuestAtLocation();
GetMonsterAtLocation();
}
}
public Monster CurrentMonster
{
get { return _currentMonster; }
set
{
_currentMonster = value;
OnPropertyChanged(nameof(CurrentMonster));
OnPropertyChanged(nameof(HasMonster));
if(CurrentMonster !=null)
{
RaiseMessage("");
RaiseMessage($"You see a {CurrentMonster.Name} here!");
}
}
}
public Weapon CurrentWeapon { get; set; }
public bool HasLocationToNorth =>
CurrentWorld.LocationAt(CurrentLocation.XCoordinate, CurrentLocation.YCoordinate + 1) != null;
public bool HasLocationToEast =>
CurrentWorld.LocationAt(CurrentLocation.XCoordinate + 1, CurrentLocation.YCoordinate) != null;
public bool HasLocationToSouth =>
CurrentWorld.LocationAt(CurrentLocation.XCoordinate, CurrentLocation.YCoordinate -1) != null;
public bool HasLocationToWest =>
CurrentWorld.LocationAt(CurrentLocation.XCoordinate -1, CurrentLocation.YCoordinate) != null;
public bool HasMonster => CurrentMonster != null;
public GameSession()
{
CurrentPlayer = new Player
{
Name = "Scott",
CharacterClass = "Fighter",
HitPoints = 10,
Gold = 1000000,
ExperiencePoints = 0,
Level = 1
};
if (!CurrentPlayer.Weapons.Any())
{
CurrentPlayer.AddItemToInventory(ItemFactory.CreateGameItem(1001));
}
CurrentWorld = WorldFactory.CreateWorld();
CurrentLocation = CurrentWorld.LocationAt(0, 0);
}
public void MoveNorth()
{
if(HasLocationToNorth)
{
CurrentLocation = CurrentWorld.LocationAt(CurrentLocation.XCoordinate, CurrentLocation.YCoordinate + 1);
}
}
public void MoveEast()
{
if (HasLocationToEast)
{
CurrentLocation = CurrentWorld.LocationAt(CurrentLocation.XCoordinate + 1, CurrentLocation.YCoordinate);
}
}
public void MoveSouth()
{
if (HasLocationToSouth)
{
CurrentLocation = CurrentWorld.LocationAt(CurrentLocation.XCoordinate, CurrentLocation.YCoordinate - 1);
}
}
public void MoveWest()
{
if (HasLocationToWest)
{
CurrentLocation = CurrentWorld.LocationAt(CurrentLocation.XCoordinate - 1, CurrentLocation.YCoordinate);
}
}
private void GivePlayerQuestAtLocation()
{
foreach(Quest quest in CurrentLocation.QuestsAvailableHere)
{
if (!CurrentPlayer.Quests.Any(q => q.PlayerQuest.ID == quest.ID))
{
CurrentPlayer.Quests.Add(new QuestStatus(quest));
}
}
}
private void GetMonsterAtLocation()
{
CurrentMonster = CurrentLocation.GetMonster();
}
public void AttackCurrentMonster()
{
if(CurrentWeapon == null)
{
RaiseMessage("You must select a weapon to attack.");
return;
}
//determine damage to monster
int damageToMonster = RandomNumberGenerator.NumberBetween(CurrentWeapon.MinimunDamage, CurrentWeapon.MaximumDamage);
if(damageToMonster == 0)
{
RaiseMessage($"You missed the {CurrentMonster.Name}.");
}
else
{
CurrentMonster.HitPoints -= damageToMonster;
RaiseMessage($"You hit the {CurrentMonster.Name} for {damageToMonster} points.");
}
//if monster is killed, collect rewards and loot
if(CurrentMonster.HitPoints<=0)
{
RaiseMessage("");
RaiseMessage($"You defeated the {CurrentMonster.Name}!");
CurrentPlayer.ExperiencePoints += CurrentMonster.RewardExperiencePoints;
RaiseMessage($"You receive {CurrentMonster.RewardExperiencePoints} experience points!");
CurrentPlayer.Gold += CurrentMonster.RewardGold;
RaiseMessage($"You receive {CurrentMonster.RewardGold} gold.");
foreach(ItemQuantity itemQuantity in CurrentMonster.Inventory)
{
GameItem item = ItemFactory.CreateGameItem(itemQuantity.ItemID);
CurrentPlayer.AddItemToInventory(item);
RaiseMessage($"You receive {itemQuantity.Quantity} {item.Name}.");
}
//get another monster to fight
GetMonsterAtLocation();
}
else
{
//if monster is still alive, let monster attack
int damageToPlayer = RandomNumberGenerator.NumberBetween(CurrentMonster.MinimumDamage, CurrentMonster.MaximumHitPoints);
if (damageToPlayer == 0)
{
RaiseMessage($"The monster attacks, but misses you.");
}
else
{
CurrentPlayer.HitPoints -= damageToPlayer;
RaiseMessage($"The {CurrentMonster.Name} attacks and hit you for {damageToPlayer} points.");
}
//if player is killed, move player back to home location
if (CurrentPlayer.HitPoints <= 0)
{
RaiseMessage("");
RaiseMessage($"The {CurrentMonster.Name} killed you.");
CurrentLocation = CurrentWorld.LocationAt(0, -1);
CurrentPlayer.HitPoints = CurrentPlayer.Level * 10;
}
}
}
private void RaiseMessage(string message)
{
OnMessageRaised?.Invoke(this, new GameMessageEventArgs(message));
}
}
}
using Engine.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Engine.Factories
{
public static class ItemFactory
{
private static readonly List<GameItem> _standardGameItems = new List<GameItem>();
static ItemFactory()
{
_standardGameItems.Add(new Weapon(1001, "Pointy Stick", 1, 1, 2));
_standardGameItems.Add(new Weapon(1002, "Rusty Sword", 5, 1, 3));
_standardGameItems.Add(new GameItem(9001, "Snake Fang", 1));
_standardGameItems.Add(new GameItem(9002, "Snakeskin", 2));
_standardGameItems.Add(new GameItem(9003, "Rat tail", 1));
_standardGameItems.Add(new GameItem(9004, "Rat fur", 2));
_standardGameItems.Add(new GameItem(9005, "Spdier fang", 1));
_standardGameItems.Add(new GameItem(9006, "Spider silk", 2));
}
public static GameItem CreateGameItem(int itemTypeID)
{
GameItem standardItem = _standardGameItems.FirstOrDefault(item => item.ItemTypeID == itemTypeID);
if (standardItem != null)
{
if (standardItem is Weapon)
{
return (standardItem as Weapon).Clone();
}
return standardItem.Clone();
}
return null;
}
}
}
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
namespace Engine.Models
{
public class Player: BaseNotificationClass
{
private string _name;
private string _characterClass;
private int _hitPoints;
private int _experiencePoints;
private int _level;
private int _gold;
public string Name
{
get { return _name; }
set { _name = value;
OnPropertyChanged(nameof(Name));}
}
public string CharacterClass
{
get { return _characterClass; }
set { _characterClass = value;
OnPropertyChanged(nameof(CharacterClass));
}
}
public int HitPoints
{
get { return _hitPoints; }
set { _hitPoints = value;
OnPropertyChanged(nameof(HitPoints));
}
}
public int ExperiencePoints
{
get { return _experiencePoints; }
set { _experiencePoints = value;
OnPropertyChanged(nameof(ExperiencePoints));
}
}
public int Level
{
get { return _level; }
set { _level = value;
OnPropertyChanged(nameof(Level));
}
}
public int Gold
{
get { return _gold; }
set { _gold = value;
OnPropertyChanged(nameof(Gold));
}
}
public ObservableCollection<GameItem> Inventory { get; set; }
public List<GameItem> Weapons =>
Inventory.Where(i => i is Weapon).ToList();
public ObservableCollection<QuestStatus> Quests { get; set; }
public Player()
{
Inventory = new ObservableCollection<GameItem>();
Quests = new ObservableCollection<QuestStatus>();
}
public void AddItemToInventory(GameItem item)
{
Inventory.Add(item);
OnPropertyChanged(nameof(Weapons));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment