Mercurial > projects > myrrdin
comparison 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 |
comparison
equal
deleted
inserted
replaced
4:292df259cc85 | 5:f4b89014ad39 |
---|---|
1 /* | |
2 This file is part of myrrdin. | |
3 | |
4 myrrdin is free software: you can redistribute it and/or modify | |
5 it under the terms of the GNU Lesser General Public License as | |
6 published by the Free Software Foundation, either version 3 of | |
7 the License, or (at your option) any later version. | |
8 | |
9 myrrdin is distributed in the hope that it will be useful, | |
10 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 GNU Lesser General Public License for more details. | |
13 | |
14 You should have received a copy of the GNU Lesser General Public | |
15 License along with myrrdin. If not, see <http://www.gnu.org/licenses/>. | |
16 */ | |
17 | |
18 module movingfigure; | |
19 | |
20 import Integer = tango.text.convert.Integer; | |
21 | |
22 import dsfml.window.all; | |
23 import dsfml.system.all; | |
24 import dsfml.graphics.all; | |
25 | |
26 import spriteconsumer; | |
27 import animatedsprite; | |
28 import renderer; | |
29 import imagecache; | |
30 | |
31 class MovingAnimatedFigureConsumer : SpriteConsumer { | |
32 private Animation ani_left, ani_right, ani_up, ani_down; | |
33 public AnimatedSprite sprite; | |
34 private Input input; | |
35 | |
36 this(Renderer renderer, Animation go_left, Animation go_right, Animation go_up, Animation go_down) { | |
37 super(renderer); | |
38 this.ani_left = go_left; | |
39 this.ani_right = go_right; | |
40 this.ani_up = go_up; | |
41 this.ani_down = go_down; | |
42 this.sprite = new AnimatedSprite; | |
43 this.add_sprite(sprite); | |
44 this.sprite.setImage(this.ani_down.frames[0].image); | |
45 this.input = this.renderer.app.getInput(); | |
46 } | |
47 | |
48 void draw() { | |
49 super.draw(); | |
50 } | |
51 | |
52 void loop_iteration() { | |
53 float x=0, y=0; | |
54 if(this.input.isKeyDown(KeyCode.LEFT)) x -= 1; | |
55 if(this.input.isKeyDown(KeyCode.RIGHT)) x += 1; | |
56 if(this.input.isKeyDown(KeyCode.UP)) y -= 1; | |
57 if(this.input.isKeyDown(KeyCode.DOWN)) y += 1; | |
58 if(x != 0 || y != 0) { | |
59 this.sprite.move(x, y); | |
60 if (!this.sprite.is_playing()) { | |
61 if(x > 0) this.sprite.play_animation(this.ani_right); | |
62 if(x < 0) this.sprite.play_animation(this.ani_left); | |
63 if(y > 0) this.sprite.play_animation(this.ani_down); | |
64 if(y < 0) this.sprite.play_animation(this.ani_up); | |
65 } | |
66 } | |
67 } | |
68 } | |
69 | |
70 MovingAnimatedFigureConsumer load_charset(Renderer renderer, Cache cache, char[] prefix, char[] suffix, uint frame_duration=0) { | |
71 Image get_f(int id) { | |
72 return cache.get_image(prefix~Integer.toString(id)~suffix); | |
73 } | |
74 Animation up = new Animation; | |
75 up.add_frame(get_f(1), frame_duration); | |
76 up.add_frame(get_f(2), frame_duration); | |
77 up.add_frame(get_f(3), frame_duration); | |
78 Animation right = new Animation; | |
79 right.add_frame(get_f(4), frame_duration); | |
80 right.add_frame(get_f(5), frame_duration); | |
81 right.add_frame(get_f(6), frame_duration); | |
82 Animation down = new Animation; | |
83 down.add_frame(get_f(7), frame_duration); | |
84 down.add_frame(get_f(8), frame_duration); | |
85 down.add_frame(get_f(9), frame_duration); | |
86 Animation left = new Animation; | |
87 left.add_frame(get_f(10), frame_duration); | |
88 left.add_frame(get_f(11), frame_duration); | |
89 left.add_frame(get_f(12), frame_duration); | |
90 return new MovingAnimatedFigureConsumer(renderer, left, right, up, down); | |
91 } |