1 /* ResidualVM - A 3D game interpreter
2  *
3  * ResidualVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the AUTHORS
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "engines/stark/movement/followpathlight.h"
24 
25 #include "engines/stark/services/global.h"
26 #include "engines/stark/services/services.h"
27 #include "engines/stark/services/stateprovider.h"
28 
29 #include "engines/stark/resources/anim.h"
30 #include "engines/stark/resources/floor.h"
31 #include "engines/stark/resources/item.h"
32 #include "engines/stark/resources/light.h"
33 #include "engines/stark/resources/path.h"
34 
35 namespace Stark {
36 
FollowPathLight(Resources::ItemVisual * item)37 FollowPathLight::FollowPathLight(Resources::ItemVisual *item) :
38 		Movement(item),
39 		_light(nullptr),
40 		_path(nullptr),
41 		_speed(0.0),
42 		_position(0.0),
43 		_previouslyEnabled(true) {
44 }
45 
~FollowPathLight()46 FollowPathLight::~FollowPathLight() {
47 }
48 
start()49 void FollowPathLight::start() {
50 	Movement::start();
51 
52 	_previouslyEnabled = _item->isEnabled();
53 	_item->setEnabled(true);
54 
55 	Math::Vector3d newPosition = _path->getWeightedPositionInEdge(0, 0);
56 	_light->setPosition(newPosition);
57 }
58 
stop()59 void FollowPathLight::stop() {
60 	Movement::stop();
61 
62 	_item->setEnabled(_previouslyEnabled);
63 }
64 
onGameLoop()65 void FollowPathLight::onGameLoop() {
66 	// Compute the new position on the path
67 	_position += _speed * StarkGlobal->getMillisecondsPerGameloop();
68 
69 	// Find the current path edge, and position on the path edge
70 	uint currentEdge = 0;
71 	float positionInEdge = _position;
72 	for (uint i = 0; i < _path->getEdgeCount(); i++) {
73 		float edgeLength = _path->getWeightedEdgeLength(i);
74 		if (positionInEdge < edgeLength) {
75 			break; // Found the current path edge
76 		}
77 
78 		positionInEdge -= edgeLength;
79 		currentEdge++;
80 	}
81 
82 	// Check if we went beyond the path's end
83 	if (currentEdge >= _path->getEdgeCount()) {
84 		stop();
85 		return;
86 	}
87 
88 	// Set the new position for the light
89 	Math::Vector3d newPosition = _path->getWeightedPositionInEdge(currentEdge, positionInEdge);
90 	_light->setPosition(newPosition);
91 }
92 
setPath(Resources::Path * path)93 void FollowPathLight::setPath(Resources::Path *path) {
94 	_path = path;
95 }
96 
setSpeed(float speed)97 void FollowPathLight::setSpeed(float speed) {
98 	_speed = speed;
99 }
100 
setLight(Resources::Light * light)101 void FollowPathLight::setLight(Resources::Light *light) {
102 	_light = light;
103 }
104 
getType() const105 uint32 FollowPathLight::getType() const {
106 	return kTypeFollowPathLight;
107 }
108 
saveLoad(ResourceSerializer * serializer)109 void FollowPathLight::saveLoad(ResourceSerializer *serializer) {
110 	serializer->syncAsResourceReference(&_path);
111 	serializer->syncAsResourceReference(&_light);
112 	serializer->syncAsFloat(_position);
113 	serializer->syncAsFloat(_speed);
114 	serializer->syncAsUint32LE(_previouslyEnabled);
115 }
116 
117 } // End of namespace Stark
118