diff import/myrrdin/consumers/movingfigure.d @ 10:79b534bbda65

new director model
author fred@reichbier.de
date Sat, 19 Jul 2008 19:29:00 +0200
parents import/myrrdin/movingfigure.d@156a95e4c018
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/import/myrrdin/consumers/movingfigure.d	Sat Jul 19 19:29:00 2008 +0200
@@ -0,0 +1,98 @@
+/*
+    myrrdin, a 2d tile engine
+    Copyright (c) 2008 Friedrich Weber
+
+    Permission is hereby granted, free of charge, to any person obtaining a copy
+    of this software and associated documentation files (the Software), to deal
+    in the Software without restriction, including without limitation the rights
+    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+    copies of the Software, and to permit persons to whom the Software is
+    furnished to do so, subject to the following conditions:
+
+    The above copyright notice and this permission notice shall be included in
+    all copies or substantial portions of the Software.
+
+    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+    THE SOFTWARE.
+ */
+
+module myrrdin.movingfigure;
+
+import Integer = tango.text.convert.Integer;
+
+import dsfml.window.all;
+import dsfml.system.all;
+import dsfml.graphics.all;
+
+import myrrdin.consumers.sprite;
+import myrrdin.animatedsprite;
+import myrrdin.renderer;
+import myrrdin.imagecache;
+import myrrdin.director;
+
+class MovingAnimatedFigureConsumer : SpriteConsumer {
+    private Animation ani_left, ani_right, ani_up, ani_down;
+    public AnimatedSprite sprite;
+    private Input input;
+
+    this(Animation go_left, Animation go_right, Animation go_up, Animation go_down) {
+	super();
+	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(char[] prefix, char[] suffix, uint frame_duration=0) {
+    Image get_f(int id) {
+	return Director.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(left, right, up, down);
+}