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
use amethyst::ecs::{Component, DenseVecStorage, Entity, World};

use crate::{
    common::Direction,
    entities::character::CharacterId,
    events::ScriptEvent,
};

use serde::{Deserialize, Serialize};

use super::{
    conversions::{map_to_world_coordinates, player_to_map_coordinates},
    MapCoordinates,
    MapId,
    PlayerCoordinates,
    serializable_map::InitializedMap,
    WorldCoordinates,
};

use std::{
    collections::HashMap,
    fmt::{Debug, Error, Formatter},
};

pub struct Map {
    pub(super) map_id: MapId,
    pub(super) map_name: String,
    /**
     * The Reference Point of this map, which corresponds to the coordinates of
     * its bottom-left corner.
     */
    pub(super) reference_point: WorldCoordinates,
    pub(super) terrain_entity: Entity,
    pub(super) solids: HashMap<MapCoordinates, Tile>,
    pub(super) decoration_entity: Entity,
    pub script_repository: Vec<GameScript>,
    pub actions: HashMap<MapCoordinates, GameAction>,
    pub(super) map_scripts: Vec<MapScript>,
    pub(super) connections: HashMap<MapCoordinates, MapConnection>,
}

impl Component for Map {
    type Storage = DenseVecStorage<Self>;
}

impl Map {
    pub(super) fn from_initialized_map(map: InitializedMap) -> Map {
        Map {
            map_id: map.map_id,
            map_name: map.map_name,
            reference_point: map.reference_point,
            terrain_entity: map.terrain_entity,
            solids: map.solids
                .into_iter()
                .map(|tile_position| (MapCoordinates::from_tuple(&tile_position), Tile))
                .collect(),
            decoration_entity: map.decoration_entity,
            script_repository: map.script_repository
                .into_iter()
                .map(Into::into)
                .collect(),
            actions: map.actions
                .into_iter()
                .map(|(tile_position, action)| (MapCoordinates::from_tuple(&tile_position), action))
                .collect(),
            map_scripts: map.map_scripts,
            connections: map.connections
                .into_iter()
                .map(|(tile_position, connection)| {
                    (
                        MapCoordinates::from_tuple(&tile_position),
                        MapConnection {
                            map: connection.map,
                            directions: connection.directions
                                .into_iter()
                                .map(|(direction, coordinates)| {
                                    (direction, MapCoordinates::from_tuple(&coordinates))
                                })
                                .collect(),
                        },
                    )
                })
                .collect(),
        }
    }

    pub(super) fn map_to_world_coordinates(&self, position: &MapCoordinates) -> WorldCoordinates {
        map_to_world_coordinates(&position, &self.reference_point)
    }

    pub(super) fn player_to_map_coordinates(&self, position: &PlayerCoordinates) -> MapCoordinates {
        player_to_map_coordinates(&position, &self.reference_point)
    }

    pub(super) fn is_tile_blocked(&self, position: &PlayerCoordinates) -> bool {
        let tile = self.player_to_map_coordinates(&position);
        self.solids.contains_key(&tile)
    }

    pub(super) fn get_map_scripts<'a>(
        &'a self,
        kind: MapScriptKind,
    ) -> impl Iterator<Item = ScriptEvent> + 'a {
        self.map_scripts
            .iter()
            .filter(move |script| script.when == kind)
            .map(move |script| ScriptEvent::new(self.map_id.clone(), script.script_index))
    }
}

#[derive(Clone, Default)]
pub struct Tile;

impl Component for Tile {
    type Storage = DenseVecStorage<Self>;
}

#[derive(Clone)]
pub enum GameScript {
    Native {
        script: fn(&mut World, &Option<GameScriptParameters>) -> (),
        parameters: Option<GameScriptParameters>,
    },
    Lua {
        file: String,
        function: String,
        parameters: Option<GameScriptParameters>,
    },
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum GameScriptParameters {
    SourceTile(MapCoordinates),
    TargetCharacter(CharacterId),
    SourceMap(String),
}

impl Debug for GameScript {
    fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error> {
        match self {
            GameScript::Native { .. } => write!(formatter, "Native Script"),
            GameScript::Lua { file, function, parameters } => {
                write!(formatter, "Lua Script({}, {}, {:?})", file, function, parameters)
            },
        }
    }
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct GameAction {
    pub when: GameActionKind,
    pub script_index: usize,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub enum GameActionKind {
    /**
     * Triggered when the player presses Z on a tile.
     */
    OnInteraction,
    /**
     * Triggered after the player steps on a tile.
     */
    OnStep,
    /**
     * Triggered when the player tries to step on a tile,
     * _before_ actually stepping on it (e.g doors, sign posts).
     */
    OnStepAttempt,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct MapScript {
    pub when: MapScriptKind,
    pub script_index: usize,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub enum MapScriptKind {
    /// Triggered when the map loads.
    OnMapLoad,
    /// Triggered when the player steps on a new tile.
    OnTileChange,
    /// Triggered when the player enters in this map.
    OnMapEnter,
}

#[derive(Clone)]
pub struct MapConnection {
    // TODO: maybe pub is ok
    pub(super) map: String,
    pub(super) directions: HashMap<Direction, MapCoordinates>,
}