annotate import/myrrdin/movingfigure.d @ 8:156a95e4c018

final package structure now, i hope
author fred@reichbier.de
date Sat, 19 Jul 2008 17:26:41 +0200
parents 806b3781f4e4
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;
5
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
36
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
37 class MovingAnimatedFigureConsumer : SpriteConsumer {
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
38 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
39 public AnimatedSprite sprite;
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
40 private Input input;
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
41
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
42 this(Renderer renderer, Animation go_left, Animation go_right, Animation go_up, Animation go_down) {
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
43 super(renderer);
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
44 this.ani_left = go_left;
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
45 this.ani_right = go_right;
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
46 this.ani_up = go_up;
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
47 this.ani_down = go_down;
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
48 this.sprite = new AnimatedSprite;
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
49 this.add_sprite(sprite);
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
50 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
51 this.input = this.renderer.app.getInput();
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
52 }
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 void draw() {
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
55 super.draw();
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
56 }
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 void loop_iteration() {
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
59 float x=0, y=0;
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
60 if(this.input.isKeyDown(KeyCode.LEFT)) x -= 1;
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
61 if(this.input.isKeyDown(KeyCode.RIGHT)) x += 1;
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
62 if(this.input.isKeyDown(KeyCode.UP)) y -= 1;
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
63 if(this.input.isKeyDown(KeyCode.DOWN)) y += 1;
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
64 if(x != 0 || y != 0) {
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
65 this.sprite.move(x, y);
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
66 if (!this.sprite.is_playing()) {
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
67 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
68 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
69 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
70 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
71 }
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 MovingAnimatedFigureConsumer load_charset(Renderer renderer, Cache cache, char[] prefix, char[] suffix, uint frame_duration=0) {
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
77 Image get_f(int id) {
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
78 return cache.get_image(prefix~Integer.toString(id)~suffix);
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
79 }
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
80 Animation up = new Animation;
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
81 up.add_frame(get_f(1), frame_duration);
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
82 up.add_frame(get_f(2), frame_duration);
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
83 up.add_frame(get_f(3), frame_duration);
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
84 Animation right = new Animation;
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
85 right.add_frame(get_f(4), frame_duration);
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
86 right.add_frame(get_f(5), frame_duration);
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
87 right.add_frame(get_f(6), frame_duration);
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
88 Animation down = new Animation;
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
89 down.add_frame(get_f(7), frame_duration);
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
90 down.add_frame(get_f(8), frame_duration);
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
91 down.add_frame(get_f(9), frame_duration);
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
92 Animation left = new Animation;
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
93 left.add_frame(get_f(10), frame_duration);
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
94 left.add_frame(get_f(11), frame_duration);
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
95 left.add_frame(get_f(12), frame_duration);
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
96 return new MovingAnimatedFigureConsumer(renderer, left, right, up, down);
f4b89014ad39 added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
diff changeset
97 }