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
//! Rotates a character towards a direction.

use amethyst::{
    core::Transform,
    ecs::{World, WorldExt},
};

use crate::{
    common::Direction,
    entities::{
        AnimationTable,
        character::{Character, CharacterAnimation, CharacterId, PlayerEntity},
    },
    map::{MapHandler, PlayerCoordinates},
};

use super::{BoxedGameEvent, ExecutionConditions, GameEvent};

#[derive(Clone)]
pub struct CharacterRotateEvent {
    character_id: CharacterId,
    direction_type: DirectionType,
}

#[derive(Clone)]
enum DirectionType {
    Fixed(Direction),
    TowardsPlayer,
}

impl DirectionType {
    fn get_direction(&self, world: &World, character_id: CharacterId) -> Option<Direction> {
        match self {
            DirectionType::Fixed(direction) => Some(direction.clone()),
            DirectionType::TowardsPlayer => {
                let npc_entity = world
                    .read_resource::<MapHandler>()
                    .get_character_by_id(character_id);

                let npc_position = world.read_storage::<Transform>()
                    .get(npc_entity)
                    .map(PlayerCoordinates::from_transform)
                    .unwrap();

                let player_entity = world.read_resource::<PlayerEntity>();

                let player_position = world.read_storage::<Transform>()
                        .get(player_entity.0)
                        .map(PlayerCoordinates::from_transform)
                        .unwrap();

                npc_position.get_direction_to(&player_position)
            }
        }
    }
}

impl CharacterRotateEvent {
    /// Instantiates a CharacterRotateEvent that rotates a character towards
    /// a given direction.
    pub fn new(character_id: CharacterId, direction: Direction) -> CharacterRotateEvent {
        CharacterRotateEvent {
            character_id,
            direction_type: DirectionType::Fixed(direction),
        }
    }

    /// Instantiates a CharacterRotateEvent that rotates a character towards
    /// the human player.
    pub fn towards_player(character_id: CharacterId) -> CharacterRotateEvent {
        CharacterRotateEvent {
            character_id,
            direction_type: DirectionType::TowardsPlayer,
        }
    }
}

impl GameEvent for CharacterRotateEvent {
    fn boxed_clone(&self) -> BoxedGameEvent {
        Box::new(self.clone())
    }

    fn get_execution_conditions(&self) -> ExecutionConditions {
        ExecutionConditions {
            requires_disabled_input: false,
        }
    }

    fn start(&mut self, _world: &mut World) { }

    fn tick(&mut self, world: &mut World, _disabled_inputs: bool) {
        let entity = world
            .read_resource::<MapHandler>()
            .get_character_by_id(self.character_id);

        if let Some(direction) = self.direction_type.get_direction(world, self.character_id) {
            world.write_storage::<AnimationTable<CharacterAnimation>>()
                .get_mut(entity)
                .unwrap()
                .change_animation(CharacterAnimation::Idle(
                    direction.clone(),
                ));

            world.write_storage::<Character>()
                .get_mut(entity)
                .unwrap()
                .facing_direction = direction;
        }
    }

    fn is_complete(&self, _world: &mut World) -> bool {
        true
    }
}