use crate::cart::Cart; use crate::game::*; use crate::iso::*; use crate::sheets::CharSheets; use crate::world::*; use bevy::prelude::*; use std::collections::HashMap; const STRIDE: f32 = 1.6; #[derive(Component)] pub struct Mover { pub pos: Vec2, pub facing: usize, pub moving: bool, pub frame: f32, pub pose: usize, } impl Mover { pub fn new(pos: Vec2, facing: usize) -> Self { Self { pos, facing, moving: false, frame: 0.0, pose: 0 } } } #[derive(Component)] pub struct Player; #[derive(Component)] pub struct Small; #[derive(Component)] pub struct MainCamera; #[derive(Resource, Default)] pub struct Pad { pub dir: Vec2, pub interact: bool, pub choice: Option, pub journey: bool, pub camp: bool, pub eat: bool, pub inventory: bool, pub lantern: bool, } #[derive(Resource, Default)] pub struct Nearby(pub Option); #[derive(Resource)] pub struct Scripted; pub const WALK: f32 = 2.4; pub struct PlayerPlugin; impl Plugin for PlayerPlugin { fn build(&self, app: &mut App) { app.init_resource::() .init_resource::() .add_systems(Startup, spawn_player.after(crate::world::build_world).after(crate::sheets::load_sheets)) .add_systems(PreUpdate, read_keys.run_if(not(resource_exists::))) .add_systems( Update, (move_player, find_nearby, interact, set_pose, animate, place_movers, camera_follow).chain(), ); } } fn spawn_player(mut commands: Commands, sheets: Res) { let pos = ASHFORD + Vec2::new(-7.5, 8.0); commands.spawn(( sheets.sprite("player", 3 * 9), anchor(sheets.spr("player")), Transform::from_translation(to_screen(pos).extend(depth(pos))).with_scale(Vec3::splat(PX)), Mover::new(pos, 3), Player, )); } fn read_keys(keys: Res>, mut pad: ResMut) { let mut d = Vec2::ZERO; if keys.any_pressed([KeyCode::KeyW, KeyCode::ArrowUp]) { d.y += 1.0; } if keys.any_pressed([KeyCode::KeyS, KeyCode::ArrowDown]) { d.y -= 1.0; } if keys.any_pressed([KeyCode::KeyA, KeyCode::ArrowLeft]) { d.x -= 1.0; } if keys.any_pressed([KeyCode::KeyD, KeyCode::ArrowRight]) { d.x += 1.0; } pad.dir = d; pad.interact = keys.just_pressed(KeyCode::KeyE) || keys.just_pressed(KeyCode::Space); pad.journey = keys.pressed(KeyCode::ShiftLeft); pad.camp = keys.just_pressed(KeyCode::KeyC); pad.eat = keys.just_pressed(KeyCode::KeyF); pad.inventory = keys.just_pressed(KeyCode::Tab); pad.lantern = keys.just_pressed(KeyCode::KeyL); let nums = [KeyCode::Digit1, KeyCode::Digit2, KeyCode::Digit3, KeyCode::Digit4, KeyCode::Digit5]; pad.choice = nums.iter().position(|k| keys.just_pressed(*k)); } pub fn facing_of(screen_dir: Vec2) -> usize { let a = screen_dir.y.atan2(screen_dir.x).to_degrees(); let k = (a / 45.0).round() as i32; (5 + k).rem_euclid(8) as usize } pub fn screen_of(facing: usize) -> Vec2 { let a = (facing as f32 - 5.0) * std::f32::consts::FRAC_PI_4; Vec2::new(a.cos(), a.sin()) } pub fn to_world_dir(sd: Vec2) -> Vec2 { Vec2::new(sd.x / TW - sd.y / TH, -sd.x / TW - sd.y / TH).normalize() } pub fn sort_z(pos: Vec2, occ: &Query<&Occluder>) -> f32 { let mut z = depth(pos); for o in occ { let c = (o.min + o.max) * 0.5; if (pos - c).length() > 12.0 { continue; } let q = pos.clamp(o.min, o.max); let d = (pos.x - q.x) + (pos.y - q.y); if d > 0.0 { z = z.max(o.z + 0.001); } else if d < 0.0 { z = z.min(o.z - 0.001); } } z } pub fn move_player(time: Res