- Treballant en les colisions
This commit is contained in:
@@ -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 );
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,8 @@ namespace actor
|
||||
vec3_t pos;
|
||||
vec3_t size;
|
||||
|
||||
uint8_t push;
|
||||
|
||||
actor_t *prev;
|
||||
actor_t *next;
|
||||
};
|
||||
@@ -28,6 +30,11 @@ namespace actor
|
||||
|
||||
void reorder();
|
||||
|
||||
void update(actor_t *act);
|
||||
|
||||
void draw(actor_t *act, const bool draw_all=true);
|
||||
|
||||
actor_t *get_collision(actor_t *act);
|
||||
|
||||
const bool check_collision(actor_t *obj1, actor_t *obj2);
|
||||
}
|
||||
@@ -26,7 +26,20 @@ int sx=1, sy=0;
|
||||
|
||||
bool game::loop()
|
||||
{
|
||||
if (input::keyDown(SDL_SCANCODE_LEFT) && box->pos.x>0) { box->pos.x--; actor::setDirty(box); }
|
||||
if (input::keyDown(SDL_SCANCODE_LEFT) && box->pos.x>0) {
|
||||
box->pos.x--;
|
||||
actor::actor_t *other = actor::get_collision(box);
|
||||
if (other)
|
||||
{
|
||||
other->push |= 1;
|
||||
box->pos.x++;
|
||||
}
|
||||
else
|
||||
{
|
||||
actor::setDirty(box);
|
||||
}
|
||||
}
|
||||
|
||||
if (input::keyDown(SDL_SCANCODE_RIGHT) && box->pos.x<42) { box->pos.x++; actor::setDirty(box); }
|
||||
if (input::keyDown(SDL_SCANCODE_UP) && box->pos.y>0) { box->pos.y--; actor::setDirty(box); }
|
||||
if (input::keyDown(SDL_SCANCODE_DOWN) && box->pos.y<42) { box->pos.y++; actor::setDirty(box); }
|
||||
|
||||
Reference in New Issue
Block a user