示例#1
0
	void add_obstruction(Vector pos, std::vector<Vector> points) {
		///////////Old//////////
		//obstructions.push_back(obstruction(pos, points));
		////////////////////////
		if (bg_collision == NULL) init_bg_collision();
		obstruction tmp = obstruction(pos, points);
		tmp.draw(bg_collision);
		
	}
示例#2
0
文件: entity.c 项目: beoran/kq-fork
/*! \brief Generic movement
 *
 * Set up the entity vars to move in the given direction
 *
 * \param   target_entity Index of entity to move
 * \param   dx tiles to move in x direction
 * \param   dy tiles to move in y direction
 */
static int move(t_entity target_entity, int dx, int dy)
{
    int tile_x, tile_y, source_tile, oldfacing;
    s_entity *ent = &g_ent[target_entity];

    if (dx == 0 && dy == 0)      // Speed optimization.
    {
        return 0;
    }

    tile_x = ent->x / TILE_W;
    tile_y = ent->y / TILE_H;
    oldfacing = ent->facing;
    if (dx < 0)
    {
        ent->facing = FACE_LEFT;
    }
    else if (dx > 0)
    {
        ent->facing = FACE_RIGHT;
    }
    else if (dy > 0)
    {
        ent->facing = FACE_DOWN;
    }
    else if (dy < 0)
    {
        ent->facing = FACE_UP;
    }
    if (tile_x + dx == -1 || tile_x + dx == g_map.xsize ||
            tile_y + dy == -1 || tile_y + dy == g_map.ysize)
    {
        return 0;
    }
    if (ent->obsmode == 1)
    {
        // Try to automatically walk/run around obstacle.
        if (dx && obstruction(tile_x, tile_y, dx, 0, FALSE))
        {
            if (dy != -1 && oldfacing == ent->facing
                    && !obstruction(tile_x, tile_y + 1, dx, 0, TRUE)
                    && !obstruction(tile_x, tile_y, 0, 1, TRUE))
            {
                dy = 1;
            }
            else if (dy != 1 && oldfacing == ent->facing
                     && !obstruction(tile_x, tile_y - 1, dx, 0, TRUE)
                     && !obstruction(tile_x, tile_y, 0, -1, TRUE))
            {
                dy = -1;
            }
            else
            {
                dx = 0;
            }
        }
        if (dy && obstruction(tile_x, tile_y, 0, dy, FALSE))
        {
            if (dx != -1 && oldfacing == ent->facing
                    && !obstruction(tile_x + 1, tile_y, 0, dy, TRUE)
                    && !obstruction(tile_x, tile_y, 1, 0, TRUE))
            {
                dx = 1;
            }
            else if (dx != 1 && oldfacing == ent->facing
                     && !obstruction(tile_x - 1, tile_y, 0, dy, TRUE)
                     && !obstruction(tile_x, tile_y, -1, 0, TRUE))
            {
                dx = -1;
            }
            else
            {
                dy = 0;
            }
        }
        if ((dx || dy) && obstruction(tile_x, tile_y, dx, dy, FALSE))
        {
            dx = dy = 0;
        }
    }
    if (!dx && !dy && oldfacing == ent->facing)
    {
        return 0;
    }
    if (ent->obsmode == 1 && entityat(tile_x + dx, tile_y + dy, target_entity))
    {
        return 0;
    }

    // Make sure that the player can't avoid special zones by moving diagonally.
    if (dx && dy)
    {
        source_tile = tile_y * g_map.xsize + tile_x;
        if (z_seg[source_tile] != z_seg[source_tile + dx]
                || z_seg[source_tile] != z_seg[source_tile + dy * g_map.xsize])
        {
            if (ent->facing == FACE_LEFT || ent->facing == FACE_RIGHT)
            {
                if (!obstruction(tile_x, tile_y, dx, 0, TRUE))
                {
                    dy = 0;
                }
                else
                {
                    dx = 0;
                }
            }
            else                   // They are facing up or down.
            {
                if (!obstruction(tile_x, tile_y, 0, dy, TRUE))
                {
                    dx = 0;
                }
                else
                {
                    dy = 0;
                }
            }
        }
    }

    // Make sure player can't walk diagonally between active entities.
    if (dx && dy)
    {
        if (obstruction(tile_x, tile_y, dx, 0, TRUE) && obstruction(tile_x, tile_y, 0, dy, TRUE))
        {
            return 0;
        }
    }

    ent->tilex = tile_x + dx;
    ent->tiley = tile_y + dy;
    ent->y += dy;
    ent->x += dx;
    ent->moving = 1;
    ent->movcnt = 15;
    return 1;
}