1// SuperTux - Badguy "Igel"
2// Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.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/igel.hpp"
18
19#include "object/bullet.hpp"
20#include "supertux/sector.hpp"
21
22namespace {
23
24const float IGEL_SPEED = 80; /**< speed at which we walk around */
25const float TURN_RECOVER_TIME = 0.5; /**< seconds before we will again turn around when shot at */
26const float RANGE_OF_VISION = 256; /**< range in px at which we can see bullets */
27
28} // namespace
29
30Igel::Igel(const ReaderMapping& reader) :
31 WalkingBadguy(reader, "images/creatures/igel/igel.sprite", "walking-left", "walking-right"),
32 turn_recover_timer()
33{
34 walk_speed = IGEL_SPEED;
35 max_drop_height = 16;
36}
37
38void
39Igel::be_normal()
40{
41 initialize();
42}
43
44void
45Igel::turn_around()
46{
47 WalkingBadguy::turn_around();
48 turn_recover_timer.start(TURN_RECOVER_TIME);
49}
50
51bool
52Igel::can_see(const MovingObject& o) const
53{
54 Rectf ob = o.get_bbox();
55
56 bool inReach_left = ((ob.get_right() < m_col.m_bbox.get_left()) && (ob.get_right() >= m_col.m_bbox.get_left()-((m_dir == Direction::LEFT) ? RANGE_OF_VISION : 0)));
57 bool inReach_right = ((ob.get_left() > m_col.m_bbox.get_right()) && (ob.get_left() <= m_col.m_bbox.get_right()+((m_dir == Direction::RIGHT) ? RANGE_OF_VISION : 0)));
58 bool inReach_top = (ob.get_bottom() >= m_col.m_bbox.get_top());
59 bool inReach_bottom = (ob.get_top() <= m_col.m_bbox.get_bottom());
60
61 return ((inReach_left || inReach_right) && inReach_top && inReach_bottom);
62}
63
64void
65Igel::active_update(float dt_sec)
66{
67 bool wants_to_flee = false;
68
69 // check if we see a fire bullet
70 for (const auto& bullet : Sector::get().get_objects_by_type<Bullet>()) {
71 if (bullet.get_type() != FIRE_BONUS) continue;
72 if (can_see(bullet)) wants_to_flee = true;
73 }
74
75 // if we flee, handle this ourselves
76 if (wants_to_flee && (!turn_recover_timer.started())) {
77 turn_around();
78 BadGuy::active_update(dt_sec);
79 return;
80 }
81
82 // else adhere to default behaviour
83 WalkingBadguy::active_update(dt_sec);
84}
85
86HitResponse
87Igel::collision_bullet(Bullet& bullet, const CollisionHit& hit)
88{
89 // default reaction if hit on front side or for freeze and unfreeze
90 if (((m_dir == Direction::LEFT) && hit.left) || ((m_dir == Direction::RIGHT) && hit.right) ||
91 (bullet.get_type() == ICE_BONUS) || ((bullet.get_type() == FIRE_BONUS) && (m_frozen))) {
92 return BadGuy::collision_bullet(bullet, hit);
93 }
94
95 // else make bullet ricochet and ignore the hit
96 bullet.ricochet(*this, hit);
97 return FORCE_MOVE;
98}
99
100bool
101Igel::is_freezable() const
102{
103 return true;
104}
105
106/**bool
107Igel::collision_squished(GameObject& )
108{
109 // this will hurt
110 return false;
111}*/
112// Enable this and the igle will no longer be butt-jumpable.
113// Don't forget to enable it in .hpp too!
114
115/* EOF */
116