File: BattleObstacleController.cpp

package info (click to toggle)
vcmi 1.6.5%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 32,060 kB
  • sloc: cpp: 238,971; python: 265; sh: 224; xml: 157; ansic: 78; objc: 61; makefile: 49
file content (222 lines) | stat: -rw-r--r-- 6,803 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 * BattleObstacleController.cpp, part of VCMI engine
 *
 * Authors: listed in file AUTHORS in main folder
 *
 * License: GNU General Public License v2.0 or later
 * Full text of license available in license.txt file, in main folder
 *
 */
#include "StdInc.h"
#include "BattleObstacleController.h"

#include "BattleInterface.h"
#include "BattleFieldController.h"
#include "BattleAnimationClasses.h"
#include "BattleStacksController.h"
#include "BattleRenderer.h"
#include "CreatureAnimation.h"

#include "../CGameInfo.h"
#include "../CPlayerInterface.h"
#include "../gui/CGuiHandler.h"
#include "../media/ISoundPlayer.h"
#include "../render/CAnimation.h"
#include "../render/Canvas.h"
#include "../render/IRenderHandler.h"

#include "../../CCallback.h"
#include "../../lib/battle/CObstacleInstance.h"
#include "../../lib/ObstacleHandler.h"
#include "../../lib/serializer/JsonDeserializer.h"

BattleObstacleController::BattleObstacleController(BattleInterface & owner):
	owner(owner),
	timePassed(0.f)
{
	auto obst = owner.getBattle()->battleGetAllObstacles();
	for(auto & elem : obst)
	{
		if ( elem->obstacleType == CObstacleInstance::MOAT )
			continue; // handled by siege controller;
		loadObstacleImage(*elem);
	}
}

void BattleObstacleController::loadObstacleImage(const CObstacleInstance & oi)
{
	AnimationPath animationName = oi.getAnimation();

	if (oi.obstacleType == CObstacleInstance::ABSOLUTE_OBSTACLE)
	{
		// obstacle uses single bitmap image for animations
		obstacleImages[oi.uniqueID] = GH.renderHandler().loadImage(animationName.toType<EResType::IMAGE>(), EImageBlitMode::SIMPLE);
	}
	else
	{
		obstacleAnimations[oi.uniqueID] = GH.renderHandler().loadAnimation(animationName, EImageBlitMode::SIMPLE);
		obstacleImages[oi.uniqueID] = obstacleAnimations[oi.uniqueID]->getImage(0);
	}
}

void BattleObstacleController::obstacleRemoved(const std::vector<ObstacleChanges> & obstacles)
{
	for(const auto & oi : obstacles)
	{
		auto & obstacle = oi.data["obstacle"];

		if (!obstacle.isStruct())
		{
			logGlobal->error("I don't know how to animate removal of this obstacle");
			continue;
		}

		AnimationPath animationPath;
		JsonDeserializer ser(nullptr, obstacle);
		ser.serializeStruct("appearAnimation", animationPath);

		if(animationPath.empty())
			continue;

		auto animation = GH.renderHandler().loadAnimation(animationPath, EImageBlitMode::SIMPLE);
		auto first = animation->getImage(0, 0);
		if(!first)
			continue;

		//we assume here that effect graphics have the same size as the usual obstacle image
		// -> if we know how to blit obstacle, let's blit the effect in the same place
		Point whereTo = getObstaclePosition(first, obstacle);
		//AFAIK, in H3 there is no sound of obstacle removal
		owner.stacksController->addNewAnim(new EffectAnimation(owner, animationPath, whereTo, obstacle["position"].Integer(), 0, true));

		obstacleAnimations.erase(oi.id);
		obstacleImages.erase(oi.id);
		//so when multiple obstacles are removed, they show up one after another
		owner.waitForAnimations();
	}
}

void BattleObstacleController::obstaclePlaced(const std::vector<std::shared_ptr<const CObstacleInstance>> & obstacles)
{
	for(const auto & oi : obstacles)
	{
		auto side = owner.getBattle()->playerToSide(owner.curInt->playerID);

		if(!oi->visibleForSide(side, owner.getBattle()->battleHasNativeStack(side)))
			continue;

		auto animation = GH.renderHandler().loadAnimation(oi->getAppearAnimation(), EImageBlitMode::SIMPLE);
		auto first = animation->getImage(0, 0);
		if(!first)
			continue;

		//we assume here that effect graphics have the same size as the usual obstacle image
		// -> if we know how to blit obstacle, let's blit the effect in the same place
		Point whereTo = getObstaclePosition(first, *oi);
		CCS->soundh->playSound( oi->getAppearSound() );
		owner.stacksController->addNewAnim(new EffectAnimation(owner, oi->getAppearAnimation(), whereTo, oi->pos));

		//so when multiple obstacles are added, they show up one after another
		owner.waitForAnimations();

		loadObstacleImage(*oi);
	}
}

void BattleObstacleController::showAbsoluteObstacles(Canvas & canvas)
{
	//Blit absolute obstacles
	for(auto & obstacle : owner.getBattle()->battleGetAllObstacles())
	{
		if(obstacle->obstacleType == CObstacleInstance::ABSOLUTE_OBSTACLE)
		{
			auto img = getObstacleImage(*obstacle);
			if(img)
				canvas.draw(img, Point(obstacle->getInfo().width, obstacle->getInfo().height));
		}

		if (obstacle->obstacleType == CObstacleInstance::USUAL)
		{
			if (obstacle->getInfo().isForegroundObstacle)
				continue;

			auto img = getObstacleImage(*obstacle);
			if(img)
			{
				Point p = getObstaclePosition(img, *obstacle);
				canvas.draw(img, p);
			}
		}
	}
}

void BattleObstacleController::collectRenderableObjects(BattleRenderer & renderer)
{
	for (auto obstacle : owner.getBattle()->battleGetAllObstacles())
	{
		if (obstacle->obstacleType == CObstacleInstance::ABSOLUTE_OBSTACLE)
			continue;

		if (obstacle->obstacleType == CObstacleInstance::MOAT)
			continue;

		if (obstacle->obstacleType == CObstacleInstance::USUAL && !obstacle->getInfo().isForegroundObstacle)
			continue;

		renderer.insert(EBattleFieldLayer::OBSTACLES, obstacle->pos, [this, obstacle]( BattleRenderer::RendererRef canvas ){
			auto img = getObstacleImage(*obstacle);
			if(img)
			{
				Point p = getObstaclePosition(img, *obstacle);
				canvas.draw(img, p);
			}
		});
	}
}

void BattleObstacleController::tick(uint32_t msPassed)
{
	timePassed += msPassed / 1000.f;
	int framesCount = timePassed * AnimationControls::getObstaclesSpeed();

	for(auto & animation : obstacleAnimations)
	{
		int frameIndex = framesCount % animation.second->size(0);
		obstacleImages[animation.first] = animation.second->getImage(frameIndex, 0);
	}
}

std::shared_ptr<IImage> BattleObstacleController::getObstacleImage(const CObstacleInstance & oi)
{
	// obstacle is not loaded yet, don't show anything
	if (obstacleImages.count(oi.uniqueID) == 0)
		return nullptr;

	return obstacleImages[oi.uniqueID];
}

Point BattleObstacleController::getObstaclePosition(std::shared_ptr<IImage> image, const CObstacleInstance & obstacle)
{
	int offset = obstacle.getAnimationYOffset(image->height());

	Rect r = owner.fieldController->hexPositionLocal(obstacle.pos);
	r.y += 42 - image->height() + offset;

	return r.topLeft();
}

Point BattleObstacleController::getObstaclePosition(std::shared_ptr<IImage> image, const JsonNode & obstacle)
{
	auto animationYOffset = obstacle["animationYOffset"].Integer();
	auto offset = image->height() % 42;

	if(obstacle["needAnimationOffsetFix"].Bool() && offset > 37)
		animationYOffset -= 42;

	offset += animationYOffset;

	Rect r = owner.fieldController->hexPositionLocal(obstacle["position"].Integer());
	r.y += 42 - image->height() + offset;

	return r.topLeft();
}