File: container.cc

package info (click to toggle)
enemylines3 1.2-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,380 kB
  • ctags: 1,444
  • sloc: cpp: 16,323; makefile: 74; sh: 52
file content (114 lines) | stat: -rw-r--r-- 2,264 bytes parent folder | download | duplicates (6)
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
#include "SDL.h"
#include "SDL_opengl.h"


#include "container.h"
#include "entity.h"
#include "random.h"

namespace PRJID {


Container::Container() {
}
Container::~Container() {
	clear();
}

void Container::clear() {
}

void Container::add(Entity *e,bool special) {
	e->set_container(this);
	entities.push_back(e);
}

void Container::tick(unsigned int ticks) {
	if (ticks==0) return;
	std::list <Entity *>::iterator it;
	std::list <std::list <Entity *>::iterator>::iterator dit;
	std::list <std::list <Entity *>::iterator> todelete;

	for (it=entities.begin();it!=entities.end();it++) {
		(*it)->tick(ticks);
		if ((*it)->remove) {
			delete (*it);
			todelete.push_back(it);
		}
	}

	for (dit=todelete.begin();dit!=todelete.end();dit++) {
		entities.erase((*dit));
	}

}

void Container::act(unsigned int ticks) {
	std::list <Entity *>::iterator it;

	for (it=entities.begin();it!=entities.end();it++) {
		if ((*it)->get_type()==ET_PLAYER) continue;
		(*it)->act(ticks);
	}
}

void Container::draw() {
	std::list <Entity *>::iterator it;

	for (it=entities.begin();it!=entities.end();it++) {
		if ((*it)==NULL) continue;
		if ((*it)->get_type()==ET_PLAYER) continue;
		(*it)->draw();
	}
}

Entity *Container::select(int x,int y) {
   glReadBuffer(GL_BACK);

	Entity *selected=NULL;
	selected=select_single(x,y);
	//SDL_GL_SwapBuffers(); SDL_Delay(500);
	if (selected!=NULL) return selected;
	return NULL;
}

Entity *Container::select_single(int x,int y) {
	std::list <Entity *>::iterator it;

	Entity *selected=NULL;
	unsigned int p;
	unsigned int rem=std::numeric_limits<unsigned int>::max();

	glReadPixels(x,y,1,1,GL_DEPTH_COMPONENT, GL_UNSIGNED_INT,static_cast<GLvoid *>(&p));
	rem=p;

	for (it=entities.begin();it!=entities.end();it++) {
		if ((*it)==NULL) continue;
		if ((*it)->get_type()==ET_PLAYER) continue;
		if (!(*it)->isactive()) continue;
		(*it)->draw();
		glReadPixels(x,y,1,1,GL_DEPTH_COMPONENT, GL_UNSIGNED_INT,static_cast<GLvoid *>(&p));
		if (p>=rem) continue;
		rem=p;
		selected=(*it);
	}
	return selected;
}

void Container::set_game(Game *g) {
	game=g;
}


Entity * Container::create(e_entitytype t) {
	Entity *n;
	n=new Entity();
	n->set_game(game);
	n->set_container(this);
	n->set_type(t);
	add(n,true);
	return n;
}


} //namespace