aboutsummaryrefslogtreecommitdiff
path: root/src/entities.rs
blob: 98c84432444f58c9a4fb0943f75e79b40cb2a4bc (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
use std::cmp;

use crate::tiling::{Tile, TileType};
use crate::world::{apply_movement, Movement, Point};

pub trait Entity {
    /// Get information about the entity
    fn info(&self) -> String;
    /// Initial placement of the entity
    fn place(&mut self, location: Point);
    /// Get the tiletype for the entity
    fn tile(&self) -> &Tile;
    /// Get the entity's current location
    fn location(&self) -> &Point;
    /// Get the entity's previous location (before it moved)
    fn previous_location(&self) -> &Point;
    /// Move the entity to another point
    fn move_to(&mut self, location: Point);
    /// Move the entity with a movement differential
    fn move_by(&mut self, movement: Movement) -> Result<(), String>;
    /// Know if the entity needs to be re-rendered
    fn is_dirty(&self) -> bool;
    /// Declare the entity clean
    fn clean(&mut self);
    fn visibility(&mut self, visible: bool);
    fn is_visible(&self) -> bool;
}

#[derive(Clone)]
pub struct Character {
    pub name: String,
    pub class: String,
    pub health: i32,
    pub level: i32,
    location: Point,
    previous_location: Point,
    dirty: bool,
    max_health: i32,
    attack: i32,
    dodge: i32,
    luck: i32,
    xp: i32,
    tile: Tile,
}

pub trait Enemy {
    fn new(
        class: String,
        health: i32,
        attack: i32,
        dodge: i32,
        luck: i32,
        location: Point,
        tile_str: &'static str,
    ) -> Self;

    fn set_tile(&mut self, tile: Tile);
}

pub trait Player {
    fn new(name: String, class: String, health: i32, attack: i32, dodge: i32, luck: i32) -> Self;
    fn damage(&mut self, damage_amount: i32);
    fn heal(&mut self, heal_amount: i32);
    fn attack(&self) -> i32;
    fn dodge(&self) -> i32;
    fn stats(&self) -> String;
}

impl Entity for Character {
    fn place(&mut self, location: Point) {
        self.location = location;
        self.previous_location = location;
        self.dirty = true;
    }

    fn info(&self) -> String {
        format!(
            "{} \thp: {} attack: {} dodge: {} luck: {}",
            self.class, self.health, self.attack, self.dodge, self.luck
        )
    }

    fn tile(&self) -> &Tile {
        &self.tile
    }

    fn location(&self) -> &Point {
        &self.location
    }

    fn previous_location(&self) -> &Point {
        &self.previous_location
    }

    fn move_to(&mut self, location: Point) {
        self.previous_location = self.location;
        self.location = location;
        self.dirty = true;
    }

    fn move_by(&mut self, movement: Movement) -> Result<(), String> {
        self.previous_location = self.location;
        self.location = apply_movement(self.location, movement)?;
        Ok(())
    }

    fn is_dirty(&self) -> bool {
        self.dirty
    }

    fn clean(&mut self) {
        self.dirty = false;
    }

    fn visibility(&mut self, visible: bool) {
        if visible != self.is_visible() {
            self.dirty = true;
        }
        self.tile.visibility(visible)
    }

    fn is_visible(&self) -> bool {
        self.tile.is_visible()
    }
}

impl Enemy for Character {
    fn new(
        class: String,
        health: i32,
        attack: i32,
        dodge: i32,
        luck: i32,
        location: Point,
        tile_str: &'static str,
    ) -> Character {
        Character {
            name: class.clone(),
            class: class.clone(),
            max_health: health,
            health,
            attack,
            dodge,
            luck,
            level: 0,
            xp: 0,
            location,
            previous_location: location,
            tile: Tile::from(TileType::Character(tile_str)),
            dirty: false,
        }
    }

    fn set_tile(&mut self, tile: Tile) {
        self.tile = tile
    }
}

impl Player for Character {
    fn new(
        name: String,
        class: String,
        health: i32,
        attack: i32,
        dodge: i32,
        luck: i32,
    ) -> Character {
        Character {
            name,
            class,
            max_health: health,
            health,
            attack,
            dodge,
            luck,
            xp: 0,
            level: 0,
            location: (0, 0),
            previous_location: (0, 0),
            tile: Tile::new(
                TileType::Player,
                true, // player is visible by default
                false,
            ),
            dirty: false,
        }
    }

    fn damage(&mut self, damage_amount: i32) {
        self.health = cmp::max(0, self.health - damage_amount);
        self.xp += 2;
    }

    fn heal(&mut self, heal_amount: i32) {
        if (self.health) <= self.max_health {
            self.health = cmp::min(self.health + heal_amount, self.max_health);
            self.xp += 1;
        }
    }

    fn attack(&self) -> i32 {
        self.xp + self.attack + self.luck / 2
    }

    fn dodge(&self) -> i32 {
        self.xp + self.dodge + self.luck / 2
    }

    fn stats(&self) -> String {
        format!(
            "{}({}) - hp: {}/{} attack: {} dodge: {} luck: {} experience: {}",
            self.name,
            self.class,
            self.health,
            self.max_health,
            self.attack,
            self.dodge,
            self.luck,
            self.xp
        )
    }
}