Source: src/cl_gameobj.cc
|
|
|
|
//Game Object (9.11.2002)
// Basic object structure - the parrent structure of object
// in game. All other game structures can be herited from
// this structure.
#include "cl_gameobj.h"
#define DUMMY_ANIM_KEY "dummy_anim_key"
void GAMEOBJ::init()
{
x = y = z = mc = 0;
lastmove = MOVE_NONE;
animkey = DUMMY_ANIM_KEY;
speedc = 0;
animspeed = 1;
falling = 0;
fall_counter = 0;
jump = 0;
obj_para = 0;
obj_active = 1;
obj_control = 0;
para_key = "";
k_up = KEY_UP;
k_down = KEY_DOWN;
k_left = KEY_LEFT;
k_right = KEY_RIGHT;
property_int.clear();
property_str.clear();
stage = NULL;
}
void GAMEOBJ::connect_stage(STAGE *s)
{
stage = s;
}
void GAMEOBJ::xy(int x_, int y_)
{
x = x_;
y = y_;
}
int GAMEOBJ::setxy(int x_, int y_)
{
if(stage->walkable(x_, y_) == TEX_WALKABLE) {
DRS_add_rectangle(x,y,xs,ys);
x = x_;
y = y_;
DRS_add_rectangle(x,y,xs,ys);
return 1;
}
return 0;
}
int GAMEOBJ::check_walk_points(int x, int y)
{
if(!stage) {
error("Accessing to unexisting stage");
return 0;
}
if(
(stage->walkable(x + 2, y + 2) == TEX_WALKABLE) &&
(stage->walkable(x + xs - 2, y + 2) == TEX_WALKABLE) &&
(stage->walkable(x + 2, y + ys - 2) == TEX_WALKABLE) &&
(stage->walkable(x + xs - 2, y + ys -2) == TEX_WALKABLE)) {
return 1;
} else {
return 0;
}
}
int GAMEOBJ::move(int x_, int y_)
{
if(check_walk_points(x + x_, y + y_) == TEX_WALKABLE) {
DRS_add_rectangle(x,y,xs,ys);
x += x_;
y += y_;
DRS_add_rectangle(x,y,xs,ys);
return 1;
}
return 0;
}
void GAMEOBJ::setanim(ANIM *anim_)
{
anim = anim_;
}
void GAMEOBJ::update_size()
{
BITMAP *b;
b = anim->get(animkey, mc);
xs = b->w;
ys = b->h;
}
int GAMEOBJ::processable() //test if it's possible to process this object
{ //if it is visible, etc...
string pr;
pr = property_str[OBJ_ROOM];
// message("Property [%s] - room [%s]", pr.c_str(), stage->room_name().c_str());
pr = "";
if((pr.empty()) || (pr == GAME_OBJECT_GLOBAL) ||
(pr == stage->room_name())) {
return 1;
}
return 0;
}
int GAMEOBJ::process()
{
int sx, sy;
if(!processable()) {
return 0;
}
if(obj_weight) { //aply weight of object
falling = move(0, obj_weight);
}
if(animated) { //animation
speedc++;
if(speedc >= animspeed) {
mc++;
speedc = 0;
DRS_add_rectangle(x,y,xs,ys);
if(anim->getcount(animkey) <= mc) {
mc = 0;
}
}
}
if(jump) { //jump
jump--;
if(!jump) {
setanimkey(MOVE_DOWN_STR);
} //normal jump termination
fall_counter = 0;
}
if(falling) { //fall
fall_counter++;
} else {
if((fall_counter) && (obj_para)){
DRS_add_rectangle(x-4,y-ys-8,xs+8,ys+8);
}
fall_counter = 0;
}
if(property_str[OBJ_MOTION] == MOTION_SIMPLE) { //motion logic = simple
sx = property_int[OBJ_SPEED_X];
sy = property_int[OBJ_SPEED_Y];
move(sx, sy);
if( (sx) && (
(x > property_int[OBJ_BORDER_X2]) ||
(x < property_int[OBJ_BORDER_X1])))
{
property_int[OBJ_SPEED_X] = -sx;
aply_speed_x();
}
if( (sy) && (
(y > property_int[OBJ_BORDER_Y2]) ||
(y < property_int[OBJ_BORDER_Y1])))
{
property_int[OBJ_SPEED_Y] = -sy;
aply_speed_y();
}
}
if(property_str[OBJ_MOTION] == MOTION_BOUNCE) { //motion logic = bounce
sx = property_int[OBJ_SPEED_X];
sy = property_int[OBJ_SPEED_Y];
if(!move(sx, 0)) {
property_int[OBJ_SPEED_X] = -sx;
aply_speed_x();
}
if(!move(0, sy)) {
property_int[OBJ_SPEED_Y] = -sy;
aply_speed_y();
}
}
return 1;
}
void GAMEOBJ::setanimkey(string s)
{
animkey = s;
mc = 0;
}
void GAMEOBJ::draw()
{
if(obj_active) {
draw_sprite(bmp, anim->get(animkey, mc), x, y);
if((fall_counter > 26) && (obj_para)) {
DRS_add_rectangle(x-4,y-ys-8,xs+8,ys+8);
if(fall_counter > 25 + obj_para) {
fall_counter = 26 + obj_para;
}
draw_sprite(bmp, anim->get(para_key, fall_counter-27), x, y-ys);
}
}
}
void GAMEOBJ::changedirection(int dir)
{
if((lastmove!=dir) && (property_int[OBJ_DIRECTIONS])) {
lastmove = dir;
switch(dir) {
case MOVE_UP :{
setanimkey(MOVE_UP_STR);
break;
}
case MOVE_DOWN :{
setanimkey(MOVE_DOWN_STR);
break;
}
case MOVE_LEFT :{
setanimkey(MOVE_LEFT_STR);
break;
}
case MOVE_RIGHT :{
setanimkey(MOVE_RIGHT_STR);
break;
}
case MOVE_JUMP_UP:{
setanimkey(MOVE_JUMP_UP_STR);
break;
}
case MOVE_JUMP_RIGHT:{
setanimkey(MOVE_JUMP_RIGHT_STR);
break;
}
case MOVE_JUMP_LEFT:{
setanimkey(MOVE_JUMP_LEFT_STR);
break;
}
default :{
error("Wrong direction!");
}
}
}
}
void GAMEOBJ::aply_speed_x()
{
if(property_int[OBJ_SPEED_X] > 0) {
changedirection(MOVE_RIGHT);
}
if(property_int[OBJ_SPEED_X] < 0) {
changedirection(MOVE_LEFT);
}
}
void GAMEOBJ::aply_speed_y()
{
if(property_int[OBJ_SPEED_Y] > 0) {
changedirection(MOVE_DOWN);
}
if(property_int[OBJ_SPEED_X] < 0) {
changedirection(MOVE_UP);
}
}
void GAMEOBJ::control_process(int type)
{
if(key[k_up]) {
if(type == GAME_TYPE_ARCADE) {
if(!((key[k_right]) && (key[k_left]))) {
//disable patological situation when left and right keys
//are pressed
start_jump();
}
if((!key[k_right]) && (!key[k_left]) && (jump)) {
move(MOVE_JUMP_UP);
}
} else {
if(!falling) {
move(MOVE_UP);
}
}
}
if(key[k_down]) {
if((!jump) && (!falling)) {
move(MOVE_DOWN);
}
}
if((key[k_left]) && (!key[k_right])){ //only left key is pressed
if(!jump) {
if(!move(MOVE_LEFT)) { //partial move
if(type == GAME_TYPE_ARCADE) {
move(MOVE_PARTLY_LEFT);
}
}
} else {
move(MOVE_JUMP_LEFT);
}
}
if((key[k_right]) && (!key[k_left])) { //only right key is pressed
if(!jump) {
if(!move(MOVE_RIGHT)) {
if(type == GAME_TYPE_ARCADE) {
move(MOVE_PARTLY_RIGHT);
}
}
} else {
move(MOVE_JUMP_RIGHT);
}
}
}
int GAMEOBJ::move(int m)
{
switch(m) {
case MOVE_LEFT: {
m = move(-4,0);
changedirection(MOVE_LEFT);
break;
}
case MOVE_RIGHT: {
m = move(4,0);
changedirection(MOVE_RIGHT);
break;
}
case MOVE_UP: {
m = move(0,-4);
changedirection(MOVE_UP);
break;
}
case MOVE_DOWN: {
m = move(0,4);
changedirection(MOVE_DOWN);
break;
}
case MOVE_JUMP_UP: {
m = move(0,-4);
m = move(0,-4);
if(m) {
changedirection(MOVE_JUMP_UP);
} else {
stop_jump();
}
break;
}
case MOVE_JUMP_RIGHT: {
m = move(2,-4);
m = move(2,-4);
if(m) {
changedirection(MOVE_JUMP_RIGHT);
} else {
stop_jump();
}
break;
}
case MOVE_JUMP_LEFT: {
m = move(-2,-4);
m = move(-2,-4);
if(m) {
changedirection(MOVE_JUMP_LEFT);
} else {
stop_jump();
}
break;
}
case MOVE_PARTLY_LEFT: {
m = move(-4,-4);
changedirection(MOVE_LEFT);
break;
}
case MOVE_PARTLY_RIGHT: {
m = move(4,-4);
changedirection(MOVE_RIGHT);
break;
}
}
return m;
}
/*
void GAMEOBJ::set_level(LEVEL *l)
{
GAMEOBJ::lev = l;
}*/
void GAMEOBJ::setscreen(BITMAP *bmp_)
{
bmp = bmp_;
}
int GAMEOBJ::getx()
{
return x;
}
int GAMEOBJ::gety()
{
return y;
}
int GAMEOBJ::collision(GAMEOBJ &o) //detects colision
{
if((x == o.getx()) && (y == o.gety())) {
return 1;
} else {
return 0;
}
}
void GAMEOBJ::set_animspeed(int i)
{
animspeed = i;
}
void GAMEOBJ::set_anim_property(int animat, int apos, int aspeed)
{
animated = animat;
mc = apos;
animspeed = aspeed;
}
void GAMEOBJ::id(int i)
{
obj_id = i;
}
int GAMEOBJ::id()
{
return obj_id;
}
void GAMEOBJ::name(string s)
{
obj_name = s;
}
string GAMEOBJ::name()
{
return obj_name;
}
void GAMEOBJ::type(string s)
{
obj_type = s;
}
string GAMEOBJ::type()
{
return obj_type;
}
int GAMEOBJ::bb_collision(GAMEOBJ *p) //bounding box
{
if(obj_active) {
if(( ((x)>=(p->x)+(p->xs)) || ((p->x)>=(x)+(xs)) ||
((y)>=(p->y)+(p->ys)) || ((p->y)>=(y)+(ys)) )) {
return 0;
}
}
return 1;
}
void GAMEOBJ::drs_rectangle()
{
DRS_add_rectangle(x,y,xs,ys);
}
int GAMEOBJ::start_jump()
{
if((!jump) && (!falling)) {
jump = 10;
return 1;
}
return 0;
}
void GAMEOBJ::stop_jump()
{
jump = 0;
mc = 0;
setanimkey(MOVE_DOWN_STR);
}
void GAMEOBJ::set_para_key(string s)
{
para_key = s;
obj_para = anim->getcount(s);
}
void GAMEOBJ::active(int i)
{
DRS_add_rectangle(x,y,xs,ys);
obj_active = i;
}
int GAMEOBJ::active()
{
return obj_active;
}
void GAMEOBJ::control(int i)
{
obj_control = i;
}
int GAMEOBJ::control()
{
return obj_control;
}
void GAMEOBJ::set_property(string name, int value)
{
if(name == OBJ_WEIGHT) {
obj_weight = value;
}
if(name == OBJ_CONTROL) {
obj_control = value;
}
if(name == OBJ_KEY_LEFT) {
k_left = value;
}
if(name == OBJ_KEY_RIGHT) {
k_right = value;
}
if(name == OBJ_KEY_DOWN) {
k_down = value;
}
if(name == OBJ_KEY_UP) {
k_up = value;
}
property_int[name] = value;
}
void GAMEOBJ::set_property(string name, string value)
{
property_str[name] = value;
}
string GAMEOBJ::get_property_str(string name)
{
return property_str[name];
}
int GAMEOBJ::get_property_int(string name)
{
return property_int[name];
}
Generated by: georgik on armada on Sat Jul 24 07:07:15 2004, using kdoc 2.0a54. |