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

Data Driven (ability) system

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

Problem

I am making a data drive system for creatures, abilities, items, etc. Considering my ability objects I need to create "templates" from the these since parsing them in real time is too slow. I am trying to find a way to just reference these on my creatures instead of a copy of the reference for each ability.

The thing is I have changeable data in the ability objects that are referenced. This is because the XML data can contain data wherefore no fields exist.

Take a normal attack ability.


    
    Weapon
    damage

    
    
        CREATURE_TARGET
        ATTACK
    

    
    
        MELEE_WEAPON
    

    
    WeaponSpeed
    
    0
    0
    0
    0

    
    
        
        
            
                
                                        
                    Damage
                    PHYSICAL
                    TARGET
                    Weapon
                
            
        
    


A ability could hold as many actions as you want. It could throw 100 spikes where each spike would have a different visual and status effect.

Now let's imagine someone creates a creature with stamina, there is nothing to hold stamina so we need additional parameters. Imagine you want to create a ability that damages this new creatures stamina.


                                        
                    Damage
                    PHYSICAL
                    TARGET
                    stamina_damage 
                


I could also store display information into the parameter to display proper info about the ability.

The actual code

The following code is a clean example of what I am currently trying to do.

```
public class Ability implements CreatureListener {
private final String name;//Not actually final since that would make my constructor too long with parsing everything. Considering on a builder pattern for this.
//...

//This gets changed and used by each creature that has this reference
public HashMap parameters =

Solution

public interface OneOnOneAction {
  public void perform(S sourceCreature, T targetCreature, P parameter);
}

public interface Creature {
  // ...
}

public interface StaminaCreature extends Creature {
  public int getStamina();
  public void setStamina(int stamina);
}

public class TransferStaminaAction implements OneOnOneAction {

  public void perform(StaminaCreature sourceCreature, StaminaCreature targetCreature, Integer parameter) {
    sourceCreature.setStamina( sourceCreature.setStamina() - parameter);
    targetCreature.setStamina( targetCreature.getStamina() + parameter );
  }

}


You can use reflection to build creatures, actions &c. from configuration like


  
    
  


This is just an example to illustrate how behavior can be parameterized without having to invent a Touring-complete language for configuration.

I think however that we don't have enough information on what you're trying to achieve in the end to give more useful advice.

Code Snippets

public interface OneOnOneAction<S extends Creature, T extends Creature, P> {
  public void perform(S sourceCreature, T targetCreature, P parameter);
}


public interface Creature {
  // ...
}

public interface StaminaCreature extends Creature {
  public int getStamina();
  public void setStamina(int stamina);
}


public class TransferStaminaAction implements OneOnOneAction<StaminaCreature, StaminaCreature, Integer> {

  public void perform(StaminaCreature sourceCreature, StaminaCreature targetCreature, Integer parameter) {
    sourceCreature.setStamina( sourceCreature.setStamina() - parameter);
    targetCreature.setStamina( targetCreature.getStamina() + parameter );
  }

}
<action id="transferStaminaAction" class="my.game.TransferStaminaAction"/>
<creature id="staminaMan" class="my.game.StaminaManCreature">
  <actions>
    <actionref id="transferStaminaAction"/>
  </actions>
</creature>

Context

StackExchange Code Review Q#123660, answer score: 3

Revisions (0)

No revisions yet.