20
20
#include < math.h>
21
21
22
22
#include " audio/sound_manager.hpp"
23
+ #include " badguy/ghosttree_attack.hpp"
23
24
#include " badguy/root.hpp"
24
25
#include " badguy/treewillowisp.hpp"
25
26
#include " math/random.hpp"
@@ -57,7 +58,8 @@ GhostTree::GhostTree(const ReaderMapping& mapping) :
57
58
suck_lantern_color(),
58
59
m_taking_life(),
59
60
suck_lantern(nullptr),*/
60
- m_willowisps()
61
+ m_willowisps(),
62
+ m_root_attack()
61
63
{
62
64
mapping.get (" hud-icon" , m_hud_icon, " images/creatures/ghosttree/hudlife.png" );
63
65
m_hud_head = Surface::from_file (m_hud_icon);
@@ -69,6 +71,11 @@ GhostTree::GhostTree(const ReaderMapping& mapping) :
69
71
set_state (STATE_INIT);
70
72
}
71
73
74
+ GhostTree::~GhostTree ()
75
+ {
76
+
77
+ }
78
+
72
79
void
73
80
GhostTree::die ()
74
81
{
@@ -93,6 +100,17 @@ GhostTree::activate()
93
100
root_timer.start(5, true);*/
94
101
}
95
102
103
+ Vector
104
+ GhostTree::get_player_pos ()
105
+ {
106
+ auto player = get_nearest_player ();
107
+ if (player) {
108
+ return player->get_pos ();
109
+ } else {
110
+ return m_col.m_bbox .get_middle ();
111
+ }
112
+ }
113
+
96
114
void
97
115
GhostTree::active_update (float dt_sec)
98
116
{
@@ -122,7 +140,8 @@ GhostTree::active_update(float dt_sec)
122
140
case STATE_SUCKING:
123
141
break ;
124
142
case STATE_ATTACKING:
125
- if (m_state_timer.check ()) {
143
+ m_root_attack->active_update (dt_sec);
144
+ if (m_root_attack->is_done ()) {
126
145
set_state (STATE_RECHARGING);
127
146
}
128
147
break ;
@@ -255,7 +274,8 @@ GhostTree::active_update(float dt_sec)
255
274
}*/
256
275
}
257
276
258
- bool GhostTree::suck_now (const Color& color) const {
277
+ bool
278
+ GhostTree::suck_now (const Color& color) const {
259
279
switch (m_attack) {
260
280
case ATTACK_RED:
261
281
return color.red == 1.0 ;
@@ -349,8 +369,7 @@ GhostTree::set_state(MyState new_state) {
349
369
std::cout<<" attacking" <<std::endl;
350
370
set_action (m_attack == ATTACK_PINCH ? " scream-pinch" : " scream" );
351
371
SoundManager::current ()->play (" sounds/tree_howling.ogg" , get_pos ());
352
- m_state_timer.start (5 );
353
- // TODO
372
+ start_attack ();
354
373
break ;
355
374
case STATE_RECHARGING:
356
375
std::cout<<" recharging" <<std::endl;
@@ -369,6 +388,32 @@ GhostTree::set_state(MyState new_state) {
369
388
m_state = new_state;
370
389
}
371
390
391
+ void
392
+ GhostTree::start_attack ()
393
+ {
394
+ Vector player_pos = get_player_pos ();
395
+ const float middle = m_col.m_bbox .get_middle ().x ;
396
+ const float base = m_col.m_bbox .get_bottom () + 64 ;
397
+ switch (m_attack) {
398
+ case ATTACK_RED:
399
+ if (player_pos.x > middle) {
400
+ m_root_attack.reset (new GhostTreeAttackRed (base, middle - 512 , middle + 512 ));
401
+ } else {
402
+ m_root_attack.reset (new GhostTreeAttackRed (base, middle + 512 , middle - 512 ));
403
+ }
404
+ break ;
405
+ case ATTACK_GREEN:
406
+ m_root_attack.reset (new GhostTreeAttackGreen (Vector (player_pos.x , base)));
407
+ break ;
408
+ case ATTACK_BLUE:
409
+ m_root_attack.reset (new GhostTreeAttackBlue (Vector (player_pos.x , base)));
410
+ break ;
411
+ case ATTACK_PINCH:
412
+ default :
413
+ m_root_attack.reset (new GhostTreeAttackPinch (Vector (player_pos.x , base), middle - 512 , middle + 512 ));
414
+ break ;
415
+ }
416
+ }
372
417
/* bool
373
418
GhostTree::is_color_deadly(Color color) const
374
419
{
0 commit comments