summaryrefslogtreecommitdiff
path: root/src/game/rocket.cpp
blob: 531092252c350420f5bba8a2611e396658e585eb (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
/*
This file is part of Minitrem.

Minitrem 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 2 of the License, or
(at your option) any later version.

Minitrem 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 Minitrem.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "game.hpp"

namespace game {

const v2f_t rocket_radius(1.0f, 1.0f);

rocket_t::rocket_t(game::state_t *game, v2f_t x_, v2f_t target_, unit_t *shooter_) : entity_t(game, ET_ROCKET)
{
	x = x_;
	target = target_;
	v = (target - x).norm() * 10.0f;
	shooter = shooter_;

	size = rectf_t(v2f_t(-0.1f, -0.1f), v2f_t(0.1f, 0.1f));
	render_size = size;
	cmodel.cflags = CF_BACKGROUND;

	wake();
}

void rocket_t::on_spawn(void)
{
	aim_marker = std::make_unique<fx_aim_marker_t>(game, target);
}

void rocket_t::on_think(void)
{
	float Ds, ds;
	v2f_t end;
	world::trace_t trace;

	Ds = (target - x) * v.norm();
	ds = v.len() * game->dt;

	if (Ds > ds) {
		end = x + v * game->dt;
		trace = world->ray_v_all(x, end, CF_SOLID|CF_BODY|CF_BODY_SMALL, shooter);
	} else {
		end = target;
		trace = world->ray_v_all_p3d(x, end, CF_SOLID|CF_BODY|CF_BODY_SMALL, -1, this);
	}

	if (trace.hit) {
		if (trace.tile && trace.tile->type == TILE_WATER) {
			fx_bullet_miss_t *bullet_miss;

			bullet_miss = new fx_bullet_miss_t(game, trace.end, true);
			bullet_miss->place(world);
		} else {
			game->explosion(trace.end);
		}

		delete this;
		return;
	}

	x = trace.end;
	place(world, x);
}

void rocket_t::render_to(render::state_t *render)
{
	render->render(game->now, &assets::soldier.rocket, render_bounds);
}

}