Mercurial > projects > myrrdin
view import/myrrdin/animatedsprite.d @ 5:f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
author | fred@reichbier.de |
---|---|
date | Sat, 19 Jul 2008 14:33:08 +0200 |
parents | src/animatedsprite.d@292df259cc85 |
children | 510541745cd1 |
line wrap: on
line source
/* This file is part of myrrdin. myrrdin is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. myrrdin is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with myrrdin. If not, see <http://www.gnu.org/licenses/>. */ module animatedsprite; import dsfml.window.all; import dsfml.system.all; import dsfml.graphics.all; class Frame { Image image; int length; // length in frames this(Image image, int length) { this.image = image; this.length = length; } } class Animation { public Frame[] frames; private int current_frame_idx; private Frame current_frame; private int frame_counter; this() { } /* add a frame */ void add_frame(Image image, int length) { this.frames ~= new Frame(image, length); } void add_frame(Frame frame) { this.frames ~= frame; } /* internal. you do not have to call it */ void play() { this.current_frame = this.frames[0]; this.current_frame_idx = 0; } /* return the current image or null if the animation ended */ Image get_image() { if(this.frame_counter >= this.current_frame.length) { this.frame_counter = 0; this.current_frame_idx++; if (this.frames.length <= this.current_frame_idx) { // animation stop return null; } this.current_frame = this.frames[this.current_frame_idx]; } this.frame_counter++; return this.current_frame.image; } } class AnimatedSprite : Sprite { private Animation current_animation = null; private bool is_loop = false; /* play an animation now. */ void play_animation(Animation animation, bool loop=false) { this.current_animation = animation; this.is_loop = loop; this.current_animation.play(); } bool is_playing() { return (this.current_animation !is null); } /* update everything. Perfect for an animated sprite */ void update() { if(this.current_animation) { Image img = this.current_animation.get_image(); if(img) { this.setImage(img); } else if (this.is_loop) { this.current_animation.play(); // is a loop, play again } else { this.current_animation = null; } } } }