- File Changes
- .gitignore
- Cargo.toml
- assets/bardo.png
- assets/darkdimension.png
- assets/reaper.png
- src/main.rs
- src/components.rs
- assets
-
src
- components.rs
- main.rs
- .gitignore
- Cargo.toml
+44
-0
src/components.rs
| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
|
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
+4
-42
src/main.rs
| 1 |
|
|
| 2 |
|
|
| 1 | 3 |
|
| 2 | 4 |
|
| 3 | 5 |
|
| 4 | 6 |
|
| 5 | 7 |
|
| 6 | 8 |
|
| 7 | 9 |
|
| 8 | 10 |
|
| 9 | 11 |
|
| 10 |
|
|
| 11 | 12 |
|
| 12 | 13 |
|
| 13 | 14 |
|
| 15 |
|
|
| 16 |
|
|
| 14 | 17 |
|
| 15 | 18 |
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
|
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 | 19 |
|
| 58 | 20 |
|
| 59 | 21 |
|
| 60 | 22 |
|
| 61 | 23 |
|
| 62 | 24 |
|
| 63 | 25 |
|
| 64 | 26 |
|
| 65 | 27 |
|
| 66 | 28 |
|
| 67 | 29 |
|
| 68 | 30 |
|
| 69 | 31 |
|
| 70 | 32 |
|
| 71 | 33 |
|
| 72 | 34 |
|
| 73 | 35 |
|
| 74 | 36 |
|
| 75 | 37 |
|
| 76 | 38 |
|
| 77 | 39 |
|
| 78 | 40 |
|
| 79 | 41 |
|
| 80 | 42 |
|
| 81 | 43 |
|
| 82 | 44 |
|
| 83 | 45 |
|
| 84 | 46 |
|
| 85 | 47 |
|
| 86 | 48 |
|
| 87 | 49 |
|
| 88 | 50 |
|
| 89 | 51 |
|
| 90 | 52 |
|
| 91 | 53 |
|
| 92 | 54 |
|
| 93 | 55 |
|
| 94 | 56 |
|
| 95 | 57 |
|
| 96 | 58 |
|
| 97 | 59 |
|
| 98 | 60 |
|
| 99 | 61 |
|
| 100 | 62 |
|
| 101 | 63 |
|
| 102 | 64 |
|
| 103 | 65 |
|
| 104 | 66 |
|
| 105 | 67 |
|
| 106 | 68 |
|
| 107 | 69 |
|
| 108 | 70 |
|
| 109 | 71 |
|
| 110 | 72 |
|
| 111 | 73 |
|
| 112 | 74 |
|
| 113 | 75 |
|
| 114 | 76 |
|
| 115 | 77 |
|
| 116 | 78 |
|
| 117 | 79 |
|
| 118 | 80 |
|
| 119 | 81 |
|
| 120 | 82 |
|
| 121 | 83 |
|
| 122 | 84 |
|
| 123 | 85 |
|
| 124 | 86 |
|
| 125 | 87 |
|
| 126 | 88 |
|
| 127 | 89 |
|
| 128 | 90 |
|
| 129 | 91 |
|
| 130 | 92 |
|
| 131 | 93 |
|
| 132 | 94 |
|
| 133 | 95 |
|
| 134 | 96 |
|
| 135 | 97 |
|
| 136 | 98 |
|
| 137 | 99 |
|
| 138 | 100 |
|
| 139 | 101 |
|
| 140 | 102 |
|
| 141 | 103 |
|
| 142 | 104 |
|
| 143 | 105 |
|
| 144 | 106 |
|
| 145 | 107 |
|
| 146 | 108 |
|
| 147 | 109 |
|
| 148 | 110 |
|
| 149 | 111 |
|
| 150 | 112 |
|
| 151 | 113 |
|
| 152 | 114 |
|
| 153 | 115 |
|
| 154 | 116 |
|
| 155 | 117 |
|
| 156 | 118 |
|
| 157 | 119 |
|
| 158 | 120 |
|
| 159 | 121 |
|
| 160 | 122 |
|
| 161 | 123 |
|
| 162 | 124 |
|
| 163 | 125 |
|
| 164 | 126 |
|
| 165 | 127 |
|
| 166 | 128 |
|
| 167 | 129 |
|
| 168 | 130 |
|
| 169 | 131 |
|
| 170 | 132 |
|
| 171 | 133 |
|
| 172 | 134 |
|
| 173 | 135 |
|
| 174 | 136 |
|
| 175 | 137 |
|
| 176 | 138 |
|
| 177 | 139 |
|
| 178 | 140 |
|
| 179 | 141 |
|
| 180 | 142 |
|
| 181 | 143 |
|
| 182 | 144 |
|
| 183 | 145 |
|
| 184 | 146 |
|
| 185 | 147 |
|
| 186 | 148 |
|
| 187 | 149 |
|
| 188 | 150 |
|
| 189 | 151 |
|
| 190 | 152 |
|
| 191 | 153 |
|
| 192 | 154 |
|
| 193 | 155 |
|
| 194 | 156 |
|
| 195 | 157 |
|
| 196 | 158 |
|
| 197 | 159 |
|
| 198 | 160 |
|
| 199 | 161 |
|
| 200 | 162 |
|
| 201 | 163 |
|
| 202 | 164 |
|
| 203 | 165 |
|
| 204 | 166 |
|
| 205 | 167 |
|
| 206 | 168 |
|
| 207 | 169 |
|
| 208 | 170 |
|
| 209 | 171 |
|
| 210 | 172 |
|
| 211 | 173 |
|
| 212 | 174 |
|
| 213 | 175 |
|
| 214 | 176 |
|
| 215 | 177 |
|
| 216 | 178 |
|
| 217 | 179 |
|
| 218 | 180 |
|
| 219 | 181 |
|
| 220 | 182 |
|
| 221 | 183 |
|
| 222 | 184 |
|
| 223 | 185 |
|
| 224 | 186 |
|
| 225 | 187 |
|
| 226 | 188 |
|
| 227 | 189 |
|
| 228 | 190 |
|
| 229 | 191 |
|
| 230 | 192 |
|
| 231 | 193 |
|
| 232 | 194 |
|
| 233 | 195 |
|
| 234 | 196 |
|
| 235 | 197 |
|
| 236 | 198 |
|
| 237 | 199 |
|
| 238 | 200 |
|
| 239 | 201 |
|
+44
-0
src/components.rs
| 1 |
|
||
| 2 |
|
||
| 3 |
|
||
| 4 |
|
||
| 5 |
|
||
| 6 |
|
||
| 7 |
|
||
| 8 |
|
||
| 9 |
|
||
| 10 |
|
||
| 11 |
|
||
| 12 |
|
||
| 13 |
|
||
| 14 |
|
||
| 15 |
|
||
| 16 |
|
||
| 17 |
|
||
| 18 |
|
||
| 19 |
|
||
| 20 |
|
||
| 21 |
|
||
| 22 |
|
||
| 23 |
|
||
| 24 |
|
||
| 25 |
|
||
| 26 |
|
||
| 27 |
|
||
| 28 |
|
||
| 29 |
|
||
| 30 |
|
||
| 31 |
|
||
| 32 |
|
||
| 33 |
|
||
| 34 |
|
||
| 35 |
|
||
| 36 |
|
||
| 37 |
|
||
| 38 |
|
||
| 39 |
|
||
| 40 |
|
||
| 41 |
|
||
| 42 |
|
||
| 43 |
|
||
| 44 |
|
+4
-42
src/main.rs
| 1 |
|
||
| 2 |
|
||
| 1 |
|
3 |
|
| 2 |
|
4 |
|
| 3 |
|
5 |
|
| 4 |
|
6 |
|
| 5 |
|
7 |
|
| 6 |
|
8 |
|
| 7 |
|
9 |
|
| 8 |
|
10 |
|
| 9 |
|
11 |
|
| 10 |
|
||
| 11 |
|
12 |
|
| 12 |
|
13 |
|
| 13 |
|
14 |
|
| 15 |
|
||
| 16 |
|
||
| 14 |
|
17 |
|
| 15 |
|
18 |
|
| 16 |
|
||
| 17 |
|
||
| 18 |
|
||
| 19 |
|
||
| 20 |
|
||
| 21 |
|
||
| 22 |
|
||
| 23 |
|
||
| 24 |
|
||
| 25 |
|
||
| 26 |
|
||
| 27 |
|
||
| 28 |
|
||
| 29 |
|
||
| 30 |
|
||
| 31 |
|
||
| 32 |
|
||
| 33 |
|
||
| 34 |
|
||
| 35 |
|
||
| 36 |
|
||
| 37 |
|
||
| 38 |
|
||
| 39 |
|
||
| 40 |
|
||
| 41 |
|
||
| 42 |
|
||
| 43 |
|
||
| 44 |
|
||
| 45 |
|
||
| 46 |
|
||
| 47 |
|
||
| 48 |
|
||
| 49 |
|
||
| 50 |
|
||
| 51 |
|
||
| 52 |
|
||
| 53 |
|
||
| 54 |
|
||
| 55 |
|
||
| 56 |
|
||
| 57 |
|
19 |
|
| 58 |
|
20 |
|
| 59 |
|
21 |
|
| 60 |
|
22 |
|
| 61 |
|
23 |
|
| 62 |
|
24 |
|
| 63 |
|
25 |
|
| 64 |
|
26 |
|
| 65 |
|
27 |
|
| 66 |
|
28 |
|
| 67 |
|
29 |
|
| 68 |
|
30 |
|
| 69 |
|
31 |
|
| 70 |
|
32 |
|
| 71 |
|
33 |
|
| 72 |
|
34 |
|
| 73 |
|
35 |
|
| 74 |
|
36 |
|
| 75 |
|
37 |
|
| 76 |
|
38 |
|
| 77 |
|
39 |
|
| 78 |
|
40 |
|
| 79 |
|
41 |
|
| 80 |
|
42 |
|
| 81 |
|
43 |
|
| 82 |
|
44 |
|
| 83 |
|
45 |
|
| 84 |
|
46 |
|
| 85 |
|
47 |
|
| 86 |
|
48 |
|
| 87 |
|
49 |
|
| 88 |
|
50 |
|
| 89 |
|
51 |
|
| 90 |
|
52 |
|
| 91 |
|
53 |
|
| 92 |
|
54 |
|
| 93 |
|
55 |
|
| 94 |
|
56 |
|
| 95 |
|
57 |
|
| 96 |
|
58 |
|
| 97 |
|
59 |
|
| 98 |
|
60 |
|
| 99 |
|
61 |
|
| 100 |
|
62 |
|
| 101 |
|
63 |
|
| 102 |
|
64 |
|
| 103 |
|
65 |
|
| 104 |
|
66 |
|
| 105 |
|
67 |
|
| 106 |
|
68 |
|
| 107 |
|
69 |
|
| 108 |
|
70 |
|
| 109 |
|
71 |
|
| 110 |
|
72 |
|
| 111 |
|
73 |
|
| 112 |
|
74 |
|
| 113 |
|
75 |
|
| 114 |
|
76 |
|
| 115 |
|
77 |
|
| 116 |
|
78 |
|
| 117 |
|
79 |
|
| 118 |
|
80 |
|
| 119 |
|
81 |
|
| 120 |
|
82 |
|
| 121 |
|
83 |
|
| 122 |
|
84 |
|
| 123 |
|
85 |
|
| 124 |
|
86 |
|
| 125 |
|
87 |
|
| 126 |
|
88 |
|
| 127 |
|
89 |
|
| 128 |
|
90 |
|
| 129 |
|
91 |
|
| 130 |
|
92 |
|
| 131 |
|
93 |
|
| 132 |
|
94 |
|
| 133 |
|
95 |
|
| 134 |
|
96 |
|
| 135 |
|
97 |
|
| 136 |
|
98 |
|
| 137 |
|
99 |
|
| 138 |
|
100 |
|
| 139 |
|
101 |
|
| 140 |
|
102 |
|
| 141 |
|
103 |
|
| 142 |
|
104 |
|
| 143 |
|
105 |
|
| 144 |
|
106 |
|
| 145 |
|
107 |
|
| 146 |
|
108 |
|
| 147 |
|
109 |
|
| 148 |
|
110 |
|
| 149 |
|
111 |
|
| 150 |
|
112 |
|
| 151 |
|
113 |
|
| 152 |
|
114 |
|
| 153 |
|
115 |
|
| 154 |
|
116 |
|
| 155 |
|
117 |
|
| 156 |
|
118 |
|
| 157 |
|
119 |
|
| 158 |
|
120 |
|
| 159 |
|
121 |
|
| 160 |
|
122 |
|
| 161 |
|
123 |
|
| 162 |
|
124 |
|
| 163 |
|
125 |
|
| 164 |
|
126 |
|
| 165 |
|
127 |
|
| 166 |
|
128 |
|
| 167 |
|
129 |
|
| 168 |
|
130 |
|
| 169 |
|
131 |
|
| 170 |
|
132 |
|
| 171 |
|
133 |
|
| 172 |
|
134 |
|
| 173 |
|
135 |
|
| 174 |
|
136 |
|
| 175 |
|
137 |
|
| 176 |
|
138 |
|
| 177 |
|
139 |
|
| 178 |
|
140 |
|
| 179 |
|
141 |
|
| 180 |
|
142 |
|
| 181 |
|
143 |
|
| 182 |
|
144 |
|
| 183 |
|
145 |
|
| 184 |
|
146 |
|
| 185 |
|
147 |
|
| 186 |
|
148 |
|
| 187 |
|
149 |
|
| 188 |
|
150 |
|
| 189 |
|
151 |
|
| 190 |
|
152 |
|
| 191 |
|
153 |
|
| 192 |
|
154 |
|
| 193 |
|
155 |
|
| 194 |
|
156 |
|
| 195 |
|
157 |
|
| 196 |
|
158 |
|
| 197 |
|
159 |
|
| 198 |
|
160 |
|
| 199 |
|
161 |
|
| 200 |
|
162 |
|
| 201 |
|
163 |
|
| 202 |
|
164 |
|
| 203 |
|
165 |
|
| 204 |
|
166 |
|
| 205 |
|
167 |
|
| 206 |
|
168 |
|
| 207 |
|
169 |
|
| 208 |
|
170 |
|
| 209 |
|
171 |
|
| 210 |
|
172 |
|
| 211 |
|
173 |
|
| 212 |
|
174 |
|
| 213 |
|
175 |
|
| 214 |
|
176 |
|
| 215 |
|
177 |
|
| 216 |
|
178 |
|
| 217 |
|
179 |
|
| 218 |
|
180 |
|
| 219 |
|
181 |
|
| 220 |
|
182 |
|
| 221 |
|
183 |
|
| 222 |
|
184 |
|
| 223 |
|
185 |
|
| 224 |
|
186 |
|
| 225 |
|
187 |
|
| 226 |
|
188 |
|
| 227 |
|
189 |
|
| 228 |
|
190 |
|
| 229 |
|
191 |
|
| 230 |
|
192 |
|
| 231 |
|
193 |
|
| 232 |
|
194 |
|
| 233 |
|
195 |
|
| 234 |
|
196 |
|
| 235 |
|
197 |
|
| 236 |
|
198 |
|
| 237 |
|
199 |
|
| 238 |
|
200 |
|
| 239 |
|
201 |
|
/target**/*.rs.bk
[package]name = "game-tutorial"version = "0.1.0"authors = ["Sunjay Varma <[email protected]>"]edition = "2018"[dependencies]specs = "0.14"specs-derive = "0.4"[dependencies.sdl2]version = "0.32.1"default-features = falsefeatures = ["image"]
use specs::prelude::*;use specs_derive::Component;use sdl2::rect::{Point, Rect};#[derive(Debug, Clone, Copy, PartialEq, Eq)]pub enum Direction {Up,Down,Left,Right,}/// The current position of a given entity#[derive(Component, Debug)]#[storage(VecStorage)]pub struct Position(pub Point);/// The current speed and direction of a given entity#[derive(Component, Debug)]#[storage(VecStorage)]pub struct Velocity {pub speed: i32,pub direction: Direction,}#[derive(Component, Debug, Clone)]#[storage(VecStorage)]pub struct Sprite {/// The specific spritesheet to render frompub spritesheet: usize,/// The current region of the spritesheet to be renderedpub region: Rect,}#[derive(Component, Debug)]#[storage(VecStorage)]pub struct MovementAnimation {// The current frame in the animation of the direction this entity is moving inpub current_frame: usize,pub up_frames: Vec<Sprite>,pub down_frames: Vec<Sprite>,pub left_frames: Vec<Sprite>,pub right_frames: Vec<Sprite>,}
mod components;use sdl2::pixels::Color;use sdl2::event::Event;use sdl2::keyboard::Keycode;use sdl2::render::{WindowCanvas, Texture};use sdl2::rect::{Point, Rect};// "self" imports the "image" module itself as well as everything else we listeduse sdl2::image::{self, LoadTexture, InitFlag};use specs::prelude::*;use std::time::Duration;use crate::components::*;const PLAYER_MOVEMENT_SPEED: i32 = 20;/// Returns the row of the spritesheet corresponding to the given directionfn direction_spritesheet_row(direction: Direction) -> i32 {use self::Direction::*;match direction {Up => 3,Down => 0,Left => 1,Right => 2,}}/// Create animation frames for the standard character spritesheetfn character_animation_frames(spritesheet: usize, top_left_frame: Rect, direction: Direction) -> Vec<Sprite> {// All assumptions about the spritesheets are now encapsulated in this function instead of in// the design of our entire system. We can always replace this function, but replacing the// entire system is harder.let (frame_width, frame_height) = top_left_frame.size();let y_offset = top_left_frame.y() + frame_height as i32 * direction_spritesheet_row(direction);let mut frames = Vec::new();for i in 0..3 {frames.push(Sprite {spritesheet,region: Rect::new(top_left_frame.x() + frame_width as i32 * i,y_offset,frame_width,frame_height,),})}frames}fn render(canvas: &mut WindowCanvas,color: Color,texture: &Texture,player: &Player,) -> Result<(), String> {canvas.set_draw_color(color);canvas.clear();let (width, height) = canvas.output_size()?;let (frame_width, frame_height) = player.sprite.size();let current_frame = Rect::new(player.sprite.x() + frame_width as i32 * player.current_frame,player.sprite.y() + frame_height as i32 * direction_spritesheet_row(player.direction),frame_width,frame_height,);// Treat the center of the screen as the (0, 0) coordinatelet screen_position = player.position + Point::new(width as i32 / 2, height as i32 / 2);let screen_rect = Rect::from_center(screen_position, frame_width, frame_height);canvas.copy(texture, current_frame, screen_rect)?;canvas.present();Ok(())}// Update player a fixed amount based on their speed.// WARNING: Calling this function too often or at a variable speed will cause the player's speed// to be unpredictable!fn update_player(player: &mut Player) {use self::Direction::*;match player.direction {Left => {player.position = player.position.offset(-player.speed, 0);},Right => {player.position = player.position.offset(player.speed, 0);},Up => {player.position = player.position.offset(0, -player.speed);},Down => {player.position = player.position.offset(0, player.speed);},}// Only continue to animate if the player is movingif player.speed != 0 {// Cheat: using the fact that all animations are 3 frames (NOT extensible)player.current_frame = (player.current_frame + 1) % 3;}}fn main() -> Result<(), String> {let sdl_context = sdl2::init()?;let video_subsystem = sdl_context.video()?;// Leading "_" tells Rust that this is an unused variable that we don't care about. It has to// stay unused because if we don't have any variable at all then Rust will treat it as a// temporary value and drop it right away!let _image_context = image::init(InitFlag::PNG | InitFlag::JPG)?;let window = video_subsystem.window("game tutorial", 800, 600).position_centered().build().expect("could not initialize video subsystem");let mut canvas = window.into_canvas().build().expect("could not make a canvas");let texture_creator = canvas.texture_creator();let textures = [texture_creator.load_texture("assets/bardo.png")?,];// First texture in textures arraylet player_spritesheet = 0;let player_top_left_frame = Rect::new(0, 0, 26, 36);let player_animation = MovementAnimation {current_frame: 0,up_frames: character_animation_frames(player_spritesheet, player_top_left_frame, Direction::Up),down_frames: character_animation_frames(player_spritesheet, player_top_left_frame, Direction::Down),left_frames: character_animation_frames(player_spritesheet, player_top_left_frame, Direction::Left),right_frames: character_animation_frames(player_spritesheet, player_top_left_frame, Direction::Right),};let mut world = World::new();world.create_entity().with(Position(Point::new(0, 0))).with(Velocity {speed: 0, direction: Direction::Right}).with(player_animation.right_frames[0].clone()).with(player_animation).build();let mut event_pump = sdl_context.event_pump()?;let mut i = 0;'running: loop {// Handle eventsfor event in event_pump.poll_iter() {match event {Event::Quit {..} |Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {break 'running;},Event::KeyDown { keycode: Some(Keycode::Left), repeat: false, .. } => {player.speed = PLAYER_MOVEMENT_SPEED;player.direction = Direction::Left;},Event::KeyDown { keycode: Some(Keycode::Right), repeat: false, .. } => {player.speed = PLAYER_MOVEMENT_SPEED;player.direction = Direction::Right;},Event::KeyDown { keycode: Some(Keycode::Up), repeat: false, .. } => {player.speed = PLAYER_MOVEMENT_SPEED;player.direction = Direction::Up;},Event::KeyDown { keycode: Some(Keycode::Down), repeat: false, .. } => {player.speed = PLAYER_MOVEMENT_SPEED;player.direction = Direction::Down;},Event::KeyUp { keycode: Some(Keycode::Left), repeat: false, .. } |Event::KeyUp { keycode: Some(Keycode::Right), repeat: false, .. } |Event::KeyUp { keycode: Some(Keycode::Up), repeat: false, .. } |Event::KeyUp { keycode: Some(Keycode::Down), repeat: false, .. } => {player.speed = 0;},_ => {}}}// Updatei = (i + 1) % 255;update_player(&mut player);// Renderrender(&mut canvas, Color::RGB(i, 64, 255 - i), &texture, &player)?;// Time management!::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 20));}Ok(())}