Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (578)
games submitted by our members
Games in WIP (499)
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  
  Faking threads (scripting)  (Read 430 times)
0 Members and 1 Guest are viewing this topic.
Offline kitfox

Junior Member




Java games rock!


« Posted 2013-02-23 16:44:00 »

I'm writing a game and trying to think of a good way to handle complex scripted animation.

Right now the main game loop looks like:
1  
2  
3  
4  
5  
6  
7  
8  
9  
while (true)
{
   for (LevelObject obj: levelObjects)
   {
      obj.performGameLogic();
   }

drawLevel();
}

Right now performGameLogic() is hard coded per object and does something simple like cause the game object to slowly walk back and forth while updating the sprite animation. However, I'd like to do something more complex. It would be nice if I could write something like:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
class Monster extends GameObject
{
   public void performGameLogic()
   {
      takeStep(Direction.DOWN);
      takeStep(Direction.LEFT);
      if (chest5.isClosed())
      {
         chest5.openChest();
      }
      playAnimation("BeatChest");
   }
}


The trouble with this is that most of the above commands will require several hundred milliseconds to complete (and animate). They should also play sequentially - one after the other. However, the main thread needs to return almost immediately to perform the rest of the game logic and draw the level.

It would be better if I could somehow encapsulate this in a 'thread'. Each time performGameLogic() was called, it would advance this 'thread' as far as it could and then return. (Ie, the 'thread' would run until it blocks, waiting for time to have elapsed or some state to be set.) When performGameLogic() was called again, the 'thread' would pick up again from where it left off and run until it blocks again.

Using real threads to do this would be expensive. I was wondering if there might be some clever way to create this thread-like behavior with arbitrary code.

(At the moment, I have a solution where I create Animation objects and link them together. Each Animation has an advanceLogic() method that is called to advance its state and which will fire an event when it completes. A listener will then detect this event and schedule the next Animation object. While this works, it is tedious and unintuitive.)
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2013-02-23 17:12:11 »

Seems like you're looking for green threads.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
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.

xsi3rr4x (28 views)
2014-04-15 20:08:23

BurntPizza (25 views)
2014-04-15 05:46:01

UprightPath (40 views)
2014-04-14 19:39:50

UprightPath (22 views)
2014-04-14 19:35:47

Porlus (38 views)
2014-04-14 17:48:38

tom_mai78101 (62 views)
2014-04-10 06:04:31

BurntPizza (121 views)
2014-04-09 01:06:04

tom_mai78101 (221 views)
2014-04-05 15:34:39

trollwarrior1 (188 views)
2014-04-04 14:06:45

CJLetsGame (195 views)
2014-04-01 04:16:10
List of Learning Resources
by SHC
2014-04-18 05:17:39

List of Learning Resources
by Longarmx
2014-04-08 05:14:44

Good Examples
by matheus23
2014-04-05 15:51:37

Good Examples
by Grunnt
2014-04-03 17:48:46

Good Examples
by Grunnt
2014-04-03 17:48:37

Good Examples
by matheus23
2014-04-01 20:40:51

Good Examples
by matheus23
2014-04-01 20:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 17:22:30
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!