1 /* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
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 "neverhood/entity.h"
24 #include "neverhood/sound.h"
25
26 namespace Neverhood {
27
asInteger() const28 uint32 MessageParam::asInteger() const {
29 assert(_type == mptInteger);
30 return _integer;
31 }
32
asPoint() const33 NPoint MessageParam::asPoint() const {
34 assert(_type == mptInteger || _type == mptPoint);
35 if (_type == mptInteger) {
36 NPoint pt;
37 pt.x = _integer & 0xFFFF;
38 pt.y = (_integer >> 16) & 0xFFFF;
39 return pt;
40 }
41 return _point;
42 }
43
asEntity() const44 Entity *MessageParam::asEntity() const {
45 assert(_type == mptEntity);
46 return _entity;
47 }
48
Entity(NeverhoodEngine * vm,int priority)49 Entity::Entity(NeverhoodEngine *vm, int priority)
50 : _vm(vm), _updateHandlerCb(NULL), _messageHandlerCb(nullptr), _priority(priority), _soundResources(NULL) {
51 }
52
~Entity()53 Entity::~Entity() {
54 deleteSoundResources();
55 }
56
draw()57 void Entity::draw() {
58 // Empty
59 }
60
handleUpdate()61 void Entity::handleUpdate() {
62 debug(5, "handleUpdate() -> [%s]", _updateHandlerCbName.c_str());
63 if (_updateHandlerCb)
64 (this->*_updateHandlerCb)();
65 }
66
receiveMessage(int messageNum,const MessageParam & param,Entity * sender)67 uint32 Entity::receiveMessage(int messageNum, const MessageParam ¶m, Entity *sender) {
68 debug(5, "receiveMessage(%04X) -> [%s]", messageNum, _messageHandlerCbName.c_str());
69 return _messageHandlerCb ? (this->*_messageHandlerCb)(messageNum, param, sender) : 0;
70 }
71
sendMessage(Entity * receiver,int messageNum,const MessageParam & param)72 uint32 Entity::sendMessage(Entity *receiver, int messageNum, const MessageParam ¶m) {
73 return receiver ? receiver->receiveMessage(messageNum, param, this) : 0;
74 }
75
sendMessage(Entity * receiver,int messageNum,uint32 param)76 uint32 Entity::sendMessage(Entity *receiver, int messageNum, uint32 param) {
77 return sendMessage(receiver, messageNum, MessageParam(param));
78 }
79
sendPointMessage(Entity * receiver,int messageNum,const NPoint & param)80 uint32 Entity::sendPointMessage(Entity *receiver, int messageNum, const NPoint ¶m) {
81 return sendMessage(receiver, messageNum, MessageParam(param));
82 }
83
sendEntityMessage(Entity * receiver,int messageNum,Entity * param)84 uint32 Entity::sendEntityMessage(Entity *receiver, int messageNum, Entity *param) {
85 return sendMessage(receiver, messageNum, MessageParam((Entity*)param));
86 }
87
getGlobalVar(uint32 nameHash)88 uint32 Entity::getGlobalVar(uint32 nameHash) {
89 return _vm->_gameVars->getGlobalVar(nameHash);
90 }
91
setGlobalVar(uint32 nameHash,uint32 value)92 void Entity::setGlobalVar(uint32 nameHash, uint32 value) {
93 _vm->_gameVars->setGlobalVar(nameHash, value);
94 }
95
getSubVar(uint32 nameHash,uint32 subNameHash)96 uint32 Entity::getSubVar(uint32 nameHash, uint32 subNameHash) {
97 return _vm->_gameVars->getSubVar(nameHash, subNameHash);
98 }
99
setSubVar(uint32 nameHash,uint32 subNameHash,uint32 value)100 void Entity::setSubVar(uint32 nameHash, uint32 subNameHash, uint32 value) {
101 _vm->_gameVars->setSubVar(nameHash, subNameHash, value);
102 }
103
incGlobalVar(uint32 nameHash,int incrValue)104 void Entity::incGlobalVar(uint32 nameHash, int incrValue) {
105 setGlobalVar(nameHash, getGlobalVar(nameHash) + incrValue);
106 }
107
incSubVar(uint32 nameHash,uint32 subNameHash,int incrValue)108 void Entity::incSubVar(uint32 nameHash, uint32 subNameHash, int incrValue) {
109 setSubVar(nameHash, subNameHash, getSubVar(nameHash, subNameHash) + incrValue);
110 }
111
getSoundResource(uint index)112 SoundResource *Entity::getSoundResource(uint index) {
113 assert(index < kMaxSoundResources);
114 if (!_soundResources) {
115 _soundResources = new SoundResource*[kMaxSoundResources];
116 for (uint i = 0; i < kMaxSoundResources; ++i)
117 _soundResources[i] = NULL;
118 }
119 if (!_soundResources[index])
120 _soundResources[index] = new SoundResource(_vm);
121 return _soundResources[index];
122 }
123
loadSound(uint index,uint32 fileHash)124 void Entity::loadSound(uint index, uint32 fileHash) {
125 getSoundResource(index)->load(fileHash);
126 }
127
playSound(uint index,uint32 fileHash)128 void Entity::playSound(uint index, uint32 fileHash) {
129 if (fileHash)
130 getSoundResource(index)->play(fileHash);
131 else
132 getSoundResource(index)->play();
133 }
134
stopSound(uint index)135 void Entity::stopSound(uint index) {
136 getSoundResource(index)->stop();
137 }
138
isSoundPlaying(uint index)139 bool Entity::isSoundPlaying(uint index) {
140 return getSoundResource(index)->isPlaying();
141 }
142
setSoundVolume(uint index,int volume)143 void Entity::setSoundVolume(uint index, int volume) {
144 getSoundResource(index)->setVolume(volume);
145 }
146
setSoundPan(uint index,int pan)147 void Entity::setSoundPan(uint index, int pan) {
148 getSoundResource(index)->setPan(pan);
149 }
150
deleteSoundResources()151 void Entity::deleteSoundResources() {
152 if (_soundResources) {
153 for (uint i = 0; i < kMaxSoundResources; ++i)
154 delete _soundResources[i];
155 delete[] _soundResources;
156 }
157 }
158
159 } // End of namespace Neverhood
160