All my “whys?” where rhetorical.
I’m not having much free time ATM so I banged out some junk skeleton code to show one possible structure: http://www.java-gaming.org/?action=pastebin&id=1359
All my “whys?” where rhetorical.
I’m not having much free time ATM so I banged out some junk skeleton code to show one possible structure: http://www.java-gaming.org/?action=pastebin&id=1359
Thank you all for your support.
The way of creating a large variety of spells and weapons is working now.
All the items do have the following in them:
short type
the types that are currently possible are the following:
public static final short allType = -1;
public static final short equip = 2;
public static final short consumable = 4;
public static final short material = 8;
public static final short spell = 16;
public static final short weapon = 32;
public static final short shield = 64;
public static final short backpack = 128;
public static final short ammo = 256;
public static final short face = 512;
public static final short helmet = 1024;
public static final short body = 2048;
public static final short boots = 4096;
public static final short gloves = 8192;
The reason why I’ve done this is to make it quite easy to create an item that is made out of multiple types.
for example I have a sword:
Sword sword(short(equip & sword))
If you write it in shorts:
equip = 0000000000000010
weapon = 0000000000100000
/* so sword has those types together */
sword = 0000000000100010
so sword will have the type:
But with only this knowledge I am stuck:
I have a cell, cellA that only accepts equip, and cellB that accepts equip, weapon and shield but how to test if the bit on that place is 1 or 0?
I don’t know if it is possible to use these kind of bit operators anyways, so if you know anything about this subject, provide me with some of your delicious information.
As always, thanks for reading
-RoseSlayer
Why in Gosling’s name wouldn’t you just use interfaces instead?
Or better yet, a “composition over inheritance” approach like we’ve been talking about?
I mean, how you approach it is up to you. But this seems like a pretty strange approach.
I am using composition over inheritance, but the type is declared in the very base of all things.
The (short)type is only used for holdingcells that only can hold that type (of a combination like that (i.e. CellB)). I think this is the most clean and best way to approach this situation. I am not using instanceof etcetera, also the possibilities of doing it this way is endless. Why do I need to create new lines of codes if I only need to test if a bit of the short is 0 or 1?
Thanks for responding as fast as you just did.
-RoseSlayer
I would argue that what you’re doing here is basically like having an array of booleans. Instead of that, why not use an ArrayList that holds an enum type?
Like I said, which approach you take really depends on you. If you’re really sticking with this bit approach, then check out bit shifting and the Integer class for some useful functions.
Test a bit:
public static boolean test(short mask, short tested) {
return mask & tested != 0;
}
// e.g. test an item for equip-ability
boolean isEquipable = test(equip, item);
Also, are you sure you’ll only ever need 16 traits? (bits) It’ll be painful if you eventually find out you need more.
EDIT: the sort of cleaner way of doing this is with EnumSets, btw
Well, currently I am using 14 types and I can switch to integers to use 32bit…
As you both suggest that I should use Enum types, I will gather some information about this subject. Thanks for helping me out. Honestly I also like to argue a bit with these kinds of things, it provides some usefull information. Well thanks again for the effort
-RoseSlayer
Even better is the extensible enum. It’s basically an enum with an interface (can be an empty one say IExtEnum) and you can use multiple enums and define new ones as you add new characteristics as you go and use them all via IExtEnum. Just use the interface and you can use any extensible enum value that implements that interface. The one downside is that the optimized collection EnumSet only works with one particular enum. In my efforts I’ve created an “ExtEnumSet” collection which allows bit twiddling under the hood to work like EnumSet, but for extensible enums. That way you can stuff all the unconnected state into one collection and even run tests against it with other ExtEnumSets defining exactly what you are looking for.
And while composition was touched on in this thread consider the “entity system” architecture or more generally component architectures. Use the type system of Java to your advantage.
There has been plenty of discussion on entity systems on JGO and elsewhere, but not everything you read will be accurate per se. If you Google “entity system extensible enum” you’ll come across some of my ramblings on the subject.