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

#include <vector>
#include <iomanip>

#include "sys/sys.h"
#include "core/entity.h"

namespace core
{

using math::Color;
using math::Vector3f;

/* ---- Static functions for the Entity registry ------------------- */

std::map<unsigned int, Entity *> Entity::registry;

void Entity::add(Entity *ent)
{
	std::map<unsigned int, Entity*>::iterator it;
	unsigned int id = 1;
	for (it = registry.begin(); it != registry.end() && id == (*it).second->id(); it++) {
		id++;
	}
	ent->entity_id = id;
	registry[id] = ent;
}

Entity *Entity::find(unsigned int id)
{
	std::map<unsigned int, Entity *>::iterator it = registry.find(id);
	if (it == registry.end())
		return 0;
	else
		return (*it).second;
}

void Entity::remove(unsigned int id)
{
	std::map<unsigned int, Entity *>::iterator it = registry.find(id);
	if (it != registry.end()) {
		delete((*it).second);
		registry.erase(it);
	}
}

void Entity::list()
{
	std::map<unsigned int, Entity *>::iterator it;
	for (it = registry.begin(); it != registry.end(); it++) {
		std::string typeindicator;
		Entity *entity = (*it).second;
		con_print << "  id " << std::setw(4) << entity->id()
			<< " type " << std::setw(4) << entity->type()
			<< ":" << std::setw(4) << entity->moduletype()
			<< " " << entity->name() << std::endl;
	}
	con_print << registry.size() << " registered entities" << std::endl;
}

/*----- Entity ----------------------------------------------------- */

Entity::Entity(unsigned int flags) :
	entity_location(0.0f, 0.0f, 0.0f),
	entity_color(1.0f, 1.0f, 1.0f, 1.0f)
{
	entity_id = 0;
	entity_flags = flags;
	entity_moduletypeid = 0;
	
	entity_radius = 1.0f;
	entity_direction = 0;
	entity_shape = Diamond;
	
	add(this);
}

Entity::~Entity()
{
}

void Entity::frame(float seconds)
{
}

/* ---- EntityDynamic ---------------------------------------------- */

EntityDynamic::EntityDynamic(unsigned int flags) :
	Entity(flags)
{
	entity_speed = 0.0f;
}

EntityDynamic::~EntityDynamic()
{
}

void EntityDynamic::frame(float seconds)
{
	// location avoid sin/cos calculations
	entity_location.x += cosf(entity_direction * M_PI / 180) * entity_speed * seconds;
	entity_location.z -= sinf(entity_direction * M_PI / 180) * entity_speed * seconds;
}

/*----- EntityControlable ------------------------------------------ */

EntityControlable::EntityControlable(Player *player, unsigned int flags) :
	EntityDynamic(flags)
{
	entity_owner = 0;
	entity_thrust = 0;

	target_direction = 0.0f;
	target_thrust = 0.0f;
}

EntityControlable::~EntityControlable()
{
}

void EntityControlable::frame(float seconds)
{
	entity_direction = target_direction;
	entity_thrust = target_thrust;

	EntityDynamic::frame(seconds);
}

void EntityControlable::set_thrust(float thrust)
{
	target_thrust = thrust;
}

void EntityControlable::set_direction(float direction)
{
	target_direction = direction;
}

}