view import/myrrdin/movingfigure.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
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 movingfigure;

import Integer = tango.text.convert.Integer;

import dsfml.window.all;
import dsfml.system.all;
import dsfml.graphics.all;

import spriteconsumer;
import animatedsprite;
import renderer;
import imagecache;

class MovingAnimatedFigureConsumer : SpriteConsumer {
    private Animation ani_left, ani_right, ani_up, ani_down;
    public AnimatedSprite sprite;
    private Input input;

    this(Renderer renderer, Animation go_left, Animation go_right, Animation go_up, Animation go_down) {
	super(renderer);
	this.ani_left = go_left;
	this.ani_right = go_right;
	this.ani_up = go_up;
	this.ani_down = go_down;
	this.sprite = new AnimatedSprite;
	this.add_sprite(sprite);
	this.sprite.setImage(this.ani_down.frames[0].image);
	this.input = this.renderer.app.getInput();
    }

    void draw() {
	super.draw();
    }

    void loop_iteration() {
	float x=0, y=0;
	if(this.input.isKeyDown(KeyCode.LEFT)) x -= 1;
	if(this.input.isKeyDown(KeyCode.RIGHT)) x += 1;
	if(this.input.isKeyDown(KeyCode.UP)) y -= 1;
	if(this.input.isKeyDown(KeyCode.DOWN)) y += 1;    
	if(x != 0 || y != 0) {
	    this.sprite.move(x, y);
	    if (!this.sprite.is_playing()) {
		if(x > 0) this.sprite.play_animation(this.ani_right);
		if(x < 0) this.sprite.play_animation(this.ani_left);
		if(y > 0) this.sprite.play_animation(this.ani_down);
		if(y < 0) this.sprite.play_animation(this.ani_up);
	    }
	}
    }
}

MovingAnimatedFigureConsumer load_charset(Renderer renderer, Cache cache, char[] prefix, char[] suffix, uint frame_duration=0) {
    Image get_f(int id) {
	return cache.get_image(prefix~Integer.toString(id)~suffix);
    }
    Animation up = new Animation;
    up.add_frame(get_f(1), frame_duration);
    up.add_frame(get_f(2), frame_duration);
    up.add_frame(get_f(3), frame_duration);
    Animation right = new Animation;
    right.add_frame(get_f(4), frame_duration);
    right.add_frame(get_f(5), frame_duration);
    right.add_frame(get_f(6), frame_duration);
    Animation down = new Animation;
    down.add_frame(get_f(7), frame_duration);
    down.add_frame(get_f(8), frame_duration);
    down.add_frame(get_f(9), frame_duration);
    Animation left = new Animation;
    left.add_frame(get_f(10), frame_duration);
    left.add_frame(get_f(11), frame_duration);
    left.add_frame(get_f(12), frame_duration);
    return new MovingAnimatedFigureConsumer(renderer, left, right, up, down);
}