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