Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  a question of code design  (Read 3101 times)
0 Members and 1 Guest are viewing this topic.
Offline Serethos

Junior Member




Java games rock!


« Posted 2003-10-26 20:16:12 »

many times i stumble over a problem, which concerns the design of my games.

imaging the following situation:

a game-object, a spaceship (represented by a real java class/object) has e.g. an animation-object for getting the current frame. this animation object should be nothing more than a container for the frames and a counter, which one should be choosed next.

now the anim. can realize a looped animation, a single, a random and so on. so every time something special happens (pressing a key, ship is exploding) the ship sends a signal to the anim. to change the frameset and perhaps the kind of animating.

so far, so beatiful. now my problem:

many times the changing-relationship is reversed:
if the anim. shall animate x-frames and then change to the old frameset, the change depends on the animation, not on the ship.
so there are generally two way of getting the timepoint of changing:

the ship asks every frame, if the animation has expired. this is very elegant an not a good performance choice. but the preoblem is, that the ship-object has access to the animation, but the animation has no to the ship.

so i could give every little tool-object like the animation a reference to its "owner"-object (or perhaps implement the observer pattern). so everyone could send actions to everyone.

this would solve my problem, but i think that this would be terrible coding style.



Online princec

JGO Kernel


Medals: 342
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #1 - Posted 2003-10-27 09:10:22 »

In SPGL, my Sprites are Animated, and have an Animation. The Animation is executed one tick at a time, following a sequence of Commands. Some Commands end the frame (ie. setting a new Image for the Sprite).

One of those Commands is an EventCommand, which sets a special general purpose integer to an arbitrary value.

For example, here's the Roving Eye from Alien Flux:
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  
      <animation
            name="roving_eye_shoot.animation"
      >
            <frame i="spriteimage.roving_eye0010" d="2" />
            <frame i="spriteimage.roving_eye0011" d="2" />
            <frame i="spriteimage.roving_eye0012" d="2" />
            <frame i="spriteimage.roving_eye0013" d="10" />
            <event id="1" />
            <frame i="spriteimage.roving_eye0014" d="1" />
            <frame i="spriteimage.roving_eye0015" d="2" />
            <frame i="spriteimage.roving_eye0016" d="2" />
            <frame i="spriteimage.roving_eye0017" d="7" />
            <event id="1" />
            <frame i="spriteimage.roving_eye0014" d="1" />
            <frame i="spriteimage.roving_eye0015" d="2" />
            <frame i="spriteimage.roving_eye0016" d="2" />
            <frame i="spriteimage.roving_eye0017" d="7" />
            <event id="1" />
            <frame i="spriteimage.roving_eye0014" d="1" />
            <frame i="spriteimage.roving_eye0015" d="2" />
            <frame i="spriteimage.roving_eye0018" d="2" />
            <frame i="spriteimage.roving_eye0019" d="2" />
            <frame i="spriteimage.roving_eye0020" d="2" />
            <frame i="spriteimage.roving_eye0021" d="2" />
            <event id="2" />            
      </animation>


The roving eye, once it has called sprite.setAnimation(shootAnimation), polls its event and takes appropriate action. Here's a snippet of the roving eye tick code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
                              switch (sprite.getEvent()) {
                                    case 0:
                                          // Wait
                                         break;
                                    case 1:
                                          // Fire
                                         fire();
                                          sprite.setEvent(0);
                                          break;
                                    case 2:
                                          // Move
                                         phase = PHASE_WOBBLING;
                                          sprite.setEvent(0);
                                          selectNewTarget();
                                          sprite.setAnimation(wobbleAnimationResource);
                                          shadowSprite.setAnimation(wobbleShadowAnimationResource);
                                          break;
                                    default:
                                          assert false;
                              }                              


Cas Smiley

Online princec

JGO Kernel


Medals: 342
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #2 - Posted 2003-10-27 09:17:34 »

It might be worth noting that the Roving Eye is not a Sprite itself, it allocates and controls Sprites; and that Animated things, such as Sprites, contain all the necessary state to allow Animations to be stateless sequences (ie. completely reusable and re-entrant).

When you make a gidrah separate from Sprites you can do much more clever things with it. The Alien Brain on Level 20 for example is composed of no less than 12 separate sprites - brain, jar, two eyes and eight jets, and each jet has a smoke emitter too.

Cas Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Herkules

Senior Member




Friendly fire isn't friendly!


« Reply #3 - Posted 2003-10-27 10:34:27 »

Or think of making your animation a composite.
So you have certain animation class that perform a temporary 'special' animation and than falls back to a default one w/o notifying the ship. Something like that....

HARDCODE    --     DRTS/FlyingGuns/JPilot/JXInput  --    skype me: joerg.plewe
Offline Serethos

Junior Member




Java games rock!


« Reply #4 - Posted 2003-10-27 11:38:37 »

the composite idead is very nice and simple.

now that were on topic i would like to discuss one of my mentioned "solution", the one where every object gets a reference of its owner.
in another forum some people mentioned that i should not waste any thoughts on using additional references (< 1000)
perhaps they are right in the question of memory usage but i think it is very important to defend the object-encapsulation.
(?!)
Offline Serethos

Junior Member




Java games rock!


« Reply #5 - Posted 2003-10-29 06:00:39 »

i would like to discuss another thing: the observer/ observable pattern.
im interested in the knowledge how it works. does the observerable do nothing more than passing a hidden reference to the obeserver ?
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #6 - Posted 2003-10-29 16:23:32 »

I think it's the other way around. A reference to the observer is passed to the observable. The observable keeps a list and and broadcasts stuff to observers.  This is essentially what Listeners are in the Java UI classes.

http://c2.com/cgi/wiki?ObserverPattern

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

pw (26 views)
2014-07-24 01:59:36

Riven (25 views)
2014-07-23 21:16:32

Riven (19 views)
2014-07-23 21:07:15

Riven (22 views)
2014-07-23 20:56:16

ctomni231 (51 views)
2014-07-18 06:55:21

Zero Volt (46 views)
2014-07-17 23:47:54

danieldean (37 views)
2014-07-17 23:41:23

MustardPeter (40 views)
2014-07-16 23:30:00

Cero (56 views)
2014-07-16 00:42:17

Riven (55 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!