- File Changes
- .gitignore
- Cargo.toml
- src/components.rs
- assets/bardo.png
- assets/darkdimension.png
- assets/reaper.png
- src/main.rs
- src/physics.rs
- assets
-
src
- components.rs
- main.rs
- physics.rs
- .gitignore
- Cargo.toml
+1
-16
src/main.rs
1 | 1 |
|
2 |
|
|
2 | 3 |
|
3 | 4 |
|
4 | 5 |
|
5 | 6 |
|
6 | 7 |
|
7 | 8 |
|
8 | 9 |
|
9 | 10 |
|
10 | 11 |
|
11 | 12 |
|
12 | 13 |
|
13 | 14 |
|
14 | 15 |
|
15 | 16 |
|
16 | 17 |
|
17 | 18 |
|
18 | 19 |
|
19 | 20 |
|
20 | 21 |
|
21 | 22 |
|
22 | 23 |
|
23 | 24 |
|
24 | 25 |
|
25 | 26 |
|
26 | 27 |
|
27 | 28 |
|
28 | 29 |
|
29 | 30 |
|
30 | 31 |
|
31 | 32 |
|
32 | 33 |
|
33 | 34 |
|
34 | 35 |
|
35 | 36 |
|
36 | 37 |
|
37 | 38 |
|
38 | 39 |
|
39 | 40 |
|
40 | 41 |
|
41 | 42 |
|
42 | 43 |
|
43 | 44 |
|
44 | 45 |
|
45 | 46 |
|
46 | 47 |
|
47 | 48 |
|
48 | 49 |
|
49 | 50 |
|
50 | 51 |
|
51 | 52 |
|
52 | 53 |
|
53 | 54 |
|
54 | 55 |
|
55 | 56 |
|
56 | 57 |
|
57 | 58 |
|
58 | 59 |
|
59 | 60 |
|
60 | 61 |
|
61 | 62 |
|
62 | 63 |
|
63 | 64 |
|
64 | 65 |
|
65 | 66 |
|
66 | 67 |
|
67 | 68 |
|
68 | 69 |
|
69 | 70 |
|
70 | 71 |
|
71 | 72 |
|
72 | 73 |
|
73 | 74 |
|
74 | 75 |
|
75 | 76 |
|
76 | 77 |
|
77 | 78 |
|
78 | 79 |
|
79 | 80 |
|
80 | 81 |
|
81 | 82 |
|
82 | 83 |
|
83 | 84 |
|
84 | 85 |
|
85 | 86 |
|
86 | 87 |
|
87 | 88 |
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
|
104 | 89 |
|
105 | 90 |
|
106 | 91 |
|
107 | 92 |
|
108 | 93 |
|
109 | 94 |
|
110 | 95 |
|
111 | 96 |
|
112 | 97 |
|
113 | 98 |
|
114 | 99 |
|
115 | 100 |
|
116 | 101 |
|
117 | 102 |
|
118 | 103 |
|
119 | 104 |
|
120 | 105 |
|
121 | 106 |
|
122 | 107 |
|
123 | 108 |
|
124 | 109 |
|
125 | 110 |
|
126 | 111 |
|
127 | 112 |
|
128 | 113 |
|
129 | 114 |
|
130 | 115 |
|
131 | 116 |
|
132 | 117 |
|
133 | 118 |
|
134 | 119 |
|
135 | 120 |
|
136 | 121 |
|
137 | 122 |
|
138 | 123 |
|
139 | 124 |
|
140 | 125 |
|
141 | 126 |
|
142 | 127 |
|
143 | 128 |
|
144 | 129 |
|
145 | 130 |
|
146 | 131 |
|
147 | 132 |
|
148 | 133 |
|
149 | 134 |
|
150 | 135 |
|
151 | 136 |
|
152 | 137 |
|
153 | 138 |
|
154 | 139 |
|
155 | 140 |
|
156 | 141 |
|
157 | 142 |
|
158 | 143 |
|
159 | 144 |
|
160 | 145 |
|
161 | 146 |
|
162 | 147 |
|
163 | 148 |
|
164 | 149 |
|
165 | 150 |
|
166 | 151 |
|
167 | 152 |
|
168 | 153 |
|
169 | 154 |
|
170 | 155 |
|
171 | 156 |
|
172 | 157 |
|
173 | 158 |
|
174 | 159 |
|
175 | 160 |
|
176 | 161 |
|
177 | 162 |
|
178 | 163 |
|
179 | 164 |
|
180 | 165 |
|
181 | 166 |
|
182 | 167 |
|
183 | 168 |
|
184 | 169 |
|
185 | 170 |
|
186 | 171 |
|
187 | 172 |
|
188 | 173 |
|
189 | 174 |
|
190 | 175 |
|
191 | 176 |
|
192 | 177 |
|
193 | 178 |
|
194 | 179 |
|
195 | 180 |
|
196 | 181 |
|
197 | 182 |
|
198 | 183 |
|
199 | 184 |
|
200 | 185 |
|
201 | 186 |
|
+31
-0
src/physics.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 |
|
+1
-16
src/main.rs
1 |
|
1 |
|
2 |
|
||
2 |
|
3 |
|
3 |
|
4 |
|
4 |
|
5 |
|
5 |
|
6 |
|
6 |
|
7 |
|
7 |
|
8 |
|
8 |
|
9 |
|
9 |
|
10 |
|
10 |
|
11 |
|
11 |
|
12 |
|
12 |
|
13 |
|
13 |
|
14 |
|
14 |
|
15 |
|
15 |
|
16 |
|
16 |
|
17 |
|
17 |
|
18 |
|
18 |
|
19 |
|
19 |
|
20 |
|
20 |
|
21 |
|
21 |
|
22 |
|
22 |
|
23 |
|
23 |
|
24 |
|
24 |
|
25 |
|
25 |
|
26 |
|
26 |
|
27 |
|
27 |
|
28 |
|
28 |
|
29 |
|
29 |
|
30 |
|
30 |
|
31 |
|
31 |
|
32 |
|
32 |
|
33 |
|
33 |
|
34 |
|
34 |
|
35 |
|
35 |
|
36 |
|
36 |
|
37 |
|
37 |
|
38 |
|
38 |
|
39 |
|
39 |
|
40 |
|
40 |
|
41 |
|
41 |
|
42 |
|
42 |
|
43 |
|
43 |
|
44 |
|
44 |
|
45 |
|
45 |
|
46 |
|
46 |
|
47 |
|
47 |
|
48 |
|
48 |
|
49 |
|
49 |
|
50 |
|
50 |
|
51 |
|
51 |
|
52 |
|
52 |
|
53 |
|
53 |
|
54 |
|
54 |
|
55 |
|
55 |
|
56 |
|
56 |
|
57 |
|
57 |
|
58 |
|
58 |
|
59 |
|
59 |
|
60 |
|
60 |
|
61 |
|
61 |
|
62 |
|
62 |
|
63 |
|
63 |
|
64 |
|
64 |
|
65 |
|
65 |
|
66 |
|
66 |
|
67 |
|
67 |
|
68 |
|
68 |
|
69 |
|
69 |
|
70 |
|
70 |
|
71 |
|
71 |
|
72 |
|
72 |
|
73 |
|
73 |
|
74 |
|
74 |
|
75 |
|
75 |
|
76 |
|
76 |
|
77 |
|
77 |
|
78 |
|
78 |
|
79 |
|
79 |
|
80 |
|
80 |
|
81 |
|
81 |
|
82 |
|
82 |
|
83 |
|
83 |
|
84 |
|
84 |
|
85 |
|
85 |
|
86 |
|
86 |
|
87 |
|
87 |
|
88 |
|
88 |
|
||
89 |
|
||
90 |
|
||
91 |
|
||
92 |
|
||
93 |
|
||
94 |
|
||
95 |
|
||
96 |
|
||
97 |
|
||
98 |
|
||
99 |
|
||
100 |
|
||
101 |
|
||
102 |
|
||
103 |
|
||
104 |
|
89 |
|
105 |
|
90 |
|
106 |
|
91 |
|
107 |
|
92 |
|
108 |
|
93 |
|
109 |
|
94 |
|
110 |
|
95 |
|
111 |
|
96 |
|
112 |
|
97 |
|
113 |
|
98 |
|
114 |
|
99 |
|
115 |
|
100 |
|
116 |
|
101 |
|
117 |
|
102 |
|
118 |
|
103 |
|
119 |
|
104 |
|
120 |
|
105 |
|
121 |
|
106 |
|
122 |
|
107 |
|
123 |
|
108 |
|
124 |
|
109 |
|
125 |
|
110 |
|
126 |
|
111 |
|
127 |
|
112 |
|
128 |
|
113 |
|
129 |
|
114 |
|
130 |
|
115 |
|
131 |
|
116 |
|
132 |
|
117 |
|
133 |
|
118 |
|
134 |
|
119 |
|
135 |
|
120 |
|
136 |
|
121 |
|
137 |
|
122 |
|
138 |
|
123 |
|
139 |
|
124 |
|
140 |
|
125 |
|
141 |
|
126 |
|
142 |
|
127 |
|
143 |
|
128 |
|
144 |
|
129 |
|
145 |
|
130 |
|
146 |
|
131 |
|
147 |
|
132 |
|
148 |
|
133 |
|
149 |
|
134 |
|
150 |
|
135 |
|
151 |
|
136 |
|
152 |
|
137 |
|
153 |
|
138 |
|
154 |
|
139 |
|
155 |
|
140 |
|
156 |
|
141 |
|
157 |
|
142 |
|
158 |
|
143 |
|
159 |
|
144 |
|
160 |
|
145 |
|
161 |
|
146 |
|
162 |
|
147 |
|
163 |
|
148 |
|
164 |
|
149 |
|
165 |
|
150 |
|
166 |
|
151 |
|
167 |
|
152 |
|
168 |
|
153 |
|
169 |
|
154 |
|
170 |
|
155 |
|
171 |
|
156 |
|
172 |
|
157 |
|
173 |
|
158 |
|
174 |
|
159 |
|
175 |
|
160 |
|
176 |
|
161 |
|
177 |
|
162 |
|
178 |
|
163 |
|
179 |
|
164 |
|
180 |
|
165 |
|
181 |
|
166 |
|
182 |
|
167 |
|
183 |
|
168 |
|
184 |
|
169 |
|
185 |
|
170 |
|
186 |
|
171 |
|
187 |
|
172 |
|
188 |
|
173 |
|
189 |
|
174 |
|
190 |
|
175 |
|
191 |
|
176 |
|
192 |
|
177 |
|
193 |
|
178 |
|
194 |
|
179 |
|
195 |
|
180 |
|
196 |
|
181 |
|
197 |
|
182 |
|
198 |
|
183 |
|
199 |
|
184 |
|
200 |
|
185 |
|
201 |
|
186 |
|
+31
-0
src/physics.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 |
|
/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 = false
features = ["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 from
pub spritesheet: usize,
/// The current region of the spritesheet to be rendered
pub region: Rect,
}
#[derive(Component, Debug)]
#[storage(VecStorage)]
pub struct MovementAnimation {
// The current frame in the animation of the direction this entity is moving in
pub 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;
mod physics;
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 listed
use 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 direction
fn 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 spritesheet
fn 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) coordinate
let 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) {
// Only continue to animate if the player is moving
if 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 array
let 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 events
for 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;
},
_ => {}
}
}
// Update
i = (i + 1) % 255;
update_player(&mut player);
// Render
render(&mut canvas, Color::RGB(i, 64, 255 - i), &texture, &player)?;
// Time management!
::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 20));
}
Ok(())
}
use specs::prelude::*;
use crate::components::*;
struct Physics;
impl<'a> System<'a> for Physics {
type SystemData = (WriteStorage<'a, Position>, ReadStorage<'a, Velocity>);
fn run(&mut self, mut data: Self::SystemData) {
use self::Direction::*;
//TODO: This code can be made nicer and more idiomatic using more pattern matching.
// Look up "rust irrefutable patterns" and use them here.
for (pos, vel) in (&mut data.0, &data.1).join() {
match vel.direction {
Left => {
pos.0 = pos.0.offset(-vel.speed, 0);
},
Right => {
pos.0 = pos.0.offset(vel.speed, 0);
},
Up => {
pos.0 = pos.0.offset(0, -vel.speed);
},
Down => {
pos.0 = pos.0.offset(0, vel.speed);
},
}
}
}
}