HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

RPG character, levelUp system and stat checking for object usage

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
systemcheckingcharacterlevelupusagestatforrpgandobject

Problem

So I am working on a small RPG in Unity.
Right now I am writing some business classes, POCOs, that have no link to Unity except being in the required C# flavor and using Debug.Log and stuff.

I have been doing a lot of Java for years and some C++11, but no C#, so some concepts are not familiar to me.

I think most of the code is so-so, I'd like some advice, maybe someone might see future issues or stuff like that.

Entity class :

using UnityEngine;
using System.Collections;

public abstract class Entity {

public LevelingManager LevelingManager {get; set;}
public EntityStats EntityStats{get; set;}
public int CurrentLifePoints{ get; set;}
}


Fighter class :

using UnityEngine;
using System.Collections;

public class Fighter : Entity {

    public Fighter(){

        this.EntityStats = new FighterStats ();
        this.LevelingManager = new FighterLeveling (EntityStats);
    }

}


EntityStats class :

```
using UnityEngine;
using System.Collections;
using System;

public class EntityStats : IComparable {

public int PhysicalForce{ get; set; }
public int PhysicalDefense{ get; set; }
public int MagicalDefense{ get; set; }
public int MagicalForce{ get; set; }
public int Speed{ get; set; }
public int Life{ get; set; }
public int Level{ get; set; }

public EntityStats(){
Level = 1;
}

#region IComparable implementation
public int CompareTo (object obj)
{
if (null != obj && obj is EntityStats) {

EntityStats stats = obj as EntityStats;

if (stats.Level >= Level
&& stats.Life >= Life
&& stats.MagicalDefense >= MagicalDefense
&& stats.MagicalForce >= MagicalForce
&& stats.PhysicalDefense >= PhysicalDefense
&& stats.PhysicalForce >= PhysicalForce
&& stats.Level >= Level
&& stats.Speed >= Speed) {
return 0;
}
}

Solution

Few notes :

-
public abstract class Entity this should be an interface it provides nothing more than just the signature of some properties. But why do you need so much abstract classes in the first place ? They don't even have any abstract methods/properties in them, you can virtual functions in a non-abstract class.

-
I don't see any problem with the recursion in the AddXP function.

-
public int ExperienceNeededForNextLevel (){
    return  Mathf.FloorToInt (Mathf.Log (ComparableStats.Level + 1) * 100);
}


This function just returns some value it looks and sounds more like a property.

-
Stuff doesn't sounds like a good name.

-
You got some magic numbers in FighterLeveling I'm not sure why you multiple by 2/3, maybe declare them as const variables with appropriate names.

-
In your Entity stats' CompareTo function you can shorten your if

if (null != obj && obj is EntityStats) {

    EntityStats stats = obj as EntityStats;
}


You can just declare the variable outside the statement, give it the value of obj as EntityStats if obj is not an EntityStats it will return null

EntityStats stats = obj as EntityStats;
if(stats != null)
{

}


-
You might want to restrict some of your properties so they can't be set outside of specific classes using private set, because if you have a function TakeDamage() and you perform some animations, some other calculations and also decreasing your player's health in there you definitely don't want to be able to just modify the property Health and instead you want to use the function right ? So restricting your variables is essential.

I'm not sure about the way you have your statistics. As far as I can tell you are still not at that stage where they actually benefit you but there are some important questions you should ask yourself before continuing because they will change most of your current code. Are you able to increase them by leveling up and putting points into them ? Are they providing just flat dps increase, flat health increase or they work on percentages ? Does your character has base stats and by obtaining more PhysicalForce you just add it to his current ? Depending on those answers you might need to construct a Stats class which holds all the useful info like PointsSpentInStats, AmplifierPercentage, etc. and if you don't do that now you will need to restructure a lot of your code later on.

Code Snippets

public int ExperienceNeededForNextLevel (){
    return  Mathf.FloorToInt (Mathf.Log (ComparableStats.Level + 1) * 100);
}
if (null != obj && obj is EntityStats) {

    EntityStats stats = obj as EntityStats;
}
EntityStats stats = obj as EntityStats;
if(stats != null)
{

}

Context

StackExchange Code Review Q#139001, answer score: 2

Revisions (0)

No revisions yet.