Skip to content
Snippets Groups Projects
dragon.cpp 2.33 KiB
#include "dragon.hpp"
#include "environment.hpp"
#include "key_positions_structure.hpp"
#include "interpolation.hpp"
#include "terrain.hpp"

void dragon::initialize_mvt(vec3 p, numarray<vec3> key_positions, numarray<float> key_times)
{
	for (int i = 0; i < key_positions.size(); i++) {
		key_positions[i] += p;
	}

	// Initialize the helping structure to display/interact with these positions
	keyframe.initialize(key_positions, key_times);

	// Set timer bounds
	// The timer must span a time interval on which the interpolation can be conducted
	// By default, set the minimal time to be key_times[1], and the maximal time to be key_time[N-2] (enables cubic interpolation)
	int NT = key_times.size();
	timer_mvt.t_min = key_times[0];
	timer_mvt.t_max = key_times[NT - 1];
	//timer_mvt.t = rand_interval(timer_mvt.t_min, timer_mvt.t_max);
	timer_mvt.t = 0;

	//Set max HP and current HP
	max_hp = 10;
	hp = 10;
	//No death time currently
	death_time = -1;

	//For fire
	isfiring = false;
	time_fire = 0;

	//Dragon hitbox
	dragon_hitbox.initialize_hitbox(8, { {3.5 * size,0,0.5 * size},{2.25 * size,0,0.6 * size},{1.25 * size,0,0.5 * size} , {0.25 * size ,0,0.5 * size} ,{-1 * size,0,0.5 * size},  {-2 * size,0,0.5 * size}, {-2.9 * size,0,0.4 * size}, {-3.5 * size,0,0.4 * size}}, {size, 0.5 * size, 0.75 * size, 0.9 * size, 0.75 * size, 0.75 * size, 0.4 * size, 0.4 * size});
}

void dragon::update_mvt()
{
	// Update the current time
	timer_mvt.update();
	float t = timer_mvt.t;

	// clear trajectory when the timer restart + begin going in a circle
	if (t < timer_mvt.t_min + 0.2f) //modified (was originally +0.1f so we are sure it passes through, even with low FPS)
		{
			keyframe.trajectory.clear();

			// Forget there was a first point so it goes in circles
			if (timer_mvt.t_min == 0) {
				timer_mvt.t_min = keyframe.key_times[1];
				timer_mvt.t = keyframe.key_times[1] + t;
				t = keyframe.key_times[1] + t;
				keyframe.key_positions[0] = keyframe.key_positions[1];
				keyframe.key_times[0] = keyframe.key_times[1];
			}
		}

	if (t + 0.2f > timer_mvt.t_max) pos_futur = interpolation(timer_mvt.t_min + 0.01f, keyframe.key_positions, keyframe.key_times, 0.5f);
	else pos_futur = interpolation((t + 0.1f), keyframe.key_positions, keyframe.key_times, 0.5f);

	// Compute the interpolated position
	pos = interpolation(t, keyframe.key_positions, keyframe.key_times, 0.5f);
}