comparison 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
comparison
equal deleted inserted replaced
9:adc5e1eedb8a 10:79b534bbda65
1 /*
2 myrrdin, a 2d tile engine
3 Copyright (c) 2008 Friedrich Weber
4
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the Software), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 THE SOFTWARE.
22 */
23
24 module myrrdin.movingfigure;
25
26 import Integer = tango.text.convert.Integer;
27
28 import dsfml.window.all;
29 import dsfml.system.all;
30 import dsfml.graphics.all;
31
32 import myrrdin.consumers.sprite;
33 import myrrdin.animatedsprite;
34 import myrrdin.renderer;
35 import myrrdin.imagecache;
36 import myrrdin.director;
37
38 class MovingAnimatedFigureConsumer : SpriteConsumer {
39 private Animation ani_left, ani_right, ani_up, ani_down;
40 public AnimatedSprite sprite;
41 private Input input;
42
43 this(Animation go_left, Animation go_right, Animation go_up, Animation go_down) {
44 super();
45 this.ani_left = go_left;
46 this.ani_right = go_right;
47 this.ani_up = go_up;
48 this.ani_down = go_down;
49 this.sprite = new AnimatedSprite;
50 this.add_sprite(sprite);
51 this.sprite.setImage(this.ani_down.frames[0].image);
52 this.input = this.renderer.app.getInput();
53 }
54
55 void draw() {
56 super.draw();
57 }
58
59 void loop_iteration() {
60 float x=0, y=0;
61 if(this.input.isKeyDown(KeyCode.LEFT)) x -= 1;
62 if(this.input.isKeyDown(KeyCode.RIGHT)) x += 1;
63 if(this.input.isKeyDown(KeyCode.UP)) y -= 1;
64 if(this.input.isKeyDown(KeyCode.DOWN)) y += 1;
65 if(x != 0 || y != 0) {
66 this.sprite.move(x, y);
67 if (!this.sprite.is_playing()) {
68 if(x > 0) this.sprite.play_animation(this.ani_right);
69 if(x < 0) this.sprite.play_animation(this.ani_left);
70 if(y > 0) this.sprite.play_animation(this.ani_down);
71 if(y < 0) this.sprite.play_animation(this.ani_up);
72 }
73 }
74 }
75 }
76
77 MovingAnimatedFigureConsumer load_charset(char[] prefix, char[] suffix, uint frame_duration=0) {
78 Image get_f(int id) {
79 return Director.cache.get_image(prefix~Integer.toString(id)~suffix);
80 }
81 Animation up = new Animation;
82 up.add_frame(get_f(1), frame_duration);
83 up.add_frame(get_f(2), frame_duration);
84 up.add_frame(get_f(3), frame_duration);
85 Animation right = new Animation;
86 right.add_frame(get_f(4), frame_duration);
87 right.add_frame(get_f(5), frame_duration);
88 right.add_frame(get_f(6), frame_duration);
89 Animation down = new Animation;
90 down.add_frame(get_f(7), frame_duration);
91 down.add_frame(get_f(8), frame_duration);
92 down.add_frame(get_f(9), frame_duration);
93 Animation left = new Animation;
94 left.add_frame(get_f(10), frame_duration);
95 left.add_frame(get_f(11), frame_duration);
96 left.add_frame(get_f(12), frame_duration);
97 return new MovingAnimatedFigureConsumer(left, right, up, down);
98 }