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/spidermite.hpp"
18
19#include "object/player.hpp"
20#include "sprite/sprite.hpp"
21
22static const float FLYTIME = 1.2f;
23static const float MOVE_SPEED = -100.0f;
24
25SpiderMite::SpiderMite(const ReaderMapping& reader) :
26 BadGuy(reader, "images/creatures/spidermite/spidermite.sprite"),
27 mode(),
28 timer()
29{
30 m_physic.enable_gravity(false);
31}
32
33void
34SpiderMite::initialize()
35{
36 m_sprite->set_action(m_dir == Direction::LEFT ? "left" : "right");
37 mode = FLY_UP;
38 m_physic.set_velocity_y(MOVE_SPEED);
39 timer.start(FLYTIME/2);
40}
41
42bool
43SpiderMite::collision_squished(GameObject& object)
44{
45 m_sprite->set_action(m_dir == Direction::LEFT ? "squished-left" : "squished-right");
46 kill_squished(object);
47 return true;
48}
49
50void
51SpiderMite::collision_solid(const CollisionHit& hit)
52{
53 if (hit.top || hit.bottom) { // hit floor or roof?
54 m_physic.set_velocity_y(0);
55 }
56}
57
58void
59SpiderMite::active_update(float dt_sec)
60{
61 if (m_frozen)
62 {
63 BadGuy::active_update(dt_sec);
64 return;
65 }
66 if (timer.check()) {
67 if (mode == FLY_UP) {
68 mode = FLY_DOWN;
69 m_physic.set_velocity_y(-MOVE_SPEED);
70 } else if (mode == FLY_DOWN) {
71 mode = FLY_UP;
72 m_physic.set_velocity_y(MOVE_SPEED);
73 }
74 timer.start(FLYTIME);
75 }
76 m_col.m_movement = m_physic.get_movement(dt_sec);
77
78 auto player = get_nearest_player();
79 if (player) {
80 m_dir = (player->get_pos().x > get_pos().x) ? Direction::RIGHT : Direction::LEFT;
81 m_sprite->set_action(m_dir == Direction::LEFT ? "left" : "right");
82 }
83}
84
85void
86SpiderMite::freeze()
87{
88 m_physic.enable_gravity(true);
89 BadGuy::freeze();
90}
91
92void
93SpiderMite::unfreeze()
94{
95 BadGuy::unfreeze();
96 m_physic.enable_gravity(false);
97 initialize();
98}
99
100bool
101SpiderMite::is_freezable() const
102{
103 return true;
104}
105
106/* EOF */
107