courier
Clone
https://git.tahinli.com/Tahinli/courier.git
0 watching·0 forks·Rust·AGPL-3.0·5.3 MiB·6d9eeb9·Tahinli·21 days ago·3 commits
| 1 | use crate::cart::Cart; |
| 2 | use crate::game::*; |
| 3 | use crate::iso::*; |
| 4 | use crate::sheets::CharSheets; |
| 5 | use crate::world::*; |
| 6 | use bevy::prelude::*; |
| 7 | use std::collections::HashMap; |
| 8 | |
| 9 | const STRIDE: f32 = 1.6; |
| 10 | |
| 11 | #[derive(Component)] |
| 12 | pub struct Mover { |
| 13 | pub pos: Vec2, |
| 14 | pub facing: usize, |
| 15 | pub moving: bool, |
| 16 | pub frame: f32, |
| 17 | pub pose: usize, |
| 18 | } |
| 19 | |
| 20 | impl Mover { |
| 21 | pub fn new(pos: Vec2, facing: usize) -> Self { |
| 22 | Self { pos, facing, moving: false, frame: 0.0, pose: 0 } |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | #[derive(Component)] |
| 27 | pub struct Player; |
| 28 | |
| 29 | #[derive(Component)] |
| 30 | pub struct Small; |
| 31 | |
| 32 | #[derive(Component)] |
| 33 | pub struct MainCamera; |
| 34 | |
| 35 | #[derive(Resource, Default)] |
| 36 | pub struct Pad { |
| 37 | pub dir: Vec2, |
| 38 | pub interact: bool, |
| 39 | pub choice: Option<usize>, |
| 40 | pub journey: bool, |
| 41 | pub camp: bool, |
| 42 | pub eat: bool, |
| 43 | pub inventory: bool, |
| 44 | pub lantern: bool, |
| 45 | } |
| 46 | |
| 47 | #[derive(Resource, Default)] |
| 48 | pub struct Nearby(pub Option<Talk>); |
| 49 | |
| 50 | #[derive(Resource)] |
| 51 | pub struct Scripted; |
| 52 | |
| 53 | pub const WALK: f32 = 2.4; |
| 54 | |
| 55 | pub struct PlayerPlugin; |
| 56 | |
| 57 | impl Plugin for PlayerPlugin { |
| 58 | fn build(&self, app: &mut App) { |
| 59 | app.init_resource::<Pad>() |
| 60 | .init_resource::<Nearby>() |
| 61 | .add_systems(Startup, spawn_player.after(crate::world::build_world).after(crate::sheets::load_sheets)) |
| 62 | .add_systems(PreUpdate, read_keys.run_if(not(resource_exists::<Scripted>))) |
| 63 | .add_systems( |
| 64 | Update, |
| 65 | (move_player, find_nearby, interact, set_pose, animate, place_movers, camera_follow).chain(), |
| 66 | ); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | fn spawn_player(mut commands: Commands, sheets: Res<CharSheets>) { |
| 71 | let pos = ASHFORD + Vec2::new(-7.5, 8.0); |
| 72 | commands.spawn(( |
| 73 | sheets.sprite("player", 3 * 9), |
| 74 | anchor(sheets.spr("player")), |
| 75 | Transform::from_translation(to_screen(pos).extend(depth(pos))).with_scale(Vec3::splat(PX)), |
| 76 | Mover::new(pos, 3), |
| 77 | Player, |
| 78 | )); |
| 79 | } |
| 80 | |
| 81 | fn read_keys(keys: Res<ButtonInput<KeyCode>>, mut pad: ResMut<Pad>) { |
| 82 | let mut d = Vec2::ZERO; |
| 83 | if keys.any_pressed([KeyCode::KeyW, KeyCode::ArrowUp]) { |
| 84 | d.y += 1.0; |
| 85 | } |
| 86 | if keys.any_pressed([KeyCode::KeyS, KeyCode::ArrowDown]) { |
| 87 | d.y -= 1.0; |
| 88 | } |
| 89 | if keys.any_pressed([KeyCode::KeyA, KeyCode::ArrowLeft]) { |
| 90 | d.x -= 1.0; |
| 91 | } |
| 92 | if keys.any_pressed([KeyCode::KeyD, KeyCode::ArrowRight]) { |
| 93 | d.x += 1.0; |
| 94 | } |
| 95 | pad.dir = d; |
| 96 | pad.interact = keys.just_pressed(KeyCode::KeyE) || keys.just_pressed(KeyCode::Space); |
| 97 | pad.journey = keys.pressed(KeyCode::ShiftLeft); |
| 98 | pad.camp = keys.just_pressed(KeyCode::KeyC); |
| 99 | pad.eat = keys.just_pressed(KeyCode::KeyF); |
| 100 | pad.inventory = keys.just_pressed(KeyCode::Tab); |
| 101 | pad.lantern = keys.just_pressed(KeyCode::KeyL); |
| 102 | let nums = [KeyCode::Digit1, KeyCode::Digit2, KeyCode::Digit3, KeyCode::Digit4, KeyCode::Digit5]; |
| 103 | pad.choice = nums.iter().position(|k| keys.just_pressed(*k)); |
| 104 | } |
| 105 | |
| 106 | pub fn facing_of(screen_dir: Vec2) -> usize { |
| 107 | let a = screen_dir.y.atan2(screen_dir.x).to_degrees(); |
| 108 | let k = (a / 45.0).round() as i32; |
| 109 | (5 + k).rem_euclid(8) as usize |
| 110 | } |
| 111 | |
| 112 | pub fn screen_of(facing: usize) -> Vec2 { |
| 113 | let a = (facing as f32 - 5.0) * std::f32::consts::FRAC_PI_4; |
| 114 | Vec2::new(a.cos(), a.sin()) |
| 115 | } |
| 116 | |
| 117 | pub fn to_world_dir(sd: Vec2) -> Vec2 { |
| 118 | Vec2::new(sd.x / TW - sd.y / TH, -sd.x / TW - sd.y / TH).normalize() |
| 119 | } |
| 120 | |
| 121 | pub fn sort_z(pos: Vec2, occ: &Query<&Occluder>) -> f32 { |
| 122 | let mut z = depth(pos); |
| 123 | for o in occ { |
| 124 | let c = (o.min + o.max) * 0.5; |
| 125 | if (pos - c).length() > 12.0 { |
| 126 | continue; |
| 127 | } |
| 128 | let q = pos.clamp(o.min, o.max); |
| 129 | let d = (pos.x - q.x) + (pos.y - q.y); |
| 130 | if d > 0.0 { |
| 131 | z = z.max(o.z + 0.001); |
| 132 | } else if d < 0.0 { |
| 133 | z = z.min(o.z - 0.001); |
| 134 | } |
| 135 | } |
| 136 | z |
| 137 | } |
| 138 | |
| 139 | pub fn move_player(time: Res<Time>, pad: Res<Pad>, mut g: ResMut<Game>, map: Res<Map>, mut q: Query<&mut Mover, With<Player>>) { |
| 140 | let Ok(mut m) = q.single_mut() else { return }; |
| 141 | let busy = g.dialog.is_some() || g.camping || g.request.is_some(); |
| 142 | g.timelapse = !busy && pad.journey && g.zone == Zone::Road; |
| 143 | if busy || pad.dir == Vec2::ZERO { |
| 144 | m.moving = false; |
| 145 | return; |
| 146 | } |
| 147 | let sd = pad.dir.normalize(); |
| 148 | let gd = to_world_dir(sd); |
| 149 | let tired = if g.energy < 15.0 { 0.6 } else { 1.0 }; |
| 150 | let hurry = if g.timelapse { 4.0 } else { 1.0 }; |
| 151 | let laden = if g.flag("carrying") { 0.8 } else { 1.0 }; |
| 152 | let speed = WALK * tired * hurry * laden; |
| 153 | let step = gd * speed * time.delta_secs(); |
| 154 | let mut p = m.pos; |
| 155 | if map.walkable(p + Vec2::new(step.x, 0.0)) { |
| 156 | p.x += step.x; |
| 157 | } |
| 158 | if map.walkable(p + Vec2::new(0.0, step.y)) { |
| 159 | p.y += step.y; |
| 160 | } |
| 161 | m.moving = p != m.pos; |
| 162 | m.pos = p; |
| 163 | m.facing = facing_of(sd); |
| 164 | let z = zone_at(p); |
| 165 | if z != g.zone { |
| 166 | if z != Zone::Road { |
| 167 | g.sfx.push(if z == Zone::Ashford { Sfx::Home } else { Sfx::Arrive }); |
| 168 | g.toasts.push(z.name().to_string()); |
| 169 | g.flags.retain(|f| !f.starts_with("ev_")); |
| 170 | } |
| 171 | g.zone = z; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | fn find_nearby(mut near: ResMut<Nearby>, q: Query<&Mover, With<Player>>, objs: Query<&Interact>) { |
| 176 | let Ok(m) = q.single() else { return }; |
| 177 | near.0 = objs |
| 178 | .iter() |
| 179 | .map(|i| (i.talk, (i.pos - m.pos).length())) |
| 180 | .filter(|(_, d)| *d < 1.7) |
| 181 | .min_by(|a, b| a.1.total_cmp(&b.1)) |
| 182 | .map(|(t, _)| t); |
| 183 | } |
| 184 | |
| 185 | fn interact(pad: Res<Pad>, near: Res<Nearby>, mut g: ResMut<Game>, pq: Query<&Mover, With<Player>>, mut npcs: Query<(&mut Mover, &NpcTag), Without<Player>>) { |
| 186 | if !pad.interact || g.dialog.is_some() || g.camping { |
| 187 | return; |
| 188 | } |
| 189 | let Some(t) = near.0 else { return }; |
| 190 | g.dialog = Some((t, "start")); |
| 191 | if let (Talk::Npc(n), Ok(p)) = (t, pq.single()) { |
| 192 | for (mut m, tag) in &mut npcs { |
| 193 | if tag.0 == n { |
| 194 | let d = to_screen(p.pos) - to_screen(m.pos); |
| 195 | m.facing = facing_of(d); |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | pub fn set_pose(g: Res<Game>, carts: Query<&Cart>, mut q: Query<&mut Mover, With<Player>>) { |
| 202 | let Ok(mut m) = q.single_mut() else { return }; |
| 203 | let pushing = carts.iter().any(|c| c.attached); |
| 204 | m.pose = if g.flag("carrying") { |
| 205 | 1 |
| 206 | } else if pushing { |
| 207 | 2 |
| 208 | } else { |
| 209 | 0 |
| 210 | }; |
| 211 | } |
| 212 | |
| 213 | fn animate(mut last: Local<HashMap<Entity, Vec2>>, mut q: Query<(Entity, &mut Mover, &mut Sprite)>) { |
| 214 | for (e, mut m, mut s) in &mut q { |
| 215 | let prev = last.insert(e, m.pos).unwrap_or(m.pos); |
| 216 | let dist = (m.pos - prev).length(); |
| 217 | m.moving = dist > 1e-6; |
| 218 | if m.moving { |
| 219 | m.frame = (m.frame + dist / STRIDE * 8.0) % 8.0; |
| 220 | } else { |
| 221 | m.frame = 0.0; |
| 222 | } |
| 223 | let col = if m.moving { 1 + m.frame as usize } else { 0 }; |
| 224 | let want = (m.pose * 8 + m.facing) * 9 + col; |
| 225 | if let Some(a) = &mut s.texture_atlas { |
| 226 | if a.index != want { |
| 227 | a.index = want; |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | fn place_movers(mut q: Query<(&Mover, &mut Transform)>, occ: Query<&Occluder>) { |
| 234 | for (m, mut t) in &mut q { |
| 235 | t.translation = to_screen(m.pos).extend(sort_z(m.pos, &occ)); |
| 236 | t.scale = Vec3::splat(PX); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | fn camera_follow( |
| 241 | time: Res<Time>, |
| 242 | mut cam: ResMut<crate::CamTrue>, |
| 243 | pq: Query<&Transform, (With<Player>, Without<MainCamera>, Without<crate::Upscaled>)>, |
| 244 | mut cq: Query<&mut Transform, (With<MainCamera>, Without<crate::Upscaled>)>, |
| 245 | mut uq: Query<&mut Transform, (With<crate::Upscaled>, Without<MainCamera>)>, |
| 246 | ) { |
| 247 | let (Ok(p), Ok(mut c), Ok(mut u)) = (pq.single(), cq.single_mut(), uq.single_mut()) else { return }; |
| 248 | let k = 1.0 - (-time.delta_secs() * 4.0).exp(); |
| 249 | let target = p.translation.truncate() + Vec2::new(0.0, 40.0); |
| 250 | cam.0 = cam.0.lerp(target, k); |
| 251 | let snapped = (cam.0 / 2.0).round() * 2.0; |
| 252 | c.translation = snapped.extend(c.translation.z); |
| 253 | u.translation = (-(cam.0 - snapped)).extend(u.translation.z); |
| 254 | } |