How to create a flexible weapon system

Started by
7 comments, last by Tombo 20 years, 2 months ago
I''m making a 2D action-adventure game in java and I want to have support for different types of weapons and the ability to modify them. Does anyone know of a good way to conceptually do this? A possibility that I have thought of is creating separate images for the character sprites and weapons and then overlaying one image over the other or possibly creating a temporary new image from the two of them. If the images were separate in game, then I would probably be able to do collision detection on the weapon versus a target such as the player or an enemy. If anybody knows about how it was done in Diablo 2 and how the sword collision detection works in 2D Zeldas that would also help. Thanks
-Tombo
Advertisement
1 word: polymorphism
____________________________________________________________AAAAA: American Association Against Adobe AcrobatYou know you hate PDFs...
^ read the question.

polymorphism isnt supported in java as there isnt public multiple inheritance.

** stealth edit **

technically it is supported:
The generic term for having many forms. You can use the same name for several different things and the compiler automatically figures out which version you wanted. There are several forms of polymorphism supported in Java, shadowing, overriding, and overloading.

but you still cant do multiple inheritance (interfaces arent classes...). and your reply still doesnt answer his question.

****

firstly i would try to group the weapons you are going to use (ranged, swords etc...), decide on a suitable object heirachy and implement that. all the data for the weapons should be script drive (so dont hard code it) and that will make adding weapons really easy!

if you have weapons and items coming from the same base class you can stick them all in the same inventory.

As it's a 2D game, why dont you just give each weapon a range and see if the enemy is in range of the weapon, or something like that.

try to keep it simple.

[edited by - jonnii on January 25, 2004 9:53:51 AM]
-jonnii=========jon@voodooextreme.comwww.voodooextreme.com
quote:Original post by jonnii
^ read the question.

polymorphism isnt supported in java as there isnt public multiple inheritance.

** stealth edit **

technically it is supported:
The generic term for having many forms. You can use the same name for several different things and the compiler automatically figures out which version you wanted. There are several forms of polymorphism supported in Java, shadowing, overriding, and overloading.

but you still cant do multiple inheritance (interfaces arent classes...). and your reply still doesnt answer his question.

****

firstly i would try to group the weapons you are going to use (ranged, swords etc...), decide on a suitable object heirachy and implement that. all the data for the weapons should be script drive (so dont hard code it) and that will make adding weapons really easy!

if you have weapons and items coming from the same base class you can stick them all in the same inventory.

As it''s a 2D game, why dont you just give each weapon a range and see if the enemy is in range of the weapon, or something like that.

try to keep it simple.

[edited by - jonnii on January 25, 2004 9:53:51 AM]



Polymorphism is arguably supported in Java. Java is a perversion however they call overriding, overloading and shadowing polymorphism.

OOP, UML particularry leads to total retardation. I propose simplistic partly functional approach. Also forget stupid design patterns like managerSingleton.lolParticleFactory.weaponzRawesome Monostate.Designpatternsrcoal.weaponInterface().

Common sense.
The way my weapon system works is there is a weapon manager that holds instances of all the weapons. Each weapon has an onDetonate event that points to other instances of these weapons in the weapon managers. When a new instance of a weapon is needed in the game (say you throw a grenade) then it looks up "BASIC GRENADE" in the manager, finds it, then returns a new copy of the grenade instance stored in the manager. Now say the grenade blows up and the onDetonate event happens, it now creates a copy of whatever is pointed to by onDetonate.

Using this method you could easily create nuke grenades, shrapnel grenades, mirv grenades, etc all from just a single simple grenade and some explosion types. You could make a grenade that exploded into more grenades, and all those exploded into more grenades, etc.

So far it seems to work okay. In theory it should be possible for the user to create new weapons in game. Say the player is a sorceror. Maybe by combining different gems or runes or spell elements the player could create their own magic. Instead of learning the "fireball" spell he may learn how to conjur fire (a "flashlight" spell) then learn telekinesis, and by putting the two together he can now throw fireballs.

Game Development Wiki
I like the DARK layout!
First off, you need a scripting language to make this worth your while. I know Lua has Java bindings, and I think Python has java bindings as well. Anyway, I would suggest not making whole weapons, but make parts of weapons, and puting them together in different ways. For example, a Lightning gun would have a battery, combined with a laser sight, combined with a ion stream generator (or something like that). Just make sure that the player will have some idea of what the weapon does before he/she puts it together.

As for making the weapons fit into the character's hands, you should draw the character so that the weapon will stay at the same place (relative to the sprite) every frame. You could go all out and have a "weapon start pixel" in each frame, but thats overkill. Then make the player mount each weapon he/she made onto something that has been predrawn to fit the character's hands.

[edited by - Evangelion on January 25, 2004 1:10:19 PM]
quote:Original post by jonnii
^ read the question.

polymorphism isnt supported in java as there isnt public multiple inheritance.

** stealth edit **

technically it is supported:
The generic term for having many forms. You can use the same name for several different things and the compiler automatically figures out which version you wanted. There are several forms of polymorphism supported in Java, shadowing, overriding, and overloading.

but you still cant do multiple inheritance (interfaces arent classes...). and your reply still doesnt answer his question.

****


I beg your pardon, multiple inheritance has nothing to do with polymorphism at all. In fact, single inheritance is not required to support polymorphism. C++''s templates offer an entirely different sort of polymorphism. Inheritance is arguably a concept derived from polymorphism and delegation, and isn''t really fundamental.

As for Java''s interfaces, the only thing they don''t let you do that true multiple inheritance does is to share implementation from multiple sources. In exchange for that you don''t have to deal with diamond inheritance problems.

Of course, saying "one word: polymorphism" doesn''t answer the OP''s question - it hardly answers anyone''s question But I felt the need to tear this idea apart before it took seed.

(BTW, in Java you can override methods but you can''t "overload" which afaik is terminology only applied to operators... a special case is made for ''+'' with java.lang.String which is pretty much built right into the language.)

quote:
firstly i would try to group the weapons you are going to use (ranged, swords etc...), decide on a suitable object heirachy and implement that. all the data for the weapons should be script drive (so dont hard code it) and that will make adding weapons really easy!

if you have weapons and items coming from the same base class you can stick them all in the same inventory.

As it''s a 2D game, why dont you just give each weapon a range and see if the enemy is in range of the weapon, or something like that.

try to keep it simple.


This part is good advice. I''m not sure what you mean by "script driven", but yes, concrete weapon subclasses should have data members for things like weight, length etc. rather than being hardcoded as a property associated with the weapon type.

quote:Original post by Captian Goatse
Polymorphism is arguably supported in Java. Java is a perversion however they call overriding, overloading and shadowing polymorphism.


Er, perversion of what exactly?

See above. And see also http://c2.com/cgi/wiki?PolyMorphism.

quote:
OOP, UML particularry leads to total retardation. I propose simplistic partly functional approach. Also forget stupid design patterns like managerSingleton.lolParticleFactory.weaponzRawesome Monostate.Designpatternsrcoal.weaponInterface().

Common sense.


(Please don''t ever let me hear you again associate OOP in general with that kind of nonsense.)

Yes, common sense please people. Add complexity as it becomes needed. Do things the obvious way. Picture the world from your object''s perspective, and it should be evident what to do.
I think Diablo 2 just has a swinging animation for each weapon type, and each weapon is drawn in the right position for each frame of the character''s animation so that they match up. The weapons have art for the variety of poses they need to have to match up correctly with the character''s hand. And it doesn''t even really do collision detection between melee weapons and enemies.

Zelda''s collision detection is probably done by checking if the sword sprite (or some rough approximation of it) is overlapping the enemy.

You could model the shape of weapons geometrically (and also the way the weapons are used) and check for collisions between the weapon geometry and the enemy geometry. The geometry can be different for different weapons.
Thanks for the suggestions.

I''ll look into creating a weapon system where each weapon matches the sprite motion and that way I''ll hopefully be able to have enemies use weapons that the player can also use. As for collision detection, I might have to keep a player direction plus the radius of the weapon.

-Tombo

This topic is closed to new replies.

Advertisement