Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
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  
  turn-based animation  (Read 1530 times)
0 Members and 1 Guest are viewing this topic.
Offline xproch13

Senior Newbie





« Posted 2012-07-26 00:04:14 »

Hi,
I am working on small turn-based game and now thinking about some easy way to animate objects. What I want to achive is to let for example player character to animate from one tile to other (so if I press RIGHT key, it will not only jump eg. 32 pixels to the right but slowly slide there with animation). Something like in dungeons of dremor but not so fancy of course:
http://www.youtube.com/watch?v=gIKH36J1wQQ

I know how to run animation but don't know how to fit it in the turn-based model. I do something like this in my hero class:
1  
2  
3  
4  
5  
public void act() {
   if (control.action == Actions.RIGHT) {
      hero1.setCurrentFrame(frame);  //hero1 is instance of Animation
      x=x+32;
      frame ^= 1;   // switch between two frames


Then render() and update() methods do just following:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public void render(GameContainer container, StateBasedGame game, Graphics g)
      throws SlickException {
     hero.hero1.draw(hero.x, hero.y);
     
         }

@Override
public void update(GameContainer container, StateBasedGame game, int delta)
      throws SlickException {
   hero.act();


I would normaly put this slide/animation code to update/render methods but it does not seem to be a good idea with my turn-based design (need to keep track if player's instance already played and/or can play again, etc.).
Is there some way to render animation outside the GameState's render() method? I tried to call hero1.draw(x,y) method in a loop in my hero's class but it did not draw anything at all.
Thank you...hope my question is clear.
Offline UprightPath
« Reply #1 - Posted 2012-07-26 00:09:22 »

Okay, it's pretty simple what you do.

Since your animation is going to happen only once your action has been determined, correct? Make it so that when you're shifting between units/characters/monsters what-ever, there is a timer that is set up between when an action is made and the next action is decided on. This can be as simple as setting an int to a value > 0, then decreasing that value on every frame until it equals 0 before another action can be taken.

Really, if you can't figure out a place to put your animation in your turn management system, then you probably need to rewrite it.

Offline xproch13

Senior Newbie





« Reply #2 - Posted 2012-07-26 18:04:04 »

Sorry, can you elaborate it a little bit (maybe with small code sample) as I do not get it. Maybe I am mising the basic point of update()/render()/draw() methods. I tried to add something like this to slicks update() method just for testing if the image will be at least somehow moved slowly but it did not work:
1  
2  
3  
4  
for (int i=10000; i>=0; i--) {
   hero.x += i*0.000001f;
   hero.hero1.draw(hero.x, hero.y); //does not draw anything actually
}

Loop updates hero.x slowly but does not draw anything during any of these 10000 steps. Image is just drawn in the render method after the whole loop finishes (so it is "jumping" and not "sliding to the next position").
Thanks
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Ultroman

JGO Knight


Medals: 24
Projects: 1


Snappin' at snizzes since '83


« Reply #3 - Posted 2012-07-26 18:38:52 »

Well, off the top of my head:
Make a Move-class, which can contain an animation, a start and end point (or a length to travel and a direction, depending on how you've built your engine) and perhaps even a timeToSpendMoving.

When you select a move, you fetch the appropriate animation, the current position and the desired position, and use them to create an instance of the Move-class. Hero can have a "Move currentMove" variable.

Then in your hero.update(deltaTime) method, if his currentMove != null, do the part of the movement for the deltaTime, update the animation, and when you call hero.draw(g), if he has a currentMove, you draw the current image in the animation, and if he doesn't have a currentMove, you draw the player image at his position.

- Jonas
Offline sproingie

JGO Kernel


Medals: 201



« Reply #4 - Posted 2012-07-26 19:24:54 »

If you're not married to slick2d, you could use the scene2d stuff in libgdx, where the API would look something like

hero.addAction(moveBy(dx,dy,ms));

And everything is taken care of for you, you just call stage.act(delta) on every render() call.  It even has several different easing effects for tweening, but I can't recall the API offhand, other than that it's dead simple.  Decent (and recent) documentation for scene2d is hard to come by, but the source is very easy to understand.
Offline sproingie

JGO Kernel


Medals: 201



« Reply #5 - Posted 2012-07-26 19:28:44 »

1  
2  
3  
4  
for (int i=10000; i>=0; i--) {
   hero.x += i*0.000001f;
   hero.hero1.draw(hero.x, hero.y); //does not draw anything actually
}


Assuming you're using a pixel-perfect projection, that whole loop will move the hero right by about 1/10 of a pixel.  Try a bigger multiplier.  Even then, you've got the draw in totally the wrong place, since that will draw 10000 copies of the hero overlaid on each other.  Do one movement per frame, that's it.  If calculating all this stuff is too much of a hassle, I really have to recommend again using scene2d from libgdx.




Offline MashedPotatoe

Senior Newbie





« Reply #6 - Posted 2012-07-29 23:06:43 »

I am actually working on a turn-based game like that and had similar problems at first.

The input is only setting up the target destination and gets "blocked" until the movement is complete and it's player's turn again.

1  
2  
3  
4  
5  
6  
7  
8  
if (playersTurn && !moving) {
  if (control.action == Actions.RIGHT) {
       targetX = x + TILESIZE; // set the destination
      currentAnimation = animationRight; // set the animation to the animation for moving to the right
      moving = true; // tell update it should be moving the character
 //some more input
 }
}


Now the update() moves x towards the targetX until the destination is reached. I use something like:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
public void update(GameContainer container, StateBasedGame game, int delta)
      throws SlickException {
  if (moving && x + delta * .1f < targetX) {
    x += delta * .1f;
    currentAnimation.update(delta);
  } else if (x + delta * .1f >= targetX) {
    x = targetX;
    currentAnimation = idleAnimation;
    moving = false;
  }
}


And the renderer draws as usual. Everything should've been set up in the update() and where ever you accept your input:

1  
2  
3  
4  
public void render(GameContainer container, StateBasedGame game, Graphics g)
      throws SlickException {
     currentAnimation.draw(x,y); // don't know the exact syntax, haven't used slick in a while
}


Now you have prevented the player from flying through the tiles and clipping the animations because he has to wait for each animation to finish and until it is his turn. The factor for delta should of course be adjusted to fit the length of your animation. What kind of flags you set up for blocking the input is up to you. The code as is will probably not work since I just wrote it and don't know the actual syntax of slick by heart but you should get the picture. I hope it is somewhat understandable and I could help. Good luck!
Offline xproch13

Senior Newbie





« Reply #7 - Posted 2012-07-31 15:54:52 »

Yes, this is actually the same principle I came to after few days of trying different approaches and hitting various dead ends Smiley Hope it will be solid as more and more features will be added in...
Thanks
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.

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

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

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

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

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

Riven (44 views)
2014-07-14 18:02:53

OpenGLShaders (33 views)
2014-07-14 16:23:47

Riven (34 views)
2014-07-14 11:51:35

quew8 (30 views)
2014-07-13 13:57:52

SHC (66 views)
2014-07-12 17:50:04
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!