summaryrefslogtreecommitdiff
path: root/lib/world.c
blob: 34d748928c9d1cb97b6b8ac9c50ce435e946bff1 (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
#include <stdlib.h>
#include <string.h>

#include "types.h"
#include "world.h"
#include "log.h"

struct world {
	struct entity entities[WORLD_MAX_ENTITIES];
};

struct world *
dp_world_create()
{
	struct world *w;

	if (!(w = malloc(sizeof(*w))))
		return NULL;
	memset(w, 0, sizeof(*w));

	return w;
}

void
dp_free_world(struct world *w)
{
	if (w)
		free(w);
}

int
dp_world_create_entity(struct world *w, int kind)
{
	int i;
	struct entity *e;

	if (!w)
		return -1;
	if (kind == ENTITY_NONE)
		return -1;
	
	for (i = 0; i < WORLD_MAX_ENTITIES; i++) {
		e = &w->entities[i];
		if (e->kind != ENTITY_NONE)
			continue;
		e->kind = kind;
		return i;
	}

	return -1;
}

struct entity *
dp_world_find_entity(struct world *w, int entity_id)
{
	struct entity *e;

	if (!w || entity_id < 0)
		return NULL;
	if (entity_id >= WORLD_MAX_ENTITIES)
		return NULL;

	e = &w->entities[entity_id];
	
	if (e->kind == ENTITY_NONE)
		return NULL;

	return e;
}

int
dp_world_remove_entity(struct world *w, int entity_id)
{
	struct entity *e;

	if (!(e = dp_world_find_entity(w, entity_id)))
		return -1;

	memset(e, 0, sizeof(*e));
	e->kind = ENTITY_NONE;
	return 0;
}

int
dp_world_tick(struct world *w, double delta)
{
	int i;
	struct entity *e;
	struct entity_ball *ball;
	
	for (i = 0; i < WORLD_MAX_ENTITIES; i++)
	{
		e = &w->entities[i];
		switch (e->kind) {
		case ENTITY_BALL:
			ball = &e->e.ball;
			/*ball->pos = dp_vec2_new(500.0, 500.0);*/
			ball->pos = dp_vec2_add(ball->pos, dp_vec2_mul(ball->vel, delta));
			break;
		default:
			break;
		}
	}
	return 0;
}