|
| 1 | +use super::{spaceship::SpaceshipEntity, traits::Entity}; |
| 2 | +use crate::{ |
| 3 | + image::{ |
| 4 | + types::GifFrame, |
| 5 | + utils::{ExtraImageUtils, TRAVELLING_BACKGROUND}, |
| 6 | + }, |
| 7 | + types::{AppResult, Tick}, |
| 8 | + world::{constants::SECONDS, spaceship::Spaceship}, |
| 9 | +}; |
| 10 | +use image::{GenericImageView, RgbaImage}; |
| 11 | +use ratatui::layout::Rect; |
| 12 | +use std::collections::HashMap; |
| 13 | + |
| 14 | +const FRICTION_COEFF: f64 = 0.05; |
| 15 | +const MAX_WIDTH: u32 = 160; |
| 16 | +const MAX_HEIGHT: u32 = 80; |
| 17 | + |
| 18 | +#[derive(Default, Debug)] |
| 19 | +pub struct Space { |
| 20 | + id: usize, |
| 21 | + tick: usize, |
| 22 | + background: GifFrame, |
| 23 | + entities: HashMap<usize, Box<dyn Entity>>, |
| 24 | +} |
| 25 | + |
| 26 | +impl Space { |
| 27 | + fn insert_entity(&mut self, entity: Box<dyn Entity>) -> usize { |
| 28 | + let id = self.id.clone(); |
| 29 | + self.entities.insert(id, entity); |
| 30 | + self.id += 1; |
| 31 | + id |
| 32 | + } |
| 33 | + pub fn new() -> AppResult<Self> { |
| 34 | + let bg = TRAVELLING_BACKGROUND.clone(); |
| 35 | + let mut background = RgbaImage::new(bg.width() * 2, bg.height() * 2); |
| 36 | + background.copy_non_trasparent_from(&bg, 0, 0)?; |
| 37 | + background.copy_non_trasparent_from(&bg, bg.width(), 0)?; |
| 38 | + background.copy_non_trasparent_from(&bg, 0, bg.height())?; |
| 39 | + background.copy_non_trasparent_from(&bg, bg.width(), bg.height())?; |
| 40 | + |
| 41 | + Ok(Self { |
| 42 | + background, |
| 43 | + ..Default::default() |
| 44 | + }) |
| 45 | + } |
| 46 | + |
| 47 | + pub fn with_spaceship(mut self, spaceship: &Spaceship) -> AppResult<Self> { |
| 48 | + let spaceship_entity = SpaceshipEntity::from_spaceship(spaceship)?; |
| 49 | + self.insert_entity(Box::new(spaceship_entity)); |
| 50 | + Ok(self) |
| 51 | + } |
| 52 | + |
| 53 | + pub fn current_id(&self) -> usize { |
| 54 | + self.id |
| 55 | + } |
| 56 | + |
| 57 | + pub fn player(&self) -> &Box<dyn Entity> { |
| 58 | + self.entities |
| 59 | + .get(&0) |
| 60 | + .expect("There should be a player entity") |
| 61 | + } |
| 62 | + |
| 63 | + pub fn player_mut(&mut self) -> &mut Box<dyn Entity> { |
| 64 | + self.entities |
| 65 | + .get_mut(&0) |
| 66 | + .expect("There should be a player entity") |
| 67 | + } |
| 68 | + |
| 69 | + pub fn update(&mut self, deltatime_millis: Tick) -> AppResult<()> { |
| 70 | + self.tick += 1; |
| 71 | + let deltatime = deltatime_millis as f64 / SECONDS as f64; |
| 72 | + |
| 73 | + for (_, entity) in self.entities.iter_mut() { |
| 74 | + let [x, y] = entity.position(); |
| 75 | + let [vx, vy] = entity.velocity(); |
| 76 | + let [mut ax, mut ay] = entity.accelleration(); |
| 77 | + ax = ax - FRICTION_COEFF * vx; |
| 78 | + ay = ay - FRICTION_COEFF * vy; |
| 79 | + let new_velocity = [vx + ax * deltatime, vy + ay * deltatime]; |
| 80 | + entity.set_velocity(new_velocity); |
| 81 | + entity.set_accelleration([0.0, 0.0]); |
| 82 | + |
| 83 | + let [nvx, nvy] = entity.velocity(); |
| 84 | + |
| 85 | + let size = entity.size(self.tick); |
| 86 | + let new_position = [ |
| 87 | + (x + nvx * deltatime).min((MAX_WIDTH - size[0]) as f64), |
| 88 | + (y + nvy * deltatime).min((MAX_HEIGHT - size[1]) as f64), |
| 89 | + ]; |
| 90 | + entity.set_position(new_position); |
| 91 | + } |
| 92 | + |
| 93 | + Ok(()) |
| 94 | + } |
| 95 | + |
| 96 | + pub fn gif_frame(&self, frame_size: Rect) -> AppResult<GifFrame> { |
| 97 | + let mut base = self.background.clone(); |
| 98 | + |
| 99 | + for (_, entity) in self.entities.iter() { |
| 100 | + let [x, y] = entity.position(); |
| 101 | + base.copy_non_trasparent_from(&entity.gif_frame(self.tick), x as u32, y as u32)?; |
| 102 | + } |
| 103 | + |
| 104 | + let view = base.view( |
| 105 | + frame_size.x as u32, |
| 106 | + frame_size.y as u32, |
| 107 | + frame_size.width as u32, |
| 108 | + frame_size.height as u32 * 2, // Multiply by 2 because images are rendered in half the lines |
| 109 | + ); |
| 110 | + |
| 111 | + Ok(view.to_image()) |
| 112 | + } |
| 113 | +} |
0 commit comments