controller * rect_collide_all(SDL_Rect rect,controller * selfp,int number)
{
    number+=1;
    unsigned int occur=0;
    unsigned int size = resources::control.size();

    SDL_Rect r1= rect;
    std::vector<controller*> a = resources::control;

    for(unsigned int i=0; i < size; i+=1)
    {

        if (resources::control.at(i)==NULL)
            continue;

        if ( rect_collide(rect, resources::control.at(i)->rect ) )
        {

            SDL_Rect r2= resources::control.at(i)->rect;

            if ( resources::control.at(i)==selfp)
                continue;
            else
            {
                if (resources::control.at(i)->no_collide)
                    continue;

                return resources::control.at(i);
            }
        }

    }

    return NULL;
}
int entity_collide(entity *a, entity*b)
{
	SDL_Rect aB = {a->boundBox.x + a->position.x, a->boundBox.y + a->position.y, a->boundBox.w, a->boundBox.h};
	SDL_Rect bB = {b->boundBox.x + b->position.x, b->boundBox.y + b->position.y, b->boundBox.w, b->boundBox.h};

	//slog("Ent: A X:%i Y: %i\n Ent B X: %i Y: %i", a->position.x, a->position.y, b->position.x, b->position.y);

	return rect_collide(aB,bB);
}
void draw_health_bar(entity *self){
	//percent health to draw
	int barW = self->maxhealth;
	int barH = 15;
	int pw;
	int px;
	float percent = (float)self->health / (float)self->maxhealth; 
	//rects to use as health bars
	SDL_Rect bg_rect = {self->position.x + self->sprite->frameW/2 - barW/2, 
	self->boundBox.y + self->position.y-barH, barW, barH};
	SDL_Rect fg_rect;
	//teal foreground for health
	SDL_Color old; 
	SDL_Color FGColor = {0, 255, 255, 255};
	SDL_Color BGColor = {255, 0, 0, 255};
	

	SDL_GetRenderDrawColor(graphics_get_renderer(), &old.r, &old.g, &old.g, &old.a); 
	if(SDL_SetRenderDrawColor(graphics_get_renderer(), BGColor.r, BGColor.g, BGColor.b, BGColor.a))
	 {
		slog("Failed to set render draw color");
     }
	if(!rect_collide(bg_rect, graphics_get_player_cam()))
	{
		SDL_SetRenderDrawColor(graphics_get_renderer(), 0x00, 0x00, 0x00, 0x00);
		return;
	}
		bg_rect.x -= graphics_get_player_cam().x;
		bg_rect.y -= graphics_get_player_cam().y;
		fg_rect = bg_rect;
		px = bg_rect.x;
		percent = percent > 1.f ? 1.f : percent < 0.f ? 0.f : percent;
		pw = (int)((float)bg_rect.w * percent); 

		if(SDL_RenderFillRect(graphics_get_renderer(), &bg_rect))
		{
			slog("Failed to Fill bg rect");
		}
    if(SDL_SetRenderDrawColor(graphics_get_renderer(), FGColor.r, FGColor.g, FGColor.b, FGColor.a))
	{
		slog("Failed to set render draw color");
	}
 
	fg_rect.x = px;
	fg_rect.w = pw;
	if(SDL_RenderFillRect(graphics_get_renderer(), &fg_rect))
	{
		slog("Failed to Fill Rect");
	}

    if(SDL_SetRenderDrawColor(graphics_get_renderer(), 0x00, 0x00, 0x00, 0x00))
	{
		slog("Failed to set render draw color");
	}
}
Beispiel #4
0
void tree_debugView(TreeNode *node, int depth, BITMAP *buffer, Rect screen_pos)
{
    int i;
    rect(buffer, node->node_space.x - screen_pos.x, node->node_space.y - screen_pos.y, node->node_space.x+node->node_space.w - screen_pos.x, node->node_space.y+node->node_space.h - screen_pos.y, makecol(depth*50, depth*50, depth*50));
    if(node->childs[0] != NULL)
    {
        for(i=0; i<4; ++i)
            if(rect_collide(node->childs[i]->node_space, screen_pos))
                tree_debugView(node->childs[i], depth+1, buffer, screen_pos);
    }
}
/**
* @brief draws the tiles on the screen using tile type as reference for which sprite to draw
*/
void tile_render(){
	int i;
	SDL_Rect camera = graphics_get_player_cam();

	for( i = 0; i < TOTAL_TILES; i++)
	{
		Tile * tile = (tile_list + i);
		if(tile == NULL){
			printf("Tile is null while rendering");
			return ;
		}
		if(rect_collide(graphics_get_player_cam(), tile->mBox))
		{
			SDL_Rect dest = { tile->mBox.x - camera.x,tile->mBox.y-camera.y, TILE_HEIGHT, TILE_WIDTH};
			SDL_RenderCopy(__gt_graphics_renderer,tile_sprite_grass->image, NULL, &dest);
			if(dest_tile_list[i].mType == TILE_TREE)
			{
				SDL_RenderCopy(__gt_graphics_renderer,tile_sprite_tree->image, NULL, &dest);
			}
		}
	}
}
int weapon_collision(entity *self, entity *other, Rect_f bound)
{
	Rect_f aB = {(float)other->position.x + (float)other->boundBox.x, (float)other->position.y + other->boundBox.y, other->boundBox.w, other->boundBox.h};
	Rect_f bB = {(float)self->position.x + bound.x + self->weapon->boundOffset.x, (float)self->position.y + bound.y + self->weapon->boundOffset.y, bound.w, bound.h};

	if(!self || !self->weapon || !other){
		slog("NO weap or entity");
		return false;
	}

	slog("wep bound is X:%f Y:%f W:%f H:%f",bound.x,bound.y,bound.w,bound.h);
	/*slog("Owner Pos: X: %i Y:%i\n Weapon Pos X:%f Y:%f\nEntity: X:%f Y:%f",self->position.x, self->position.y,bB.x, bB.y,aB.x, aB.y);
	slog("Bounds Range X:%f - %f Y:%f - %f", bB.x, bB.x + bB.w, bB.y,  bB.y + bB.h);
	slog("Bound other: X:%f - %f Y:%f - %f", aB.x, aB.x + aB.w, aB.y,  aB.y + aB.h);*/

	if(rect_collide(bB, aB)){
		slog("Weapon has collided");
		weapon_touch(self, other);
	// weapon_collide_draw_box(bB,aB); used for visual collision box
		return true;
	}
	return false;
}
/**
* @brief checks to see if a position and bound collide with a tile
* @param Vec2d position of entity, SDL_Rect bounding box to check collision
* @return 1(true) if position & bound collide with a tree
*/
int tile_collision(Vec2d pos, SDL_Rect bound)
{
	int i;
	Rect_f player_pos = {pos.x + bound.x, pos.y + bound.y, bound.w, bound.h};
	Rect_f tile_bound;

	for( i = 0; i < TOTAL_TILES; i++)
	{
		//offset to make tile collision more lenient
		tile_bound.x = tile_list[i].mBox.x + TILE_WIDTH * .2f;
		tile_bound.y = tile_list[i].mBox.y + TILE_HEIGHT * .2f;
		tile_bound.w = tile_list[i].mBox.w * .6f;
		tile_bound.h = tile_list[i].mBox.h * .8f;

		if(rect_collide(player_pos, tile_bound))
		{
			if(dest_tile_list[i].mType == TILE_TREE)
			{
				return true;
			}
		}
	}
	return false;
}
void ent_follow_path(entity *self)
{
	Vec2d tile_pos;
	Vec2d tile_center_pos;
	Vec2d self_center_pos = { ENT_CENTER_X(self), ENT_CENTER_Y(self)};
	Vec2d player_center_pos = { ENT_CENTER_X(entity_get_player()), ENT_CENTER_Y(entity_get_player()) };
	Vec2d new_vel;
	//smaller bounds mean closer
	Rect_f tile_bound = {-1,-1, TILE_WIDTH/3, TILE_HEIGHT/3};
	Rect_f self_bound = {self->position.x + self->boundBox.x, self->position.y + self->boundBox.y,
						 self->boundBox.w, self->boundBox.h};
	float x;
	float y;

	int distance;
	if(!self) 
	{
		return;
	}
	if(self->state != STATE_AGGRO)
	{
		return;
	}
	if(!self->path)
	{
	//	slog("No Path");
		return;
		/*Vec2dSub(self_center_pos,player_center_pos,new_vel);
		Normalize2d(new_vel);
		VectorScale(new_vel, new_vel, 10);
		self->velocity = new_vel;
		slog("New Vel X:%f Y:%f", new_vel.x, new_vel.y);
		x = new_vel.x;
		y = new_vel.y;
		while((x * x + y * y) < 25)
		{
			//VectorScale(new_vel, new_vel, 2);
			x *= 2;
			y *= 2;
		}
		new_vel.x = x;
		new_vel.y = y;
		return;*/
	}
	else
	{
		/*slog("Next is: %i", self->path->tile_index);
		slog("Player is %i", tile_get_tile_number(entity_get_player()->position, entity_get_player()->boundBox));*/
		 tile_pos = tile_get_pos(self->path->tile_index);

		 tile_center_pos.x = TILE_CENTER_X(tile_pos);
		 tile_center_pos.y = TILE_CENTER_Y(tile_pos);

		 tile_bound.x = tile_center_pos.x;
		 tile_bound.y = tile_center_pos.y;
	}

/*	if((TILE_CENTER_X(tile_pos) == ENT_CENTER_X(self)) && 
	   TILE_CENTER_Y(tile_pos) == ENT_CENTER_Y(self) )*/
	if(rect_collide(tile_bound, self_bound))
	{
//		slog("rect collide");
		path_free_node(&(self->path));
		self->velocity.x = 0;
		self->velocity.y = 0;
	}
	else
	{	
		Vec2dSub(tile_center_pos, self_center_pos, new_vel);
		Normalize2d(new_vel);
		VectorScale(new_vel, new_vel, 5);
		x = new_vel.x;
		y = new_vel.y;
		while((x * x + y * y) < 49)
		{
			//VectorScale(new_vel, new_vel, 2);
			x *= 2;
			y *= 2;
		}
		new_vel.x = x;
		new_vel.y = y;
		self->velocity = new_vel;
	//	slog("New Vel X:%f Y:%f", new_vel.x, new_vel.y);
	}

}
int tile_forage(Vec2d pos, SDL_Rect bound, int face_dir)
{
	int i;
	int curr_tile;
	int is_tree = false;
	int tree_index;
	Rect_f player_pos = {pos.x + bound.x+bound.w/2, pos.y + bound.y + bound.h/2, bound.w/2, bound.h/2};
	Rect_f tile_bound;

	for( i = 0; i < TOTAL_TILES; i++)
	{
		tile_bound.x = tile_list[i].mBox.x;
		tile_bound.y = tile_list[i].mBox.y;
		tile_bound.w = tile_list[i].mBox.w;
		tile_bound.h = tile_list[i].mBox.h;
		
		if(rect_collide(player_pos, tile_bound))
		{
			curr_tile = i;
			break;
		}
	}

	switch(face_dir)
	{
		case UP:
			slog("Up");
			if(curr_tile < TOTAL_TILES_X)
			{
				return false;
			}
			if (dest_tile_list[curr_tile - TOTAL_TILES_X].mType == TILE_TREE)
			{
				is_tree = true;
				tree_index = curr_tile - TOTAL_TILES_X;
			}
			break;
		case DOWN:
			slog("Down");
			if(curr_tile > TOTAL_TILES - TOTAL_TILES_X)
			{
				return false;
			}
			if(dest_tile_list[curr_tile + TOTAL_TILES_X].mType == TILE_TREE)
			{
				is_tree = true;
				tree_index = curr_tile + TOTAL_TILES_X;
			}
			break;
		case LEFT:
			slog("Left");
			if(curr_tile % TOTAL_TILES_X == 0)
			{
				return false;
			}
			if (dest_tile_list[curr_tile - 1].mType == TILE_TREE)
			{
				is_tree = true;
				tree_index = curr_tile - 1;
			}
			break;
		case RIGHT:
			slog("Right");
			if(curr_tile % (TOTAL_TILES_X -1 ) == 0)
			{
				return false;
			}
			if(dest_tile_list[curr_tile + 1].mType == TILE_TREE)
			{
				tree_index = curr_tile + 1;
				is_tree = true;
			}
			break;
		default:
			break;
	}

	if(is_tree)
	{
		dest_tile_list[tree_index].hits -= 1;
		if( dest_tile_list[tree_index].hits == 0)
		{
			dest_tile_list[tree_index].mType = NULL;
			inventory_add("wood");
		}
	}
	slog("Is tree is %s", is_tree ? "TRUE" : "False");
	return is_tree;
}