- Treballant en les colisions

This commit is contained in:
2023-03-06 12:31:49 +01:00
parent 53212f4bfa
commit 2942b85de7
3 changed files with 78 additions and 1 deletions

View File

@@ -1,5 +1,6 @@
#include "actor.h"
#include "jdraw.h"
#include "jinput.h"
namespace actor
{
@@ -79,6 +80,36 @@ namespace actor
}
}
void update(actor_t *act)
{
if (input::keyDown(SDL_SCANCODE_LEFT) && act->pos.x>0) {
act->pos.x--;
actor::actor_t *other = actor::get_collision(act);
if (other)
{
other->push |= 1;
act->pos.x++;
}
else
{
actor::setDirty(act);
}
}
if (input::keyDown(SDL_SCANCODE_LEFT) && act->pos.x>0) {
act->pos.x--;
actor::actor_t *other = actor::get_collision(act);
if (other)
{
other->push |= 1;
act->pos.x++;
}
else
{
actor::setDirty(act);
}
}
}
void draw(actor_t *act, const bool draw_all)
{
if (!act) return;
@@ -88,4 +119,30 @@ namespace actor
if (draw_all && act->next) draw(act->next);
}
actor_t *get_collision(actor_t *act)
{
actor_t *other = first;
while (other)
{
if (other != act)
{
if (check_collision(act, other))
{
return other;
}
}
other = other->next;
}
return nullptr;
}
const bool check_collision(actor_t *obj1, actor_t *obj2)
{
return (obj1->pos.x < obj2->pos.x + obj2->size.x) &&
(obj1->pos.x + obj1->size.x > obj2->pos.x ) &&
(obj1->pos.y < obj2->pos.y + obj2->size.y) &&
(obj1->pos.y + obj1->size.y > obj2->pos.y ) &&
(obj1->pos.z < obj2->pos.z + obj2->size.z) &&
(obj1->pos.z + obj1->size.z > obj2->pos.z );
}
}