Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: e864abab4eb9d6c431f134c731eed3e91e15faa1 (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
/*
   game/ship.cc
   This file is part of the Osirion project and is distributed under 
   the terms and conditions of the GNU General Public License version 2 
*/

// project headers
#include "game/game.h"
#include "game/ship.h"
#include "math/mathlib.h"

// C++ headers
#include <iostream>

using math::degrees360f;
using math::degrees180f;

namespace game {

Ship::Ship(core::Player *owner, ShipModel *shipmodel) :
	core::EntityControlable(owner, ship_enttype)
{
	entity_modelname = "ships/" + shipmodel->modelname();
	entity_name = shipmodel->name() + ": <" + owner->name() + ">";
	ship_shipmodel = shipmodel;

	entity_moduletypeid = ship_enttype;

	current_target_direction = 0.0f;
	current_target_pitch = 0.0f;;
	current_target_roll = 0.0f;;
}

Ship::~Ship() 
{
	if (entity_owner)
		entity_owner->remove_asset(this);
}

void Ship::frame(float seconds) 
{
	const float direction_change_speed = 2;

	// update thrust
	math::clamp(target_thrust, 0.0f, 1.0f);
	entity_thrust = target_thrust;
	
	// update pitch
	math::clamp(target_pitch, -1.0f, 1.0f);
	if (current_target_pitch < target_pitch) {
		current_target_pitch += direction_change_speed * seconds;
		if (current_target_pitch > target_pitch)
			current_target_pitch = target_pitch;
	} else if (current_target_pitch > target_pitch) {
		current_target_pitch -= direction_change_speed * seconds;
		if (current_target_pitch < target_pitch)
			current_target_pitch = target_pitch;
	}
	
	if (fabs(seconds*current_target_pitch) > 0.0f) {
		math::clamp(current_target_pitch, -1.0f, 1.0f);
		float pitch_offset =  seconds * current_target_pitch;
		entity_axis.change_pitch(360.0f * ship_shipmodel->turnspeed() * pitch_offset);
	}

	// update direction
	math::clamp(target_direction, -1.0f, 1.0f);
	if (current_target_direction < target_direction) {
		current_target_direction += direction_change_speed * seconds;
		if (current_target_direction > target_direction) {
			current_target_direction = target_direction;
		}
	} else if (current_target_direction > target_direction) {
		current_target_direction -= direction_change_speed * seconds;
		if (current_target_direction < target_direction) {
			current_target_direction = target_direction;
		}
	}
	if (fabs(seconds*current_target_direction) > 0.0f ) {
		math::clamp(current_target_direction, -1.0f, 1.0f);
		float direction_offset = seconds * current_target_direction;
		entity_axis.change_direction(360.0f * ship_shipmodel->turnspeed() * direction_offset);
	}

	// update roll
	math::clamp(target_roll, -1.0f, 1.0f);
	if (current_target_roll < target_roll) {
		current_target_roll += direction_change_speed * seconds;
		if (current_target_roll > target_roll)
			current_target_roll = target_roll;
	} else if (current_target_roll > target_roll) {
		current_target_roll -= direction_change_speed * seconds;
		if (current_target_roll < target_roll)
			current_target_roll = target_roll;
	}
	if (fabs(current_target_roll) > 0.0f) {
		math::clamp(current_target_roll, -1.0f, 1.0f);
		float roll_offset = seconds * current_target_roll;
		entity_axis.change_roll(360.0f * ship_shipmodel->turnspeed() * roll_offset);
	}

	// update speed
	if (entity_speed < entity_thrust * ship_shipmodel->maxspeed()) {
		entity_speed += ship_shipmodel->acceleration() * seconds;
		if (entity_speed > entity_thrust * ship_shipmodel->maxspeed()) {
			entity_speed = entity_thrust * ship_shipmodel->maxspeed();
		}
	} else if(entity_speed > entity_thrust * ship_shipmodel->maxspeed()) {
		entity_speed -= ship_shipmodel->acceleration() * seconds;
		if (entity_speed < 0.0f) entity_speed = 0.0f;
	}

	if (entity_speed != 0.0f) {
		entity_location += entity_axis.forward() * entity_speed * seconds;
		entity_dirty = true;
	} else if ((current_target_pitch != 0.0f) || (current_target_direction != 0.0f) || (current_target_roll != 0.0f)) {
		entity_dirty = true;
	}
}

} // namespace game