// -*- coding: utf-8 -*- // // Copyright 2021 Michael Büsch // // Derived from https://github.com/mlochen/dungeon.git // Copyright (C) 2020 Marco Lochen // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // use crate::vec2d::Vec2D; use crate::wall::Wall; pub struct Player { pos: Vec2D, phi: f32, dir: Vec2D, fov_phi: f32, health: i32, } impl Player { pub fn new(pos: Vec2D) -> Player { let mut self_ = Player { pos, phi: 0.0, dir: Default::default(), fov_phi: 0.0, health: 100, }; self_.calc_dir(); self_ } fn calc_dir(&mut self) { self.dir.set_x(self.phi.cos()); self.dir.set_y(self.phi.sin()); } pub fn set_pos(&mut self, pos: Vec2D) { self.pos = pos; } pub fn get_pos(&self) -> Vec2D { self.pos } pub fn set_phi(&mut self, phi: f32) { self.phi = phi; self.calc_dir(); } pub fn get_phi(&self) -> f32 { self.phi } pub fn get_dir(&self) -> Vec2D { self.dir } pub fn get_fov_phi(&self) -> f32 { self.fov_phi } pub fn set_fov_phi(&mut self, fov_phi: f32) { self.fov_phi = fov_phi; } pub fn set_health(&mut self, health: i32) { self.health = health; } pub fn get_health(&self) -> i32 { self.health } pub fn point_is_in_fov(&self, point: &Vec2D) -> bool { let point_dir = *point - self.pos; let a = Vec2D::new(1.0, 0.0).rotated(self.phi + self.fov_phi / 2.0); let b = Vec2D::new(1.0, 0.0).rotated(self.phi - self.fov_phi / 2.0); point_dir.phi_to_is_positive(&a) && point_dir.phi_to_is_negative(&b) } pub fn wall_is_in_fov(&self, wall: &Wall) -> bool { self.point_is_in_fov(&wall.get_p1()) || self.point_is_in_fov(&wall.get_pos()) || self.point_is_in_fov(&wall.get_p2()) } } // vim: ts=4 sw=4 expandtab