aboutsummaryrefslogtreecommitdiffstats
path: root/src/attack.cpp
blob: 41fb77c8057e37188d6695a58cfdf856de700f86 (plain)
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
#include <attack.hpp>

#include <components.hpp>
#include <engine.hpp>
#include <particle.hpp>
#include <player.hpp>
#include <render.hpp>
#include <world.hpp>

// math helpers because we don't trust stdlib
template<typename T>
inline T abs(const T& n) {
	static_assert(std::is_arithmetic<T>::value, "abs expects numbers");
	return n >= 0 ? n : -n;
}

bool inrange(float point, float left, float right, float range)
{
	return (left < point + range && left > point - range) ||
		(right < point + range && right > point - range) ||
		(point > left && point < right);
}

bool inrange(float point, float left, float right)
{
	return point > left && point < right;
}

std::vector<AttackSystem::AttackAnimation> AttackSystem::effects;

bool AttackSystem::receive(const AttackEvent& ae)
{
	attacks.emplace_front(ae);
	return true;
}

namespace lua {
	static entityx::Entity* entity;

	inline void setEntity(entityx::Entity* e) {
		entity = e;
	}

	int flash(lua_State* state) {
		float r = lua_tonumber(state, 1);
		float g = lua_tonumber(state, 2);
		float b = lua_tonumber(state, 3);
		entity->replace<Flash>(Color(r, g, b));
		return 0;
	}

	int damage(lua_State* state) {
		float d = lua_tonumber(state, 1);
		auto h = entity->component<Health>();
		h->health -= d;
		if (h->ouch != nullptr)
			Mix_PlayChannel(0, h->ouch, 0);
		return 0;
	}
}

void AttackSystem::initLua(LuaScript& s)
{
	s.addFunction("flash", lua::flash);
	s.addFunction("damage", lua::damage); 
}

void AttackSystem::update(entityx::EntityManager& en, entityx::EventManager& ev, entityx::TimeDelta dt)
{
	(void)en;
	(void)ev;
	(void)dt;

	// handle painful entities (e.g. arrow)
	en.each<Hit, Position>([&](entityx::Entity p, Hit& hit, Position& ppos) {
		bool die = false;
		en.each<Health, Position, Solid>([&](entityx::Entity e, Health& health, Position& pos, Solid& dim) {
			(void)health;
			if (!e.has_component<Player>() && inrange(ppos.x, pos.x, pos.x + dim.width) && inrange(ppos.y, pos.y - 2, pos.y + dim.height)) {
				lua::setEntity(&e);
				hit.attack->script("effect");
				if (hit.effect.size() > 0)
					effects.emplace_back(vec2(ppos.x, ppos.y), hit.effect);
				//ParticleSystem::addMultiple(15, ParticleType::SmallBlast,
				//	[&](){ return vec2(pos.x + dim.width / 2, pos.y + dim.height / 2); }, 300, 7);
				die = true;
			} else if (WorldSystem::isAboveGround(vec2(ppos.x, ppos.y - 5)))
				die = true;
		});

		if (die)
			p.destroy();
	}); 

	// handle emitted attacks (player's)
	for (const auto& a : attacks) {
		//vec2 point = a.pos + a.attack.offset;
		//vec2 size = a.attack.range;
		//point.y -= size.y / 2; // center range height
		
		vec2 point = a.pos + a.attack.offset;
		vec2 size (0, a.attack.range.y);
		point.y -= size.y / 2;
		a.attack.script("hit", {LuaVariable("xrange", size.x)});

		en.each<Position, Solid, Health>(
			[&](entityx::Entity e, Position& pos, Solid& dim, Health& h) {
				(void)h;
				if (!(e.has_component<Player>() ^ a.fromplayer)) // no self-harm please
					return;

				if (inrange(point.x, pos.x, pos.x + dim.width, HLINES(size.x)) &&
					inrange(point.y, pos.y, pos.y + dim.height, HLINES(size.y))) {
					lua::setEntity(&e);
					a.attack.script("effect");
					if (pos.x < point.x) {
						e.component<Direction>()->x = -0.1;
						e.component<Direction>()->y = 0.1;
					} else {
						e.component<Direction>()->x = 0.1;
						e.component<Direction>()->y = 0.1;
					}
					if (a.attack.effect.size() > 0)
						effects.emplace_back(point, a.attack.effect);
					//ParticleSystem::addMultiple(15, ParticleType::DownSlash,
					//	[&](){ return vec2(pos.x + dim.width / 2, pos.y + dim.height / 2); }, 300, 7);
				}
			}
		);
	}

	attacks.clear();
}

#define RATE 3
void AttackSystem::render(void)
{
	float z = Render::ZRange::Attack;
	Render::worldShader.use();
	Render::worldShader.enable();
	for (auto& ae : effects) {
		ae.effect(ae.counter / RATE); // bind current frame
		auto dim = ae.effect.getTextureDim();
		auto s = ae.pos - (dim / 2);
		GLfloat verts[] = {
			s.x,         s.y,         z, 0, 0,
			s.x + dim.x, s.y,         z, 1, 0,
			s.x + dim.x, s.y + dim.y, z, 1, 1,
			s.x + dim.x, s.y + dim.y, z, 1, 1,
			s.x,         s.y + dim.y, z, 0, 1,
			s.x,         s.y,         z, 0, 0
		};
		glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), verts);
		glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), verts + 3);
		glDrawArrays(GL_TRIANGLES, 0, 6);
		z -= 0.05f;
	}
	Render::worldShader.disable();
	Render::worldShader.unuse();

	effects.erase(std::remove_if(effects.begin(), effects.end(), [](auto& e) { return ++e.counter >= e.effect.size() * RATE; }),
		effects.end());
}