using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using UPC = System.Type;
namespace NinjaRoyale.Collections
{
/**
* Keeps track of all the collections collected inside of the game for any given player
*
* UPC - Universal Product Code, Finds out what kind of Collectible an object is
*
* Author - Mavin Martin
* Date - 11/21/2012
*/
public class CollectionsManager
{
private static CollectionsManager instance = null;
Dictionary<Entity, Dictionary<UPC, int>> ownerStash;
/**
* ===== CollectionsManager =====
*
* Uses Singleton Pattern to guarantee only one Collection Manager
*/
private CollectionsManager()
{
ownerStash = new Dictionary<Entity, Dictionary<UPC, int>>();
}
/**
* AddCollectable - Add's Collectable to the player
*
* PARAMS
* owner - Independent object owning the collectible
* item - Item owned by owner
*/
public void AddCollectable(Entity owner, Collectible item)
{
Dictionary<UPC, int> stash;
if (!ownerStash.TryGetValue(owner, out stash))
stash = initiateStash(owner);
addItemToStash(stash, item);
}
/**
* getStashItems - Get's the stash items owned by a certain owner
*
* PARAMS
* owner - Independent object owning the stash
*
* RETURN
* Dictionary<UPC, int> - Stash of items as linked list
*/
public Dictionary<UPC, int> getStashItems(Entity owner)
{
Dictionary<UPC, int> stash;
if (!ownerStash.TryGetValue(owner, out stash))
stash = initiateStash(owner);
return stash;
}
/**
* RemoveCollectible - Removes Collectible from the player.
*
* If you are getting rid of all items for an owner, please call RemoveOwner instead.
*
* PARAMS
* owner - Independent object owning items
* item - Item belonging to the owner
*/
public void RemoveCollectible(Entity owner, Collectible item)
{
Dictionary<UPC, int> stash;
ownerStash.TryGetValue(owner, out stash);
UPC upc = getUPC(item);
stash.Remove(upc);
}
/**
* RemoveOwner - Removes an owner from the Collections Manager.
*
* IMPORTANT NOTE: Owner does not need to add themselves. This happens immediately when adding collectible
*
* PARAMS
* owner - the Entity that will own the collectible
*/
public void RemoveOwner(Entity owner)
{
ownerStash.Remove(owner);
}
/**
* addItemToStash - Add's item to the stash for a given player
*
* PARAMS
* stash - Linked List of items in the stash
* item - Item to be added to the stash
*/
private void addItemToStash(Dictionary<UPC, int> stash, Collectible item)
{
int count = 0;
UPC upc = getUPC(item);
if (!stash.TryGetValue(upc, out count))
initiateItem(stash, item);
stash[upc] += 1;
}
/**
* initiateItem - Initiates item in a given stash
*
* PARAMS
* stash - Linked List of items in the stash
* item - Item to be added to the stash
*/
private void initiateItem(Dictionary<UPC, int> stash, Collectible item)
{
UPC upc = getUPC(item);
stash.Add(upc, 0);
}
/**
* getUPC - Get's the UPC of a specific Collectible
*
* RETURN
* UPC - Type of item being worked with
*/
public static UPC getUPC(Collectible item)
{
return item.GetType();
}
/**
* initiateStash - Registers an owner inside of the Collections Manager
*
* PARAMS
* owner - The independent object owning a stash
*
* RETURN
* Dictionary<UPC, int> - Stash for a given player
*/
private Dictionary<UPC, int> initiateStash(Entity owner)
{
Dictionary<UPC, int> stash = new Dictionary<UPC, int>();
ownerStash.Add(owner, stash);
return stash;
}
/**
* GetInstance - Grabs Collection Manager Instance
*
* GUARANTEED SINGLETON
*
* RETURN
* CollectionsManager - Instance of the Collections Manager
*/
public static CollectionsManager GetInstance()
{
if (instance == null)
{
instance = new CollectionsManager();
}
return instance;
}
}
}
Friday, November 16, 2012
Collections Manager
I wanted to work with a Singleton Pattern and have a class that can be moved from project to project without having to do much modification in work. I have come up with a collections manager that keeps track of what players have collected inside of a given level. The code looks like the following:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment