-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
perspective-camera.rs
193 lines (162 loc) · 5.26 KB
/
perspective-camera.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use comfy::*;
// This example is almost exactly the same as the ECS Topdown game example, except using a 2.5D
// perspective.
simple_game!("Perspective ECS Topdown Game", setup, update);
struct Player;
struct Grass;
struct Enemy;
// We'll rotate every sprite by the same amount to make them face the camera
// while the ground plane remains rotated up.
pub static ROT3: AtomicCell<f32> = AtomicCell::new(ROT3_AMOUNT);
// Default amount of 3d rotation.
pub const ROT3_AMOUNT: f32 = PI / 3.0;
pub const Z_MOBS: i32 = 5;
pub struct CameraSettings {
pub use_camera_override: bool,
pub offset: Vec2,
pub eye_z: f32,
pub fov: f32,
}
impl Default for CameraSettings {
fn default() -> Self {
Self {
use_camera_override: true,
offset: vec2(0.0, -10.0),
eye_z: 10.0,
fov: 0.8,
}
}
}
pub static CAMERA_SETTINGS: Lazy<AtomicRefCell<CameraSettings>> =
Lazy::new(|| AtomicRefCell::new(CameraSettings::default()));
fn setup(c: &mut EngineContext) {
main_camera_mut().matrix_fn = Some(Box::new(|_, center: Vec2| {
let settings = CAMERA_SETTINGS.borrow();
let eye = vec3(center.x, center.y, settings.eye_z);
let up = vec3(0.0, 1.0, 0.0);
let perspective =
Mat4::perspective_rh(settings.fov, aspect_ratio(), 0.01, 1000.0);
let view = Mat4::look_at_rh(
eye + settings.offset.extend(0.0),
center.extend(0.0),
up,
);
perspective * view
}));
set_y_sort(Z_MOBS, true);
c.load_texture_from_bytes(
"grass",
include_bytes!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../assets/grass.png"
)),
);
// Load the player texture
c.load_texture_from_bytes(
"player",
include_bytes!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../assets/chicken.png"
)),
);
for x in 0..50 {
for y in 0..50 {
let variant = random_i32(0, 2);
// Tile the world with random variant of grass sprite
commands().spawn((
Sprite::new("grass".to_string(), vec2(1.0, 1.0), 0, WHITE)
.with_rect(32 * variant, 0, 32, 32),
Transform::position(vec2(x as f32, y as f32)),
Grass,
));
}
}
let start_pos = vec2(25.0, 25.0);
// Spawn the player entity and make sure z-index is above the grass
commands().spawn((
Transform::position(start_pos),
Player,
AnimatedSpriteBuilder::new()
.z_index(Z_MOBS)
.add_animation("idle", 0.1, true, AnimationSource::Atlas {
name: "player".into(),
offset: ivec2(0, 0),
step: ivec2(16, 0),
size: isplat(16),
frames: 1,
})
.add_animation("walk", 0.05, true, AnimationSource::Atlas {
name: "player".into(),
offset: ivec2(16, 0),
step: ivec2(16, 0),
size: isplat(16),
frames: 6,
})
.build(),
));
let count = 10;
for x in -count..count {
for y in -count..count {
commands().spawn((
Transform::position(
start_pos + vec2(x as f32, y as f32) + splat(0.5),
),
Sprite::new("player", splat(1.0), 5, BLUE)
.with_z_index(Z_MOBS)
.with_rect(0, 0, 16, 16),
// We tag these so that we can query them later.
Enemy,
));
}
}
}
fn update(c: &mut EngineContext) {
clear_background(TEAL);
if is_key_pressed(KeyCode::F2) {
let mut camera = main_camera_mut();
camera.use_matrix_fn = !camera.use_matrix_fn;
ROT3.store(if camera.use_matrix_fn { ROT3_AMOUNT } else { 0.0 });
}
let dt = c.delta;
let mut player_pos = Vec2::ZERO;
for (_, (_, animated_sprite, transform)) in
world().query::<(&Player, &mut AnimatedSprite, &mut Transform)>().iter()
{
animated_sprite.rotation_x = ROT3.load();
player_pos = transform.position;
// Handle movement and animation
let mut moved = false;
let speed = 3.0;
let mut move_dir = Vec2::ZERO;
if is_key_down(KeyCode::W) {
move_dir.y += 1.0;
moved = true;
}
if is_key_down(KeyCode::S) {
move_dir.y -= 1.0;
moved = true;
}
if is_key_down(KeyCode::A) {
move_dir.x -= 1.0;
moved = true;
}
if is_key_down(KeyCode::D) {
move_dir.x += 1.0;
moved = true;
}
if moved {
animated_sprite.flip_x = move_dir.x < 0.0;
transform.position += move_dir.normalize_or_zero() * speed * dt;
animated_sprite.play("walk");
} else {
animated_sprite.play("idle");
}
main_camera_mut().center = transform.position;
}
for (_, (sprite, transform, _enemy)) in
world().query::<(&mut Sprite, &Transform, &Enemy)>().iter()
{
sprite.rotation_x = ROT3.load();
sprite.flip_x = transform.abs_position.x > player_pos.x;
}
}