2007-11-25 19:42:40 +00:00
|
|
|
/* copyright (c) 2007 magnus auvinen, see licence.txt for more info */
|
2008-08-14 17:19:13 +00:00
|
|
|
#include <base/system.h>
|
|
|
|
#include <base/math.hpp>
|
|
|
|
#include <base/vmath.hpp>
|
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
#include <math.h>
|
2008-01-19 10:57:25 +00:00
|
|
|
#include <engine/e_common_interface.h>
|
2008-08-17 08:52:24 +00:00
|
|
|
#include <game/mapitems.hpp>
|
|
|
|
#include <game/layers.hpp>
|
2008-09-23 14:10:05 +00:00
|
|
|
#include <game/collision.hpp>
|
2008-01-13 11:15:32 +00:00
|
|
|
|
|
|
|
static TILE *tiles;
|
|
|
|
static int width = 0;
|
|
|
|
static int height = 0;
|
|
|
|
|
|
|
|
int col_width() { return width; }
|
|
|
|
int col_height() { return height; }
|
|
|
|
|
|
|
|
int col_init()
|
|
|
|
{
|
2008-01-29 21:39:41 +00:00
|
|
|
width = layers_game_layer()->width;
|
|
|
|
height = layers_game_layer()->height;
|
|
|
|
tiles = (TILE *)map_get_data(layers_game_layer()->data);
|
2008-09-23 14:38:13 +00:00
|
|
|
|
|
|
|
for(int i = 0; i < width*height; i++)
|
|
|
|
{
|
|
|
|
int index = tiles[i].index;
|
|
|
|
|
|
|
|
if(index > 128)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(index == TILE_DEATH)
|
|
|
|
tiles[i].index = COLFLAG_DEATH;
|
|
|
|
else if(index == TILE_SOLID)
|
|
|
|
tiles[i].index = COLFLAG_SOLID;
|
|
|
|
else if(index == TILE_NOHOOK)
|
|
|
|
tiles[i].index = COLFLAG_SOLID|COLFLAG_NOHOOK;
|
|
|
|
else
|
|
|
|
tiles[i].index = 0;
|
|
|
|
}
|
|
|
|
|
2008-01-13 11:15:32 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-23 14:10:05 +00:00
|
|
|
int col_get(int x, int y)
|
2008-01-13 11:15:32 +00:00
|
|
|
{
|
2008-09-23 14:10:05 +00:00
|
|
|
int nx = clamp(x/32, 0, width-1);
|
|
|
|
int ny = clamp(y/32, 0, height-1);
|
2008-01-13 11:15:32 +00:00
|
|
|
|
2008-09-23 14:10:05 +00:00
|
|
|
if(tiles[ny*width+nx].index > 128)
|
|
|
|
return 0;
|
|
|
|
return tiles[ny*width+nx].index;
|
|
|
|
}
|
|
|
|
|
|
|
|
int col_is_solid(int x, int y)
|
|
|
|
{
|
|
|
|
return col_get(x,y)&COLFLAG_SOLID;
|
2008-01-13 11:15:32 +00:00
|
|
|
}
|
|
|
|
|
2008-09-23 14:10:05 +00:00
|
|
|
|
2008-01-13 11:15:32 +00:00
|
|
|
// TODO: rewrite this smarter!
|
2009-01-11 10:26:17 +00:00
|
|
|
int col_intersect_line(vec2 pos0, vec2 pos1, vec2 *out_collision, vec2 *out_before_collision)
|
2008-01-13 11:15:32 +00:00
|
|
|
{
|
|
|
|
float d = distance(pos0, pos1);
|
2009-01-11 10:26:17 +00:00
|
|
|
vec2 last = pos0;
|
2008-01-13 11:15:32 +00:00
|
|
|
|
|
|
|
for(float f = 0; f < d; f++)
|
|
|
|
{
|
|
|
|
float a = f/d;
|
|
|
|
vec2 pos = mix(pos0, pos1, a);
|
2009-01-11 10:26:17 +00:00
|
|
|
if(col_is_solid(round(pos.x), round(pos.y)))
|
2008-01-13 11:15:32 +00:00
|
|
|
{
|
2009-01-11 10:26:17 +00:00
|
|
|
if(out_collision)
|
|
|
|
*out_collision = pos;
|
|
|
|
if(out_before_collision)
|
|
|
|
*out_before_collision = last;
|
|
|
|
return col_get(round(pos.x), round(pos.y));
|
2008-01-13 11:15:32 +00:00
|
|
|
}
|
2009-01-11 10:26:17 +00:00
|
|
|
last = pos;
|
2008-01-13 11:15:32 +00:00
|
|
|
}
|
2009-01-11 10:26:17 +00:00
|
|
|
if(out_collision)
|
|
|
|
*out_collision = pos1;
|
|
|
|
if(out_before_collision)
|
|
|
|
*out_before_collision = pos1;
|
2008-09-23 14:38:13 +00:00
|
|
|
return 0;
|
2008-01-13 11:15:32 +00:00
|
|
|
}
|