Saturday, 4 October 2014

C# Source Code Post #4

Hey guys!

It's Saturday, so it's time for another C# Source Code post!

Last week we started looking at player.cs, but didn't finish it.

So, for reference, here's the code again:

We got up to around line 145 last week, so we'll continue from there.

The next code of note in the file is the LHand property.

This seems like a simple property that stores the ID of the item equipped in the left hand.. until you look at the code.

What's going in in that Set?
Well, it's basically making sure there's a weapon equipped, to avoid errors.

It's ugly, but it works.
First it loads the item into lhandi, and makes sure it's not null. If it is null, the item ID is -1.
Then it checks that it's a weapon, if it is, sure, the ID is value (whatever the code is trying to set LHand to). If it isn't a weapon, ID is -1.
Also, if the value of the item is 1000 or 1001, the ID is -1.
1000 is the Right hand component of Fists (unarmed) and should never be equipped Left hand, 1001 is the Left hand component of Fists.

The code under RHand does pretty much the same stuff.

It's all pretty simple, self explanatory code from there until we get to "public bool CanLevelUp"

This is a property, but acts as a method effectively.
The idea is, for each level, you need a certain amount of XP.
That amount is determined as follows:

for (int i = Level; i > 0; i--)
     val += i * 1000;

So basically, this is a reverse for loop, it starts at a number and goes down until it hits 0.
The number it starts at is the player's current level, and for every level it multiplied it by 1000 and adds it to val.

So if the player is level 3, to get to level 4 the player needs a total of:
(3 * 1000) + (2 * 1000) + (1 * 1000) = 3000 + 2000 + 1000 = 6000
and if the player is level 327, they would need 53,628,000 XP to level up to 328

Next up we have the Constructor of the player class, which is really very simple.

internal Player() { }

This is a special constructor that takes no arguments, and cannot be called from outside the class.
It's required for XML De-Serialization.

public Player(string name, int maxhp, int hp, int lvl, int exp, int str, int end, int dex, int agi, int wis, int inte, Race race)

Well that's a hell of a lot of args that the Player constructor needs. They're mostly self explanatory, but here's a list anyway:
string name - Name of the player
int maxhp - The maximum health of the player
int hp - The current health of the player
int lvl - The current level of the player
int exp - The current XP level of the player
int str - Strength
int end - Endurance
int dex - Dexterity
int agi - Agility
int wis - Wisdom
int inte - Intelligence
Race race - The race of the player (Brand new feature, WIP)

You may be asking why the intelligence arg is "inte" instead of simply "int". This is because C# doesn't like it when you try to call a variable a keyword. int, string, return, public, private, internal, static, void, double, bool, boolean, long, float and several others are all keywords (case sensitive) and can only be used for their primary keyword function.
In Visual Studio C#, keywords like this are highlighted in dark blue.

Next we move onto what the constructor does, aside from assign arg values to properties and fields.

HasQuest = false;

This line ensures new characters don't have a Quest when first created.

LHand = 1000;
RHand = 1000;
Body = -1;
Boots = -1;
Helmet = -1;
Gloves = -1;

Sets the weapons to fists and the armour to none.
Note that both LHand and RHand are set to 1000, even though 1000 is specifically only the Right Hand component, this is because this code was written before I added separate Fist components, and the LHand Set fixes it automatically anyway.

Inv = new List<int>();

As I said last week, the inventory contents of the player are stored as a list of ints, each int corresponding to the ID of the item.

public List<string> GetInv()

This method simply returns a list of the names of all items in the Inventory, in string format.
The code is fairly simple, it just loops through the inventory and adds the name value of each item in the inventory to a string list, then returns that list.

public void AddItem(int itemID, int amt)

A simple method that allows you to add multiple of the same item to the player's inventory.

public void AddItem(int itemID)

An even simpler method because I'm too lazy to type ", 1" for the amt in the previous method when I only want to add 1 of an item.

public void RemoveItem(int itemID, int amt)

A slightly less simple, yet still very simple method that loops through and removes a quantity of an item from the inventory, this can cause issues with large inventories when you attempt to remove more of an item than the player has, because the game will loop through every item in the inventory.

public void RemoveItem(int itemID)

Another lazy method.

public bool HasItem(int itemID, int amt)

Simply check's the player's inventory to see whether they have a certain quantity of a certain item.

Returns true if the amount of that item in the player's inventory is equal or MORE THAN the amount requested. ("return count >= amt;")

public bool HasItem(int itemID)

Yet another lazy method.

public void EquipItem(int itemID)

Okay, stuff starts to get a little in-depth and complex here, so I'll save the rest until next week >:D

Hope you enjoyed reading how lazy I am at coding, also remember that if you have any suggestions on how any of this code can be improved, feel free to let me know in the comments!

Until then, see ya next Tuesday!

No comments:

Post a Comment