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

Skill system optimization

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

Problem

This code feels long and clunky to me. How can I make it shorter, if possible?

I am writing for a libGDX game, and I need a skill algorithm.
I want it so once the player picks up something (which I already have checked for) I set a, b or c to a certain SkillType, the enum down below. Then, I want to get the value of the specific SkillType, by using the enum's methods to get cooldown and cost.

public SkillType[] skills;
public SkillType a, b, c;
public int acost, bcost, ccost;
public int acooldown, bcooldown, ccooldown;

skills = new SkillType[3];
skills[0] = a; skills[1] = b; skills[2] = c;
acost = a.getCost(); bcost = b.getCost(); ccost = c.getCost();
acooldown = a.getCooldown(); bcooldown = b.getCooldown(); ccooldown = c.getCooldown();

public SkillType getSkill(int index) { return skills[index]; }
public void setSkill(int index, SkillType skill) { skills[index] = skill; }


This code declares the enum used above. It has all the skills that I want to use. It declares, inside the SkillType, its cost and cooldown. I do not want to change these values, and that's why I don't have setters.

public static enum SkillType {
        ROAR(1,3),
        DUST_CLOAK(2,10),
        THORN_CLOAK(2,10),
        SPARK(2,5),
        FLUTTER(0,0),
        EMBER(2,2),
        GLOW(0,0),
        SCRATCH(0,0),
        BITE(0,0);

        int cost;
        int cooldown;
        SkillType(int cost, int cooldown) {
                this.cost = cost;
                this.cooldown = cooldown;
        }
        public int getCooldown() { return cooldown; }
        public int getCost() { return cost; }
}


EDIT:

I have updated the enum class to include more items.

```
public class SkillData {

public SkillType[] skills;
public static final int MAX_SKILLS = 3;
public static final int SLOT_PRIMARY = 0;
public static final int SLOT_SECONDARY = 1;
public static final int SLOT_SECONDARY_TWO = 2;

public static enum SkillType {
ROAR(1,3),
DUST_CLOAK(2,10),
THORN_

Solution

skills = new SkillType[2];
skills[0] = a; skills[1] = b; skills[2] = c;


This is simply wrong. You make an array of size 2 and then put three things in it.

Also, you never set a, b, or c, so you might as well leave off the second line.

public int acost, bcost, ccost;
public int acooldown, bcooldown, ccooldown;


These are never read, so you could delete them.

In general, this code feels incomplete. Your enum looks about as short as it can be for the functionality. The rest of the code is unclear about what it is doing. You could probably delete almost all of it.

Code Snippets

skills = new SkillType[2];
skills[0] = a; skills[1] = b; skills[2] = c;
public int acost, bcost, ccost;
public int acooldown, bcooldown, ccooldown;

Context

StackExchange Code Review Q#94505, answer score: 4

Revisions (0)

No revisions yet.