1// SuperTux
2// Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17#include "badguy/plant.hpp"
18
19#include "object/player.hpp"
20#include "sprite/sprite.hpp"
21
22static const float PLANT_SPEED = 80;
23static const float WAKE_TIME = .5;
24
25Plant::Plant(const ReaderMapping& reader) :
26 BadGuy(reader, "images/creatures/plant/plant.sprite"),
27 timer(),
28 state(PLANT_SLEEPING)
29{
30}
31
32void
33Plant::initialize()
34{
35 //FIXME: turns plant around for debugging
36 m_dir = m_dir == Direction::LEFT ? Direction::RIGHT : Direction::LEFT;
37
38 state = PLANT_SLEEPING;
39 m_physic.set_velocity_x(0);
40 m_sprite->set_action(m_dir == Direction::LEFT ? "sleeping-left" : "sleeping-right");
41}
42
43void
44Plant::collision_solid(const CollisionHit& hit)
45{
46 if (hit.top || hit.bottom) {
47 m_physic.set_velocity_y(0);
48 } else if (hit.left || hit.right) {
49 m_dir = m_dir == Direction::LEFT ? Direction::RIGHT : Direction::LEFT;
50 m_sprite->set_action(m_dir == Direction::LEFT ? "left" : "right");
51 m_physic.set_velocity_x(-m_physic.get_velocity_x());
52 }
53}
54
55HitResponse
56Plant::collision_badguy(BadGuy& , const CollisionHit& hit)
57{
58 if (state != PLANT_WALKING) return CONTINUE;
59
60 if (hit.left || hit.right) {
61 m_dir = m_dir == Direction::LEFT ? Direction::RIGHT : Direction::LEFT;
62 m_sprite->set_action(m_dir == Direction::LEFT ? "left" : "right");
63 m_physic.set_velocity_x(-m_physic.get_velocity_x());
64 }
65
66 return CONTINUE;
67}
68
69void
70Plant::active_update(float dt_sec) {
71 BadGuy::active_update(dt_sec);
72
73 if (state == PLANT_SLEEPING) {
74
75 auto player = get_nearest_player();
76 if (player) {
77 Rectf pb = player->get_bbox();
78
79 bool inReach_left = (pb.get_right() >= m_col.m_bbox.get_right()-((m_dir == Direction::LEFT) ? 256 : 0));
80 bool inReach_right = (pb.get_left() <= m_col.m_bbox.get_left()+((m_dir == Direction::RIGHT) ? 256 : 0));
81 bool inReach_top = (pb.get_bottom() >= m_col.m_bbox.get_bottom());
82 bool inReach_bottom = (pb.get_top() <= m_col.m_bbox.get_top());
83
84 if (inReach_left && inReach_right && inReach_top && inReach_bottom) {
85 // wake up
86 m_sprite->set_action(m_dir == Direction::LEFT ? "waking-left" : "waking-right");
87 if (!timer.started()) timer.start(WAKE_TIME);
88 state = PLANT_WAKING;
89 }
90 }
91 }
92
93 if (state == PLANT_WAKING) {
94 if (timer.check()) {
95 // start walking
96 m_sprite->set_action(m_dir == Direction::LEFT ? "left" : "right");
97 m_physic.set_velocity_x(m_dir == Direction::LEFT ? -PLANT_SPEED : PLANT_SPEED);
98 state = PLANT_WALKING;
99 }
100 }
101
102}
103
104void
105Plant::ignite()
106{
107 BadGuy::ignite();
108 if (state == PLANT_SLEEPING && m_sprite->has_action("sleeping-burning-left")) {
109 m_sprite->set_action(m_dir == Direction::LEFT ? "sleeping-burning-left" : "sleeping-burning-right", 1);
110 }
111}
112/* EOF */
113