File: level_collide.cpp

package info (click to toggle)
scummvm 2.9.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 450,268 kB
  • sloc: cpp: 4,297,604; asm: 28,322; python: 12,901; sh: 11,219; java: 8,477; xml: 7,843; perl: 2,633; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (149 lines) | stat: -rw-r--r-- 5,252 bytes parent folder | download | duplicates (2)
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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/*
 * This code is based on the CRAB engine
 *
 * Copyright (c) Arvind Raja Yadav
 *
 * Licensed under MIT
 *
 */

#include "crab/input/cursor.h"
#include "crab/level/level.h"

namespace Crab {

using namespace TMX;
using namespace pyrodactyl::stat;
using namespace pyrodactyl::anim;
using namespace pyrodactyl::level;
using namespace pyrodactyl::image;
using namespace pyrodactyl::people;
using namespace pyrodactyl::input;
using namespace pyrodactyl::event;
using namespace pyrodactyl::music;

//------------------------------------------------------------------------
// Purpose: See if player clicked on a sprite they are colliding with
//------------------------------------------------------------------------
bool Level::containsClick(const Common::String &id, const Common::Event &event) {
	// If mouse is moved and is hovering on the specified sprite (id), set hover = true
	if (event.type == Common::EVENT_MOUSEMOVE) {
		for (auto &i : _objects) {
			Rect r = i.posRect();

			if (r.contains(g_engine->_mouse->_motion.x + _camera.x, g_engine->_mouse->_motion.y + _camera.y)) {
				// This is to show the sprite's name on top of their head
				i._hover = true;

				// This is to update the mouse cursor only if we're in talking range of a sprite
				if (i.id() == id)
					g_engine->_mouse->_hover = true;
			} else
				i._hover = false;
		}

		return false;
	} else if (g_engine->_mouse->pressed()) {
		for (auto &i : _objects) {
			if (i.id() == id) {
				Rect r = i.posRect();
				if (r.contains(g_engine->_mouse->_button.x + _camera.x, g_engine->_mouse->_button.y + _camera.y)) {
					g_engine->_mouse->_hover = true;
					return true;
				}
			}
		}
	}

	return false;
}

//------------------------------------------------------------------------
// Purpose: Find if a layer is visible (used only for objects with layers associated with them)
//------------------------------------------------------------------------
bool Level::layerVisible(pyrodactyl::anim::Sprite *obj) {
	if (obj->_layer < 0)
		return true;

	if ((uint)obj->_layer < _terrain._layer.size())
		return _terrain._layer[obj->_layer]._collide;

	return false;
}

//------------------------------------------------------------------------
// Purpose: Check if a sprite is colliding with the trigger areas
// Common::String &id is set to the id of colliding object
//------------------------------------------------------------------------
void Level::calcTrigCollide(pyrodactyl::event::Info &info) {
	for (auto &i : _objects)
		if (info.personValid(i.id()))
			_terrain.collideWithTrigger(i.boundRect(), info.personGet(i.id())._trig);
}

//------------------------------------------------------------------------
// Purpose: Check if a sprite is colliding with interactive objects
// Common::String &id is set to the id of colliding object
//------------------------------------------------------------------------
bool Level::collidingWithObject(pyrodactyl::event::Info &info, Common::String &id) {
	// Clip and Bounding rectangle of player
	Rect pPos = _objects[_playerIndex].posRect(), pBound = _objects[_playerIndex].boundRect();

	uint index = 0;
	for (auto i = _objects.begin(); i != _objects.end(); ++i, ++index) {
		if (i->visible() && _playerIndex != index && info.state(i->id()) == PST_NORMAL) {
			// Clip and bounding rectangles for the NPC sprite
			Rect iPos = i->posRect(), iBound = i->boundRect();

			if (pPos.collide(iPos) || iPos.contains(pPos) || pBound.collide(iBound) || iBound.contains(pBound)) {
				id = i->id();
				return true;
			}
		}
	}

	return false;
}

//------------------------------------------------------------------------
// Purpose: Check if a sprite is colliding with non-interactive stuff in the level
// id is set to the id of colliding object
//------------------------------------------------------------------------
bool Level::collidingWithLevel(pyrodactyl::event::Info &info, pyrodactyl::anim::Sprite &s) {
	_terrain.collideWithNoWalk(s.boundRect(), s._collideData);

	_terrain.collideWithStairs(s.boundRect(), s._velMod);

	if (_terrain.collideWithMusic(s.boundRect(), _music)) {
		if (_music._track)
			g_engine->_musicManager->playMusic(_music._id);
		else
			g_engine->_musicManager->playEffect(_music._id, _music._loops);
	}

	// If we are colliding with something, return true
	return !s._collideData.empty();
}

} // End of namespace Crab