Source: src/mi_object.cc


Annotated List
Files
Globals
Hierarchy
Index
//Middle level for objects

#include "mi_object.h"

void MI_OBJECT::init()
{
	thing.clear();
	obj_id.clear();
	colision_control.clear();
	obj_id_counter = 0;
	grav = 4;	//one pixel per second
	avatar = NULL;
	scr = NULL;
	stage = NULL;
	aimage = NULL;
	panel = NULL;
	gtype = GAME_TYPE_ARCADE;
}

void MI_OBJECT::canvas(BITMAP *bmp)
{
	scr = bmp;
}

void MI_OBJECT::actual_image(ANIM *b)
{
	aimage = b;
}
/*
void MI_OBJECT::actual_level(LEVEL *l)
{
	alevel = l;
}
*/

void MI_OBJECT::connect_stage(STAGE *s)
{
	stage = s;

	map::iterator i;
	for(i=thing.begin(); i!=thing.end(); i++) {
		i->second->connect_stage(s);
	}

}

void MI_OBJECT::connect_event(EVENT_DRIVER *e)
{
	event = e;
}

int MI_OBJECT::create(string name)
{
	if(!exist(name)) { 
		GAMEOBJ *embrio;
		embrio = new GAMEOBJ;
		embrio->init();
		embrio->setscreen(scr);
		embrio->setanim(aimage);
		embrio->connect_stage(stage);
		embrio->setanimkey("down");
		embrio->name(name);
		obj_id_counter++;
		embrio->id(obj_id_counter);
		obj_id[obj_id_counter] = embrio;
		thing[name] = embrio;
		return 1;
	} else {
#ifdef DEBUG
		error("Object %s already exists, couldn't create new one", name.c_str());
#endif
		return 0;
	}
}

void MI_OBJECT::destroy(string name)
{
	if(exist(name)) {
		remove(thing[name]);
	} else {
#ifdef DEBUG
		error("Object %s do not exist",name.c_str());
#endif
	}
}

int MI_OBJECT::exist(string name)
{
	if(thing.find(name) != thing.end()) {
		return 1;
	} else {
		return 0;
	}
}

void MI_OBJECT::destroy_all()
{
	map::iterator i;

	for(i=thing.begin(); i!=thing.end(); i++) {
		remove(i->second);
	}
}


void MI_OBJECT::place(string name, int x, int y)
{
	if(exist(name)) {
		thing[name]->xy(x,y);
	} else {
#ifdef DEBUG
		error("Trying to move with not existing object %s", name.c_str());
#endif
	}
}

void MI_OBJECT::connect_imgkey(string name, string ik)
{
	thing[name]->setanimkey(ik);
	thing[name]->update_size();
}

void MI_OBJECT::set_avatar(string name)
{
	if(exist(name)) {
		avatar = thing[name];
		panel->avatar(avatar);
	} else {
		error("Object [%s] shouldn't be avatar, because it do not exist.", name.c_str());
	}
}

void MI_OBJECT::prop(string name, string type, int value)
{
	if(type == OBJ_ACTIVE) {
		thing[name]->active(value);
	}
	if((type == OBJ_CONTROL) && (value)) {
		colision_control_add(name, thing[name]);
#ifdef HARD_DEBUG
		message("Colision added: %s", name.c_str());
#endif
	}
	thing[name]->set_property(type, value);
}

void MI_OBJECT::colision_control_add(string name, GAMEOBJ *o)
{
	colision_control[name] = o;
}

void MI_OBJECT::colision_control_del(string name)
{
	colision_control.erase(name);
}

void MI_OBJECT::colision_control_del(GAMEOBJ *o)
{
	colision_control.erase(o->obj_name);
}

void MI_OBJECT::prop(string name, string type, string value)
{
	if(type == OBJ_PARACHUTE) {
		thing[name]->set_para_key(value);
	}
	thing[name]->set_property(type, value);
}


//#### Avatar


void MI_OBJECT::place_avatar(int x, int y)
{
	avatar->xy(x,y);
}

void MI_OBJECT::draw_avatar()
{
	avatar->draw();
}

void MI_OBJECT::process_keyboard()
{
	map::iterator i;

	for(i=thing.begin(); i!=thing.end(); i++) {
		if(i->second->obj_control) {
			i->second->control_process(gtype);
		}
	}
}

void MI_OBJECT::remove(GAMEOBJ *p)
{
	p->drs_rectangle();
	if(p == panel->avatar()) {
		panel->avatar(NULL);
	}
	colision_control_del(p);
	thing.erase(p->name());
	obj_id.erase(p->id());
	delete p;
}

//Test if thing that avatar can pick exists
int MI_OBJECT::pick_exist()
{
	map::iterator i;

	for(i=thing.begin(); i!=thing.end(); i++) {
		if(i->second->get_property_int(OBJ_PICK)) {
			return 1;
		}
	}
	return 0;

}

int MI_OBJECT::aply(GAMEOBJ *actor,GAMEOBJ *p)
//returns 1 if aplication of function affects other game objects
//returns 0 if no other objects are affected
{
	if(p->active()) {

		if(!p->get_property_str(OBJ_COLLISION).empty()) {
			event->add(p->get_property_str(OBJ_COLLISION),
					p->obj_name,actor->obj_name);
		}
		
		if((p->get_property_int(OBJ_ENERGY)) && 
			(p->get_property_int(OBJ_GROUP) != actor->get_property_int(OBJ_GROUP))){		//ENERGY
			actor->set_property(OBJ_ENERGY,
				actor->get_property_int(OBJ_ENERGY) +
				p->get_property_int(OBJ_ENERGY));
			panel->drs_rectangle();
			
			if(p->get_property_int(OBJ_ENERGY) > 0){
				event->add(EVENT_ENERGY_UP,
					p->obj_name,actor->obj_name);
			} else {
				event->add(EVENT_ENERGY_DOWN,	//call event
					p->obj_name,actor->obj_name);
			}
			
			if(actor->get_property_int(OBJ_ENERGY) <= 0) {	//energy fail to zero
				event->add(EVENT_LOOSE_LIFE,
					p->obj_name,actor->obj_name);
			}
		} //don't stop check other properties of object

		if(p->get_property_int(OBJ_PICK)) {		//PICK
			panel->drs_rectangle();
			
			actor->set_property(OBJ_SCORE,
				p->get_property_int(OBJ_SCORE) +
				actor->get_property_int(OBJ_SCORE));
			
			event->add(EVENT_PICK);	//call event
			remove(p);
			if(!pick_exist()) {
				event->add(EVENT_COLLECTED,
					"",actor->obj_name);
			}
			return 1;	//may affect other objects
		}

	}
	return 0;	//no objects affected
}


void MI_OBJECT::collision_avatar()
{
	map::iterator i;
	map::iterator j;
	GAMEOBJ *p;
	GAMEOBJ *q;

	for(j=colision_control.begin(); j!=colision_control.end(); j++) {
		q = j->second;
		for(i=thing.begin(); i!=thing.end(); i++) {
			p = i->second;
			if((q->bb_collision(p)) && (q != p)) {
				if(aply(q, p)) { break; }
			}
		}
	}
}



void MI_OBJECT::connect_image(string tname, ANIM* a)
{
	thing[tname]->setanim(a);
}
/*
void MI_OBJECT::connect_level(LEVEL* l)
{
	map::iterator i;
	for(i=thing.begin(); i!=thing.end(); i++) {
		i->second->set_level(l);
	}
}
*/
void MI_OBJECT::connect_panel(PANEL *p)
{
	panel = p;
}

void MI_OBJECT::draw()
{
	map::iterator i;
	for(i=thing.begin(); i!=thing.end(); i++ ) {
		if(i->second) {
			i->second->draw();
		} else {
#ifdef DEBUG
			cout<<"Warning: Null object to draw"<refresh();
}

void MI_OBJECT::process()
{
	map::iterator i;
	for(i=thing.begin(); i!=thing.end(); i++ ) {
		i->second->process();
	}
}

void MI_OBJECT::set_anim(string aname, int animated, int apos, int aspeed)
{
	thing[aname]->set_anim_property(animated, apos, aspeed);
}


void MI_OBJECT::game_type(string g)
{
	if(g == "maze") {
		g = GAME_TYPE_MAZE;
	}
	if (g =="arcade") {
		g = GAME_TYPE_ARCADE;
	}
}

int MI_OBJECT::game_type()
{
	return gtype;
}

void MI_OBJECT::gravity(int i)
{
	grav = i;
}



Generated by: georgik on armada on Sat Jul 24 07:07:15 2004, using kdoc 2.0a54.