Skip to content

Commit

Permalink
Player:
Browse files Browse the repository at this point in the history
Added function to remove item from inventory
Added function to check if player has the right amount of items to finish a quest.
Added new function to check if a player has completed a quest at a given location.
Added call to the new quest complete function to the CurrentLocation constructor.
refactored GIvePlayerQUestsAtLocation function to display text about the quest the player recieved and info about the quest.
Note: Status of the quest does not get updated in the UI. THis is fixed in video 10.6. Problem with IsCompleted function in QUeststatus class doesn't have the ability to set to true.
  • Loading branch information
rhinds1004 committed Sep 3, 2018
1 parent dab285e commit a6381a1
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 3 deletions.
1 change: 1 addition & 0 deletions Engine/Factories/WorldFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ internal static World CreateWorld()
newWorld.AddLocation(-1, -1, "Farmer's House",
"This is the house of your neighbor, Farmer Ted.",
"Farmhouse.png");

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

newWorld.AddLocation(-1, 0, "Trading Shop",
Expand Down
18 changes: 18 additions & 0 deletions Engine/Models/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,23 @@ public void AddItemToInventory(GameItem item)


}

public void RemoveItemFromInventory(GameItem item)
{
Inventory.Remove(item);
OnPropertyChanged(nameof(Weapons));
}

public bool HasAllTheseItems(List<ItemQuantity> items)
{
foreach (ItemQuantity item in items)
{
if(Inventory.Count(i=> i.ItemTypeID == item.ItemID) < item.Quantity)
{
return false;
}
}
return true;
}
}
}
70 changes: 67 additions & 3 deletions Engine/ViewModels/GameSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ public Location CurrentLocation
OnPropertyChanged(nameof(HasLocationToEast));
OnPropertyChanged(nameof(HasLocationToSouth));

CompleteQuestsAtLocation();
GivePlayerQuestsAtLocation();
GetMonsterAtLocation(); //TODO maybe at a timer so even if a player stays in the same area monsters will respawn.
}
}



public Monster CurrentMonster
{
Expand Down Expand Up @@ -128,13 +129,76 @@ public void MoveSouth()
}
}


private void CompleteQuestsAtLocation()
{
foreach(Quest quest in CurrentLocation.QuestAvailableHere)
{
QuestStatus questToComplete =
CurrentPlayer.Quests.FirstOrDefault(q => q.PlayerQuest.ID == quest.ID && !q.IsCompleted);

if(questToComplete != null)
{
if(CurrentPlayer.HasAllTheseItems(quest.ItemsToComplete))
{
//remove quest items from player's inventory
foreach (ItemQuantity itemQuantity in quest.ItemsToComplete)
{
for(int i = 0; i < itemQuantity.Quantity; i++)
{
CurrentPlayer.RemoveItemFromInventory(CurrentPlayer.Inventory.First(item => item.ItemTypeID == itemQuantity.ItemID));

}
}

RaiseMessage("");
RaiseMessage($"You completed the '{quest.Name}' quest");

//Give rewards
CurrentPlayer.ExperiencePoints += quest.RewardExperiencePoints;
RaiseMessage($"You recieve {quest.RewardExperiencePoints} experience");

CurrentPlayer.Gold += quest.RewardGold;
RaiseMessage($"You recieve {quest.RewardGold} gold");

foreach(ItemQuantity itemQuantity in quest.RewardItems)
{
GameItem rewardItem = ItemFactory.CreateGameItem(itemQuantity.ItemID);
CurrentPlayer.AddItemToInventory(rewardItem);
RaiseMessage($"You recieve {rewardItem.Name}");
}

//Mark quest as completed
questToComplete.IsCompleted = true;

}
}
}
}
private void GivePlayerQuestsAtLocation()
{
foreach(Quest quest in CurrentLocation.QuestAvailableHere)
{
if(!CurrentPlayer.Quests.Any(q => q.PlayerQuest.ID == quest.ID)) // USing LINQ to search through the player's quest list and see if any quests avaible at this location are not in the current player's quest list.
if (!CurrentPlayer.Quests.Any(q => q.PlayerQuest.ID == quest.ID)) // USing LINQ to search through the player's quest list and see if any quests avaible at this location are not in the current player's quest list.
{
CurrentPlayer.Quests.Add(new Models.QuestStatus(quest));
CurrentPlayer.Quests.Add(new QuestStatus(quest));
RaiseMessage("");
RaiseMessage($"You recieve the '{quest.Name}' quest");
RaiseMessage(quest.Description);

RaiseMessage("Return with:");
foreach (ItemQuantity itemQuantity in quest.ItemsToComplete)
{
RaiseMessage($" {itemQuantity.Quantity} {ItemFactory.CreateGameItem(itemQuantity.ItemID).Name}");

}
RaiseMessage("And you will recieve:");
RaiseMessage($" {quest.RewardExperiencePoints} experience points");
RaiseMessage($" {quest.RewardGold} gold");
foreach (ItemQuantity itemQuantity in quest.RewardItems)
{
RaiseMessage($" {itemQuantity.Quantity} {ItemFactory.CreateGameItem(itemQuantity.ItemID).Name}");
}
}
}
}
Expand Down

0 comments on commit a6381a1

Please sign in to comment.