Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (408)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Game Mechanics / Re: Possible to make an enum of anonymous inner classes? on: 2012-12-06 23:30:31
I'm going to make one last attempt to get my point across in case anyone is still confused. I wrote a prototype Weapon class similar to what I was looking for. Instead of using an enum, I made a class called "WeaponType" with public members containing the anonymous inner class objects. Note that what the fire() method does in this example is irrelevant. It's to demonstrate what I meant.

Main Class: http://pastebin.com/MYewSQgv
Weapon Class: http://pastebin.com/kAJrpVGL
Weapon Interface: http://pastebin.com/MQyv1nzC
Weapon Type Class (Contains anonymous inner classes): http://pastebin.com/QbFuLyMy

I hope I got the point across this time.
2  Game Development / Game Mechanics / Re: Possible to make an enum of anonymous inner classes? on: 2012-12-06 17:21:14
I already know HOW to make an anonymous inner class to implement the weapons the way I want... the only reason I asked this was to determine if I could somehow put the declarations for these in a separate file and be able to just import them and use an ID to refer to the object without cluttering up my main code.
3  Game Development / Game Mechanics / Re: Possible to make an enum of anonymous inner classes? on: 2012-12-06 17:12:35
Well at least you get it... but the whole point of this was I don't want to have to create 20 different classes for 20 different weapon types. Meanwhile, everyone else still thinks I mean I want different parameters to define damage, speed, etc...

You don't have to create 20 different classes - you only create a NEW CLASS when you need BEHAVIOUR that is different.

Right, but the amount of classes I'll need is equivalent to the number of uniquely different weapons, which is a lot, considering the behavior of a machine gun, flamethrower, rocket launcher, and shotgun are all different. Besides... how do you know how many different weapon types I'll have?
4  Game Development / Game Mechanics / Re: Possible to make an enum of anonymous inner classes? on: 2012-12-06 16:44:38
If I understand correctly, you want to be able to define various types of weapons and new behaviors for them at runtime? If so you could do something like below.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
interface Weapon {
    void fire(...);
 
    void reload(...);
}

class DefaultWeapon implements Weapon {
   
    private final WeaponMetaData meta;

    public DefaultWeapon(WeaponMetaData data /* type specific data such as bullet style, speed, etc. */) { this.meta = data; }

    void fire(...) {
    } // fire off a single bullet for default behavior

    void reload(...) {
    }
}

class FlamethrowerWeaponImpl extends DefaultWeapon {
 
    public FlamethrowerWeaponImpl(WeaponMetaData meta) {
        super(meta);
    }  

    @Override
    void fire(...) {
    } //do what has to be done for a specialized implementation.
}

This way you can create specialized implementations for weapons that don't follow the standard "spawn an accelerated particle" behavior.
1  
2  
3  
static Weapon PISTOL = new DefaultWeapon(pistolMetaData);
static Weapon RIFLE = new DefaultWeapon(rifleMetaData);
static Weapon FLAMETHROWER = new FlamethrowerWeaponImpl(flamethrowerMetaData);


Well at least you get it... but the whole point of this was I don't want to have to create 20 different classes for 20 different weapon types. Meanwhile, everyone else still thinks I mean I want different parameters to define damage, speed, etc...
5  Game Development / Game Mechanics / Re: Possible to make an enum of anonymous inner classes? on: 2012-12-06 15:48:42
I'm a big fan of composition/data-driven.  See here for a semi-write-up for a set of schemes that deal with the possibility of many individual behaviors: http://www.java-gaming.org/topics/archtypes-composition-and-code-is-data/26554/view.html

But that's not an example of different behaviors... that was an example of the same behavior and the class being designed to do the same thing but with different parameters. What I'm trying to do is create a different method to handle what the weapon actually does for each weapon object. This way, I can make a flamethrower create a spray of particles, whereas a machine gun would just shoot bullets really fast, or a napalm gun would fire a projectile that when it hits its target, small flame particles spread out around the impact site and burn for a short time, doing damage to any that touch it, before extinguishing.
6  Game Development / Game Mechanics / Re: Possible to make an enum of anonymous inner classes? on: 2012-12-06 15:12:33
You could use an .properties file to import the weapons script into the game eg. for flamethrower:

Weapon Name: FLAMETHROWER
Weapon Type: FIRE
Weapon Damage: x
Weapon Reload: y
Weapon Range: RANGED

You could do it like that (in the .properties file Grin).
P.S. The x and y are your variables for the weapons (Reload might not be applicable Grin)

No I couldn't. I'm not just talking about different parameters. Each weapon would have to have a different update method. What if one weapon just shoots simple projectiles, another is a spray weapon, and another explodes and creates more particles that shoot outward when it reaches its destination?

If it helps, I also need to figure out how to create a spell system for a game with magic in the future, if that's any indication as to the level of "customization" I need with each of these weapons.
7  Game Development / Game Mechanics / Re: Possible to make an enum of anonymous inner classes? on: 2012-12-06 15:06:08
Ok, nobody seems to be following me here, so here's a code example.
Since you've told me I can't bind an enum ID to an anonymous inner class the way I wanted, this is just to demonstrate what I meant.

1  
2  
3  
4  
public interface Weapon {
    public void update();
    public void draw(Graphics2D g2d);
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
public class WeaponObj implements Weapon {
    // some weapon vars, including name, image and other things not related to behavior
   // constructor initializing those vars
   @Override
    public void update() {
        // left blank, will be defined by anonymous inner class
   }
    @Override
    public void draw(Graphics2D g2d) {
        // same as above
   }
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
public enum WeaponType {
    FLAMETHROWER = new WeaponObj() {
        @Override
        public void update() {
            // update the particle vectors and positions, etc.
           // add new particles if weapon is held down
       }
        @Override
        public void draw(Graphics g2d) {
            // draw each particle in the particles list
       }
    };
    ROCKET_LAUNCHER = new WeaponObj() {
        @Override
        public void update() {
            // update positions of individual rockets fired, etc.
           // if a rocket has reached its destination or hit a target, 'explode', destroying the rocket and creating particles that shoot off in every direction to randomly chosen coordinates in a circle around the original target
       }
        @Override
        public void draw(Graphics g2d) {
            // draw each of the rockets
       }
    };
}


Now do you see what I mean by "different behavior"? That's not something you can do with simple parameters. Or rather, it would be unnecessarily complicated.
8  Game Development / Game Mechanics / Re: Possible to make an enum of anonymous inner classes? on: 2012-12-06 05:26:27
Because the behavior of the functions for each weapon would need to be different, hence, anonymous inner classes, where I can rewrite the functions for the weapon for each weapon.
9  Game Development / Game Mechanics / Re: Possible to make an enum of anonymous inner classes? on: 2012-12-05 22:27:42
Say I have an interface for a weapon, and I want to be able to use that to define any type of weapon, not just a simple one projectile weapon. For example, if I want to define behaviors for how the projectiles act, or even what kinds of projectiles the weapon fires. I want to have an enum of defined weapon types, such as flamethrower or machine gun or shotgun, so that when I select that weapon, the program will use that behavior for the weapon.

The only problem is I don't know how to create an enum that refers to these kinds of things.

Also, there's the problem that I need to be able to define the weapon's behavior very generally so I can just call this function when the weapon "fires" and it will perform the necessary functions to handle the weapon's projectiles and graphical behavior.
10  Game Development / Game Mechanics / Re: Possible to make an enum of anonymous inner classes? on: 2012-12-05 21:27:13
So can you show me an example of how I might attribute an ID to an anonymous inner class in an enumerator?
11  Game Development / Game Mechanics / Re: Possible to make an enum of anonymous inner classes? on: 2012-12-05 20:22:55
Scripting is a POSSIBLE feature for the future. Right now, I'm just adding a few different weapons that will be defined at compile time, so an enum is fine... I'm just asking if it's possible to do it the way I described, and if so, how?

If anyone has a link to a tutorial for scripting in the way I described, I'd be glad to read it.
12  Games Center / Showcase / Re: Hunger Games Board Game on: 2012-12-05 20:21:12
@Tj Doesn't really answer my question, but okay. I went to college and graduated last year, and even now I just started working on my first real game only a little over a month ago. Until now, it has just been non-practical practice problems and random programs for my portfolio.
13  Game Development / Game Mechanics / Re: Possible to make an enum of anonymous inner classes? on: 2012-12-05 20:16:54
Because I want something elegant that doesn't require writing a different class for each type of weapon. I know the alternative would be easier, but I want something customizable. Eventually, I might add the ability to script your own weapons if I can figure out how.
14  Games Center / Showcase / Re: Hunger Games Board Game on: 2012-12-05 20:13:09
Can I ask how you learned Java? To be able to learn Java from scratch and create something like this by the end of the year is pretty impressive...
15  Game Development / Game Mechanics / Possible to make an enum of anonymous inner classes? on: 2012-12-05 20:06:25
I'm working on a simple game in Java, and I've hit a bit of a roadblock. I don't know anything about scripting, such as using LUA, so I'm trying to think of a way to handle this. I need to add different weapons to my game, but as far as the weapon class goes, I need to make it flexible enough to handle any type of weapon... so what I was thinking of doing is to make a simple class with some general parameters, and then make an interface for it to implement that will have the functions that need to be specific.

From there, I was hoping I could create an enumerator containing anonymous inner classes. This way, I could create a separate file with mappings of IDs like "FLAMETHROWER" to an anonymous inner class where I could hard-code the effects that those weapons would display. Is this possible? If so, how could I do this?

If this isn't possible / a good idea, what would you recommend? I can provide a link to the project on Github if you need to see the code.
Pages: [1]
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (117 views)
2013-05-17 21:29:12

alaslipknot (124 views)
2013-05-16 21:24:48

gouessej (153 views)
2013-05-16 00:53:38

gouessej (146 views)
2013-05-16 00:17:58

theagentd (160 views)
2013-05-15 15:01:13

theagentd (145 views)
2013-05-15 15:00:54

StreetDoggy (189 views)
2013-05-14 15:56:26

kutucuk (213 views)
2013-05-12 17:10:36

kutucuk (212 views)
2013-05-12 15:36:09

UnluckyDevil (216 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.072 seconds with 21 queries.