-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenemy.cpp
More file actions
167 lines (143 loc) · 5.8 KB
/
Copy pathenemy.cpp
File metadata and controls
167 lines (143 loc) · 5.8 KB
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
////////////////////////////////////////////////////////////////////////////////
// Distributed under the Boost Software License, Version 1.0. //
// (See accompanying file LICENSE or copy at //
// https://www.boost.org/LICENSE_1_0.txt) //
////////////////////////////////////////////////////////////////////////////////
#include "enemy.h"
#include <any>
#include <memory>
#include <string>
#include "iris/core/resource_loader.h"
#include "iris/core/vector3.h"
#include "iris/graphics/animation/animation_layer.h"
#include "iris/graphics/scene.h"
#include "iris/graphics/single_entity.h"
#include "iris/log/log.h"
#include "iris/physics/physics_system.h"
#include "iris/physics/rigid_body.h"
#include "iris/scripting/lua/lua_script.h"
#include "character_controller.h"
#include "message_type.h"
#include "player.h"
using namespace std::literals::chrono_literals;
namespace trinket
{
Enemy::Enemy(
iris::PhysicsSystem *ps,
const std::string &script_file,
iris::SingleEntity *render_entity,
iris::SingleEntity *health_bar,
std::vector<iris::Animation> animations,
const iris::Vector3 &bounds_min,
const iris::Vector3 &bounds_max,
const Player *player,
const ThirdPersonCamera *camera)
: script_(std::make_unique<iris::LuaScript>(script_file, iris::LuaScript::LoadFile{}))
, render_entity_(render_entity)
, health_bar_(health_bar)
, animation_controller_(nullptr)
, character_controller_(nullptr)
, player_(player)
, camera_(camera)
, hit_cooldown_(std::chrono::system_clock::now())
, health_bar_scale_(health_bar_->scale())
, health_(100.0f)
, is_dead_(false)
{
// call init of script
script_.execute("init", bounds_min, bounds_max);
// set death animation to not loop
auto find = std::find_if(std::begin(animations), std::end(animations), [](const iris::Animation &animation) {
return animation.name() == "Death_Back";
});
find->set_playback_type(iris::PlaybackType::SINGLE);
// create animation controller with required transitions
animation_controller_ = std::make_unique<iris::AnimationController>(
animations,
std::vector<iris::AnimationLayer>{
{{{"Walk", "Walk", 0ms},
{"Walk", "Bite_Front", 500ms},
{"Walk", "Death_Back", 500ms},
{"Bite_Front", "Death_Back", 500ms},
{"Death_Back", "Death_Back", 0ms},
{"Bite_Front", "Walk", 500ms},
{"Bite_Front", "Bite_Front", 0ms}},
"Walk"}},
render_entity_->skeleton());
character_controller_ = ps->create_character_controller<CharacterController>(ps, 1.0f, 1.0f, 0.5f, 2.0f);
character_controller_->reposition(render_entity_->position(), {});
subscribe(MessageType::WEAPON_COLLISION);
}
void Enemy::update(std::chrono::microseconds elapsed)
{
// if we are not dead then update
if (!is_dead_)
{
// call script update
script_.execute(
"update",
render_entity_->position(),
player_->position(),
static_cast<std::int32_t>(elapsed.count()),
health_);
static const iris::Vector3 offset{0.0f, -2.0f, 0.0f};
// update entity
character_controller_->set_movement_direction(script_.execute<iris::Vector3>("get_walk_direction"));
render_entity_->set_orientation(script_.execute<iris::Quaternion>("get_orientation"));
render_entity_->set_position(character_controller_->position() + offset);
// update billboard health bar
iris::Transform billboard_transform{iris::Matrix4::invert(camera_->camera()->view())};
billboard_transform.set_translation(render_entity_->position() + iris::Vector3{0.0f, 3.0f, 0.0f});
billboard_transform.set_scale(health_bar_scale_);
health_bar_->set_transform(billboard_transform.matrix());
// check if script wants us to update animation
if (const auto [change, animation] = script_.execute<bool, std::string>("get_animation_change"); change)
{
LOG_DEBUG("enemy", "new animation: {}", animation);
animation_controller_->play(0u, animation);
}
// if script attacks then send message
if (const auto attack = script_.execute<bool>("attack_player"); attack)
{
publish(MessageType::ENEMY_ATTACK, {1.0f});
}
// if we die then send message and update state
if (health_ <= 0.0f)
{
character_controller_->reposition(render_entity_->position(), {});
is_dead_ = true;
publish(MessageType::KILLED_ENEMY, {this});
}
}
animation_controller_->update();
}
void Enemy::handle_message(MessageType message_type, const std::any &data)
{
switch (message_type)
{
case MessageType::WEAPON_COLLISION:
{
// if we are hit by player (and not in a hit cooldown) then shunt us and decrement health
const auto &[body, pos] = std::any_cast<std::tuple<iris::RigidBody *, iris::Vector3>>(data);
if (body == character_controller_->rigid_body())
{
if (std::chrono::system_clock::now() > hit_cooldown_)
{
hit_cooldown_ = std::chrono::system_clock::now() + 500ms;
const auto shunt_dir =
iris::Vector3::normalise(character_controller_->position() - player_->position());
character_controller_->shunt(shunt_dir, 6.0, 200ms);
health_ -= 25.0f;
health_bar_scale_.x = 1.5f * (health_ / 100.0f);
}
break;
}
}
default: break;
}
}
iris::Vector3 Enemy::position() const
{
return render_entity_->position();
}
}