1// SuperTux - Ispy
2// Copyright (C) 2007 Christoph Sommer <christoph.sommer@2007.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 "object/ispy.hpp"
18
19#include "editor/editor.hpp"
20#include "sprite/sprite.hpp"
21#include "supertux/sector.hpp"
22#include "util/log.hpp"
23#include "util/reader_mapping.hpp"
24#include "util/writer.hpp"
25
26Ispy::Ispy(const ReaderMapping& reader) :
27 MovingSprite(reader, "images/objects/ispy/ispy.sprite", LAYER_TILES+5, COLGROUP_DISABLED),
28 state(ISPYSTATE_IDLE),
29 script(),
30 dir(Direction::AUTO),
31 m_facing_down(false)
32{
33 // read script to execute
34 reader.get("script", script);
35
36 // read direction to face in
37 std::string dir_str;
38 if (reader.get("direction", dir_str)) {
39 dir = string_to_dir(dir_str);
40 } else {
41 if (!Editor::is_active()) {
42 dir = Direction::LEFT;
43 }
44 }
45
46 reader.get("facing-down", m_facing_down, false);
47 if (!Editor::is_active()) {
48 if (m_facing_down) {
49 dir = Direction::DOWN;
50 }
51 }
52
53 if (dir == Direction::AUTO) {
54 log_warning << "Setting an Ispy's direction to AUTO is no good idea" << std::endl;
55 }
56
57 // set initial sprite action
58 m_sprite->set_action((dir == Direction::DOWN) ? "idle-down" : ((dir == Direction::LEFT) ? "idle-left" : "idle-right"));
59}
60
61ObjectSettings
62Ispy::get_settings()
63{
64 ObjectSettings result = MovingSprite::get_settings();
65
66 result.add_bool(_("Facing Down"), &m_facing_down, "facing-down", false);
67 result.add_script(_("Script"), &script, "script");
68 result.add_direction(_("Direction"), &dir, Direction::AUTO, "direction");
69
70 result.reorder({"script", "facing-down", "direction", "x", "y"});
71
72 return result;
73}
74
75void
76Ispy::after_editor_set()
77{
78 MovingSprite::after_editor_set();
79 m_sprite->set_action((dir == Direction::DOWN) ? "idle-down" : ((dir == Direction::LEFT) ? "idle-left" : "idle-right"));
80}
81
82HitResponse
83Ispy::collision(GameObject& , const CollisionHit& )
84{
85 return ABORT_MOVE;
86}
87
88void
89Ispy::update(float )
90{
91
92 if (state == ISPYSTATE_IDLE) {
93 // check if a player has been spotted
94 Vector eye = m_col.m_bbox.get_middle();
95 if (dir == Direction::LEFT) eye = Vector(m_col.m_bbox.get_left(), m_col.m_bbox.get_middle().y);
96 if (dir == Direction::RIGHT) eye = Vector(m_col.m_bbox.get_right(), m_col.m_bbox.get_middle().y);
97 if (dir == Direction::UP) eye = Vector(m_col.m_bbox.get_middle().x, m_col.m_bbox.get_top());
98 if (dir == Direction::DOWN) eye = Vector(m_col.m_bbox.get_middle().x, m_col.m_bbox.get_bottom());
99
100 if (Sector::get().can_see_player(eye)) {
101 m_sprite->set_action((dir == Direction::DOWN) ? "alert-down" : ((dir == Direction::LEFT) ? "alert-left" : "alert-right"), 1);
102 state = ISPYSTATE_ALERT;
103
104 Sector::get().run_script(script, "Ispy");
105 }
106 }
107 if (state == ISPYSTATE_ALERT) {
108 if (m_sprite->animation_done()) {
109 m_sprite->set_action((dir == Direction::DOWN) ? "hiding-down" : ((dir == Direction::LEFT) ? "hiding-left" : "hiding-right"), 1);
110 state = ISPYSTATE_HIDING;
111 }
112 }
113 if (state == ISPYSTATE_HIDING) {
114 if (m_sprite->animation_done()) {
115 m_sprite->set_action((dir == Direction::DOWN) ? "showing-down" : ((dir == Direction::LEFT) ? "showing-left" : "showing-right"), 1);
116 state = ISPYSTATE_SHOWING;
117 }
118 }
119 if (state == ISPYSTATE_SHOWING) {
120 if (m_sprite->animation_done()) {
121 m_sprite->set_action((dir == Direction::DOWN) ? "idle-down" : ((dir == Direction::LEFT) ? "idle-left" : "idle-right"));
122 state = ISPYSTATE_IDLE;
123 }
124 }
125}
126
127/* EOF */
128