summaryrefslogtreecommitdiffstats
path: root/walleng/src/player.rs
blob: a82100e092d74a6c0a67670eb022d0b6388e08aa (plain)
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
// -*- coding: utf-8 -*-
//
// Copyright 2021 Michael Büsch <m@bues.ch>
//
// 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 <https://www.gnu.org/licenses/>.
//

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
bues.ch cgit interface