Skip to content

Commit

Permalink
NOTE:!!!!!! This is broken code. WIthin Gamession class . the Current…
Browse files Browse the repository at this point in the history
…Location constructor is incorrect. the line _currentTrader = CurrentLocation.TraderHere is WRONG!! Doing this push to document a bad bug that was not easily seen as debugging output gives no indication that there is a problem. I Believe the issue is that even though a trader is correctly assigned, the value is garbage collected as it goes out of scope once whatever MoveX() function is finished. By using the corrected line CurrentTrader = CurrentLocation.TraderHere; a new Trader object is created that doesn't go out of scope. I think???

Added trader class.
Added traderfactory.
Location:
Added functions to get and set a trader in a location
WorldFactory:
added traders at locations (-1,0) , (0,1), (-1,-1)
//TODO figure out a way to link words with the coords.
Gamession:
Added CurrentTrader property variable
Added call to get the currentTrader in the currentLocation constructor.
Added function to get and set CurrentTrader which boardcast properchange.
Added function that returns if the location has a trader that will be used to control the visibility of a trade button by the UI.

MainWIndow:
Added trade button. Visability is controled by the HasTrader bool in Gamesession.
  • Loading branch information
rhinds1004 committed Sep 4, 2018
1 parent a6381a1 commit 398ee58
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Engine/Engine.csproj
Expand Up @@ -44,6 +44,7 @@
<Compile Include="EventArgs\GameMessageEventArgs.cs" />
<Compile Include="Factories\MonsterFactory.cs" />
<Compile Include="Factories\QuestFactory.cs" />
<Compile Include="Factories\TraderFactory.cs" />
<Compile Include="Factories\WorldFactory.cs" />
<Compile Include="Models\GameItem.cs" />
<Compile Include="Factories\ItemFactory.cs" />
Expand All @@ -54,6 +55,7 @@
<Compile Include="Models\Player.cs" />
<Compile Include="Models\Quest.cs" />
<Compile Include="Models\QuestStatus.cs" />
<Compile Include="Models\Trader.cs" />
<Compile Include="Models\Weapon.cs" />
<Compile Include="Models\World.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
45 changes: 45 additions & 0 deletions Engine/Factories/TraderFactory.cs
@@ -0,0 +1,45 @@
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 TraderFactory
{
private static readonly List<Trader> _traders = new List<Trader>();

static TraderFactory()
{
Trader susan = new Trader("Susan");
susan.AddItemToInventory(ItemFactory.CreateGameItem(1001));

Trader farmerTed = new Trader("Framer Ted");
farmerTed.AddItemToInventory(ItemFactory.CreateGameItem(1001));

Trader peteTheHerbalist = new Trader("Pete the Herbalist");
peteTheHerbalist.AddItemToInventory(ItemFactory.CreateGameItem(1001));

AddTraderToList(susan);
AddTraderToList(farmerTed);
AddTraderToList(peteTheHerbalist);
}

public static Trader GetTraderByName(string name)
{
return _traders.FirstOrDefault(t => t.Name == name);
}

private static void AddTraderToList(Trader trader)
{
if(_traders.Any(t => t.Name == trader.Name)) // ensures only one of each trader gets added
{
throw new ArgumentException($"Trader '{trader.Name}' already exists");
}

_traders.Add(trader);
}
}
}
4 changes: 4 additions & 0 deletions Engine/Factories/WorldFactory.cs
Expand Up @@ -13,6 +13,7 @@ internal static World CreateWorld()
{
World newWorld = new World();

//TODO find a way to link names to location coords.
newWorld.AddLocation(-2, -1, "Farmer's Field",
"There are rows of corn growing here, with giant rats hiding between them.",
"FarmFields.png");
Expand All @@ -23,12 +24,14 @@ internal static World CreateWorld()
newWorld.AddLocation(-1, -1, "Farmer's House",
"This is the house of your neighbor, Farmer Ted.",
"Farmhouse.png");
newWorld.LocationAt(-1, -1).TraderHere = TraderFactory.GetTraderByName("Farmer Ted");

newWorld.AddLocation(0, -1, "Home", "This is your home", "Home.png");

newWorld.AddLocation(-1, 0, "Trading Shop",
"The shop of Susan, the trader.",
"Trader.png");
newWorld.LocationAt(-1, 0).TraderHere = TraderFactory.GetTraderByName("Susan");

newWorld.AddLocation(0, 0, "Town square",
"You see a fountain here.",
Expand All @@ -47,6 +50,7 @@ internal static World CreateWorld()
newWorld.AddLocation(0, 1, "Herbalist's hut",
"You see a small hut, with plants drying from the roof.",
"HerbalistsHut.png");
newWorld.LocationAt(0, 1).TraderHere = TraderFactory.GetTraderByName("Pete the Herbalist");

newWorld.LocationAt(0, 1).QuestAvailableHere.Add(QuestFactory.GetQuestByID(1));// this makes it so you don't need a temp variable to hold the location

Expand Down
2 changes: 2 additions & 0 deletions Engine/Models/Location.cs
Expand Up @@ -18,6 +18,8 @@ public class Location

public List<MonsterEncouter> MonstersHere { get; set; } = new List<MonsterEncouter>();

public Trader TraderHere { get; set; }

public void AddMonster(int monsterID, int chanceOfEncountering)
{
if(MonstersHere.Exists(m => m.MonsterID == monsterID))
Expand Down
31 changes: 31 additions & 0 deletions Engine/Models/Trader.cs
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Engine.Models
{
public class Trader : BaseNotificationClass
{
public string Name { get; set; }

public ObservableCollection<GameItem> Inventory { get; set; }

public Trader(string name)
{
Name = name;
Inventory = new ObservableCollection<GameItem>();
}

public void AddItemToInventory(GameItem item)
{
Inventory.Add(item);
}
public void RemoveItemToInventory(GameItem item)
{
Inventory.Remove(item);
}
}
}
16 changes: 15 additions & 1 deletion Engine/ViewModels/GameSession.cs
Expand Up @@ -17,6 +17,7 @@ public class GameSession : BaseNotificationClass
#region Properties
private Location _currentLocation;
private Monster _currentMonster;
private Trader _currentTrader;

public World CurrentWorld { get; set; }
public Player CurrentPlayer { get; set; }
Expand All @@ -35,6 +36,8 @@ public Location CurrentLocation
CompleteQuestsAtLocation();
GivePlayerQuestsAtLocation();
GetMonsterAtLocation(); //TODO maybe at a timer so even if a player stays in the same area monsters will respawn.

_currentTrader = CurrentLocation.TraderHere;
}
}

Expand All @@ -57,7 +60,16 @@ public Monster CurrentMonster
}
}


public Trader CurrentTrader
{
get { return _currentTrader; }
set
{
_currentTrader = value;
OnPropertyChanged(nameof(CurrentTrader));
OnPropertyChanged(nameof(HasTrader));
}
}

//TODO might be away to simplify this..
public bool HasLocationToNorth =>
Expand All @@ -75,6 +87,8 @@ public Monster CurrentMonster

public bool HasMonster => CurrentMonster != null;

public bool HasTrader => CurrentTrader != null;

#endregion

public GameSession()
Expand Down
4 changes: 4 additions & 0 deletions UI/MainWindow.xaml
Expand Up @@ -216,6 +216,10 @@
Width="65" Margin="10" Click="OnClick_MoveWest"
Visibility="{Binding HasLocationToWest, Converter={StaticResource BooleanToVisibility}}"
Content="West" />
<Button Grid.Row="1" Grid.Column="1" Height="25"
Width="65" Margin="10"
Visibility="{Binding HasTrader, Converter={StaticResource BooleanToVisibility}}"
Content="Trade" />

<Button Grid.Row="1" Grid.Column="2" Height="25"
Width="65" Margin="10" Click="OnClick_MoveEast"
Expand Down

0 comments on commit 398ee58

Please sign in to comment.