about summary refs log tree commit diff
path: root/src/game.rs
blob: f86d32d0463c2164fa66ce240c9d1c0cd6fb20aa (plain) (blame)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
use crate::display::{self, Viewport};
use crate::entities::Character;
use crate::entities::{Creature, Entity};
use crate::messages::message;
use crate::settings::Settings;
use crate::types::command::Command;
use crate::types::entity_map::EntityID;
use crate::types::entity_map::EntityMap;
use crate::types::pos;
use crate::types::Ticks;
use crate::types::{
    BoundingBox, Collision, Dimensions, Position, Positioned, PositionedMut,
};
use rand::rngs::SmallRng;
use rand::SeedableRng;
use std::io::{self, StdinLock, StdoutLock, Write};
use termion::input::Keys;
use termion::input::TermRead;
use termion::raw::RawTerminal;

type Stdout<'a> = RawTerminal<StdoutLock<'a>>;

type Rng = SmallRng;

type AnEntity<'a> = Box<dyn Entity>;

impl<'a> Positioned for AnEntity<'a> {
    fn position(&self) -> Position {
        (**self).position()
    }
}

impl<'a> PositionedMut for AnEntity<'a> {
    fn set_position(&mut self, pos: Position) {
        (**self).set_position(pos)
    }
}

/// The full state of a running Game
pub struct Game<'a> {
    settings: Settings,

    viewport: Viewport<Stdout<'a>>,

    /// An iterator on keypresses from the user
    keys: Keys<StdinLock<'a>>,

    /// The map of all the entities in the game
    entities: EntityMap<AnEntity<'a>>,

    /// The entity ID of the player character
    character_entity_id: EntityID,

    /// The messages that have been said to the user, in forward time order
    messages: Vec<String>,

    /// The index of the currently-displayed message. Used to track the index of
    /// the currently displayed message when handling PreviousMessage commands
    message_idx: usize,

    /// A global random number generator for the game
    rng: Rng,
}

impl<'a> Game<'a> {
    pub fn new(
        settings: Settings,
        stdout: RawTerminal<StdoutLock<'a>>,
        stdin: StdinLock<'a>,
        w: u16,
        h: u16,
    ) -> Game<'a> {
        let rng = match settings.seed {
            Some(seed) => SmallRng::seed_from_u64(seed),
            None => SmallRng::from_entropy(),
        };
        let mut entities: EntityMap<AnEntity<'a>> = EntityMap::new();

        // TODO make this dynamic
        {
            entities.insert(Box::new(Creature::new_from_raw(
                "gormlak",
                pos(10, 0),
            )));
        }

        Game {
            settings,
            rng,
            message_idx: 0,
            viewport: Viewport::new(
                BoundingBox::at_origin(Dimensions { w, h }),
                BoundingBox::at_origin(Dimensions { w: w - 2, h: h - 2 }),
                stdout,
            ),
            keys: stdin.keys(),
            character_entity_id: entities.insert(Box::new(Character::new())),
            messages: Vec::new(),
            entities,
        }
    }

    /// Returns a collision, if any, at the given Position in the game
    fn collision_at(&self, pos: Position) -> Option<Collision> {
        if !pos.within(self.viewport.inner) {
            Some(Collision::Stop)
        } else {
            None
        }
    }

    fn character(&self) -> &Character {
        debug!(
            "ents: {:?} cid: {:?}",
            self.entities.ids().map(|id| *id).collect::<Vec<u32>>(),
            self.character_entity_id
        );
        (*self.entities.get(self.character_entity_id).unwrap())
            .downcast_ref()
            .unwrap()
    }

    /// Draw all the game entities to the screen
    fn draw_entities(&mut self) -> io::Result<()> {
        for entity in self.entities.entities() {
            self.viewport.draw(entity)?;
        }
        Ok(())
    }

    /// Step the game forward the given number of ticks
    fn tick(&mut self, ticks: Ticks) {}

    /// Get a message from the global map based on the rng in this game
    fn message(&mut self, name: &str) -> &'static str {
        message(name, &mut self.rng)
    }

    /// Say a message to the user
    fn say(&mut self, message_name: &str) -> io::Result<()> {
        let message = self.message(message_name);
        self.messages.push(message.to_string());
        self.message_idx = self.messages.len() - 1;
        self.viewport.write_message(message)
    }

    fn previous_message(&mut self) -> io::Result<()> {
        if self.message_idx == 0 {
            return Ok(());
        }
        self.message_idx -= 1;
        let message = &self.messages[self.message_idx];
        self.viewport.write_message(message)
    }

    /// Run the game
    pub fn run(mut self) -> io::Result<()> {
        info!("Running game");
        self.viewport.init()?;
        self.draw_entities()?;
        self.say("global.welcome")?;
        self.flush()?;
        loop {
            let mut old_position = None;
            use Command::*;
            match Command::from_key(self.keys.next().unwrap().unwrap()) {
                Some(Quit) => {
                    info!("Quitting game due to user request");
                    break;
                }

                Some(Move(direction)) => {
                    use Collision::*;
                    let new_pos = self.character().position + direction;
                    match self.collision_at(new_pos) {
                        None => {
                            old_position = Some(self.character().position);
                            self.entities.update_position(
                                self.character_entity_id,
                                new_pos,
                            );
                        }
                        Some(Combat) => unimplemented!(),
                        Some(Stop) => (),
                    }
                }

                Some(PreviousMessage) => self.previous_message()?,

                None => (),
            }

            match old_position {
                Some(old_pos) => {
                    self.tick(self.character().speed().tiles_to_ticks(
                        (old_pos - self.character().position).as_tiles(),
                    ));
                    self.viewport.clear(old_pos)?;
                    self.viewport.draw(
                        // TODO this clone feels unnecessary.
                        &self.character().clone(),
                    )?;
                }
                None => (),
            }
            self.flush()?;
            debug!("{:?}", self.character());
        }
        Ok(())
    }
}

impl<'a> Drop for Game<'a> {
    fn drop(&mut self) {
        display::clear(self).unwrap_or(());
    }
}

impl<'a> Write for Game<'a> {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.viewport.write(buf)
    }

    fn flush(&mut self) -> io::Result<()> {
        self.viewport.flush()
    }

    fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
        self.viewport.write_all(buf)
    }
}

impl<'a> Positioned for Game<'a> {
    fn position(&self) -> Position {
        Position { x: 0, y: 0 }
    }
}