mirror of
https://github.com/zoriya/Gamacon.git
synced 2026-06-08 22:04:48 +00:00
merge
This commit is contained in:
@@ -21,7 +21,8 @@ struct clickable_component
|
||||
//! @param entity_id The id of the clicked entity.
|
||||
//! @return Return true if this callback catch the event (other listeners after this one won't get the event)
|
||||
//! @return Return false if you want others listener to receive the event.
|
||||
bool (*onclick)(gc_engine *engine, int entity_id);
|
||||
callback_t onclick;
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
** EPITECH PROJECT, 2020
|
||||
** Gamacon
|
||||
** File description:
|
||||
** map_managment
|
||||
*/
|
||||
|
||||
#include "engine.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
static inline bool gc_vector2_is_ccw(gc_vector2 a, gc_vector2 b,gc_vector2 c)
|
||||
{
|
||||
return ((c.y - a.y) * (b.x - a.x) > (b.y - a.x) * (c.x - a.x));
|
||||
}
|
||||
|
||||
static inline bool gc_vector2_is_intersect(gc_vector2 a, gc_vector2 b, \
|
||||
gc_vector2 c, gc_vector2 d)
|
||||
{
|
||||
return (gc_vector2_is_ccw(a, c, d) != gc_vector2_is_ccw(b, c, d) &&\
|
||||
gc_vector2_is_ccw(a, b, c) != gc_vector2_is_ccw(a, b, d));
|
||||
}
|
||||
|
||||
bool map_manage_click(gc_engine *engine, int id, gc_vector2 pos);
|
||||
+6
-1
@@ -6,8 +6,10 @@
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "vector2.h"
|
||||
|
||||
typedef struct gc_engine gc_engine;
|
||||
typedef bool (*callback_t)(gc_engine *engine, int entity_id);
|
||||
typedef bool (*callback_t)(gc_engine *engine, int entity_id, gc_vector2 pos);
|
||||
|
||||
#ifndef ENGINE
|
||||
#define ENGINE
|
||||
@@ -55,6 +57,7 @@ struct gc_engine
|
||||
|
||||
void (*on_resize)(gc_engine *this, gc_vector2 size);
|
||||
gc_vector2 (*get_screen_size)(gc_engine *this);
|
||||
gc_vector2 (*get_cursor_pos)(gc_engine *this);
|
||||
};
|
||||
|
||||
gc_engine *engine_create(void);
|
||||
@@ -85,6 +88,8 @@ void engine_add_callback(gc_engine *engine, char *name, callback_t func);
|
||||
|
||||
int engine_use_sfml(gc_engine *engine, const char *title, int framerate);
|
||||
|
||||
gc_vector2 engine_get_cursor_pos(gc_engine *engine);
|
||||
|
||||
#define GETSYS(x) ((struct x *)engine->get_system(engine, #x))
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
** EPITECH PROJECT, 2020
|
||||
** Gamacon
|
||||
** File description:
|
||||
** map_managment
|
||||
*/
|
||||
|
||||
#include <SFML/Audio.h>
|
||||
#include <systems/sfml_renderer_system.h>
|
||||
#include "engine.h"
|
||||
#include "map_managment.h"
|
||||
#include <math.h>
|
||||
#include "vertex_component.h"
|
||||
#include "stdint.h"
|
||||
|
||||
#define ANGLE_X 45
|
||||
#define ANGLE_Y 35
|
||||
|
||||
gc_vector2 get_tile_coords_to_pixels(float x, float y, float z)
|
||||
{
|
||||
return (gc_vector2){
|
||||
cos(ANGLE_X) * y * 64 - cos(ANGLE_X) * x * 64,
|
||||
-(sin(ANGLE_Y) * x * 64 + sin(ANGLE_Y) * y * 64 - z)
|
||||
};
|
||||
}
|
||||
|
||||
int nb_intersection_between_vectors(gc_vector2 x1, gc_vector2 x2, gc_vector2 polygon[4])
|
||||
{
|
||||
int nb_intersects = 0;
|
||||
|
||||
//printf("pos x : %f y : %f\n", x1.x, x1.y);
|
||||
//printf("x2 x : %f y : %f\n", x2.x, x2.y);
|
||||
//printf("c1 x : %f y : %f\n", polygon[0].x, polygon[0].y);
|
||||
if (gc_vector2_is_intersect(x1, x2, polygon[0], polygon[1])) {
|
||||
nb_intersects++;
|
||||
printf("1 p0 : x:%f f:%f\n", polygon[0].x, polygon[0].y);
|
||||
printf("1 p1 : x:%f f:%f\n", polygon[1].x, polygon[1].y);
|
||||
}
|
||||
if (gc_vector2_is_intersect(x1, x2, polygon[1], polygon[3])) {
|
||||
nb_intersects++;
|
||||
printf("2 p1 : x:%f f:%f\n", polygon[1].x, polygon[1].y);
|
||||
printf("2 p2 : x:%f f:%f\n", polygon[3].x, polygon[3].y);
|
||||
}
|
||||
if (gc_vector2_is_intersect(x1, x2, polygon[3], polygon[2])) {
|
||||
nb_intersects++;
|
||||
printf("3 p2 : x:%f f:%f\n", polygon[2].x, polygon[2].y);
|
||||
printf("3 p3 : x:%f f:%f\n", polygon[3].x, polygon[3].y);
|
||||
}
|
||||
if (gc_vector2_is_intersect(x1, x2, polygon[2], polygon[0])) {
|
||||
nb_intersects++;
|
||||
printf("4 p2 : x:%f f:%f\n", polygon[2].x, polygon[2].y);
|
||||
printf("4 p0 : x:%f f:%f\n", polygon[0].x, polygon[0].y);
|
||||
}
|
||||
return (nb_intersects);
|
||||
}
|
||||
|
||||
bool is_pos_in_tile(gc_vector2 pos, struct tile *tile)
|
||||
{
|
||||
struct vertex **c = tile->corners;
|
||||
gc_vector2 inf = {INFINITY, INFINITY};
|
||||
gc_vector2 corners[4];
|
||||
int ret;
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
corners[i] = get_tile_coords_to_pixels(c[i]->x, c[i]->y, c[i]->z);
|
||||
}
|
||||
|
||||
ret = nb_intersection_between_vectors(pos, inf, corners);
|
||||
//my_printf("nb int : %i\n", ret);
|
||||
if (!ret || ret % 2 != 0)
|
||||
return (false);
|
||||
printf("The polygon true\n");
|
||||
for (int i = 0; i < 4; i++)
|
||||
printf("%i :(x:%f, y:%f)\n", i + 1, corners[i].x, corners[i].y);
|
||||
return (true);
|
||||
}
|
||||
|
||||
struct tile *get_tile_from_pos(gc_engine *engine, struct vertex_component *map, gc_vector2 pos)
|
||||
{
|
||||
for (int i = 0; map->map[i + 1].corners[0]->z != INT32_MIN; i++) {
|
||||
//printf("map fresh x: %i y: %i z: %i\n", map->map[i].corners[0]->x, map->map[i].corners[0]->y, map->map[i].corners[0]->z);
|
||||
if (is_pos_in_tile(pos, &map->map[i])) {
|
||||
printf("found \n");
|
||||
return (&map->map[i]);
|
||||
}
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
bool map_manage_click(gc_engine *engine, int id, gc_vector2 pos)
|
||||
{
|
||||
gc_scene *scene = engine->scene;
|
||||
gc_entity *entity = scene->get_entity(scene, id);
|
||||
struct vertex_component *map = entity->get_component(entity, "vertex_component");
|
||||
struct tile *ret;
|
||||
|
||||
printf("map_manage_click called id: %i coords x: %f y:%f\n", id, pos.x, pos.y);
|
||||
if (!map) {
|
||||
my_printf("aie\n");
|
||||
return (true);
|
||||
}
|
||||
ret = get_tile_from_pos(engine, map, pos);
|
||||
if (ret) {
|
||||
ret->corners[0]->z += 10;
|
||||
ret->corners[1]->z += 10;
|
||||
ret->corners[2]->z += 10;
|
||||
ret->corners[3]->z += 10;
|
||||
ret->texture = engine->scene->get_data(scene, "sprite", "command_block");
|
||||
}
|
||||
return (true);
|
||||
}
|
||||
@@ -59,7 +59,6 @@ static bool get_tiles(struct vertex_component *this, gc_scene *scene, node *n)
|
||||
}
|
||||
}
|
||||
this->map[inc].corners[0] = &this->vertices[v_x][vy];
|
||||
my_printf("final value z : %i\n", this->map[inc].corners[0]->z);
|
||||
return (true);
|
||||
}
|
||||
|
||||
@@ -72,12 +71,18 @@ static void ctr(void *component, va_list args)
|
||||
static void fdctr(gc_entity *entity, gc_scene *scene, void *component, node *n)
|
||||
{
|
||||
struct vertex_component *this = (struct vertex_component *)component;
|
||||
char *tilemap = xml_gettempprop(n, "tilemap");
|
||||
char *name = n->name;
|
||||
|
||||
this->vertices = NULL;
|
||||
this->map = NULL;
|
||||
n = xml_parse(xml_gettempprop(n, "tilemap"));
|
||||
if (!tilemap) {
|
||||
my_printf("gamacon: unable to find property 'tilemap' on %s\n", name);
|
||||
return;
|
||||
}
|
||||
n = xml_parse(tilemap);
|
||||
if (!n) {
|
||||
my_printf("XML parser unable to find property 'tilemap'\n");
|
||||
my_printf("gamacon: unable to find a valid tilemap at %s\n", tilemap);
|
||||
return;
|
||||
}
|
||||
if (!get_vertices(component, n) || !get_tiles(this, scene, n)) {
|
||||
|
||||
@@ -22,8 +22,15 @@ void engine_on_resize(gc_engine *engine, gc_vector2 size)
|
||||
(void)engine;
|
||||
(void)size;
|
||||
}
|
||||
|
||||
gc_vector2 engine_get_screen_size(gc_engine *this)
|
||||
{
|
||||
(void)this;
|
||||
return ((gc_vector2){0, 0});
|
||||
}
|
||||
|
||||
gc_vector2 engine_get_cursor_pos(gc_engine *this)
|
||||
{
|
||||
(void)this;
|
||||
return ((gc_vector2){0, 0});
|
||||
}
|
||||
+7
-1
@@ -61,6 +61,12 @@ void engine_destroy(gc_engine *engine)
|
||||
free(engine);
|
||||
}
|
||||
|
||||
gc_engine *engine_create_more(gc_engine *engine)
|
||||
{
|
||||
engine->get_cursor_pos = &engine_get_cursor_pos;
|
||||
return (engine);
|
||||
}
|
||||
|
||||
gc_engine *engine_create(void)
|
||||
{
|
||||
gc_engine *engine = malloc(sizeof(gc_engine));
|
||||
@@ -83,5 +89,5 @@ gc_engine *engine_create(void)
|
||||
engine_add_builtin_systems(engine);
|
||||
engine_add_buildin_components(engine);
|
||||
engine_init_dataloaders(engine);
|
||||
return (engine);
|
||||
return (engine_create_more(engine));
|
||||
}
|
||||
@@ -20,6 +20,7 @@
|
||||
#include "components/collision_component.h"
|
||||
#include "components/vertex_component.h"
|
||||
#include <stdlib.h>
|
||||
#include "components/clickable_component.h"
|
||||
|
||||
void engine_add_component(gc_engine *engine, const void *component)
|
||||
{
|
||||
@@ -42,5 +43,6 @@ void engine_add_buildin_components(gc_engine *engine)
|
||||
engine->add_component(engine, &friction_giver);
|
||||
engine->add_component(engine, &friction_component);
|
||||
engine->add_component(engine, &collision_component);
|
||||
engine->add_component(engine, &clickable_component);
|
||||
engine->add_component(engine, &vertex_component);
|
||||
}
|
||||
@@ -36,6 +36,12 @@ void sfml_handle_events(gc_engine *engine)
|
||||
}
|
||||
}
|
||||
|
||||
gc_vector2 engine_get_cursor_pos(gc_engine *engine)
|
||||
{
|
||||
sfMouseMoveEvent mouse;
|
||||
sfMouse_getPositionRenderWindow(GETSYS(sfml_renderer_system));
|
||||
}
|
||||
|
||||
void sfml_resize(gc_engine *engine, gc_vector2 size)
|
||||
{
|
||||
struct sfml_renderer_system *renderer = GETSYS(sfml_renderer_system);
|
||||
|
||||
@@ -22,8 +22,8 @@
|
||||
sfVector2f get_tile_coords(int x, int y, int z)
|
||||
{
|
||||
return (sfVector2f){
|
||||
cos(ANGLE_X) * y * 64 - cos (ANGLE_X) * x * 64,
|
||||
(sin(ANGLE_Y) * x * 64 + sin (ANGLE_Y) * y * 64 - z) + 250
|
||||
cos(ANGLE_X) * y * 64 - cos(ANGLE_X) * x * 64,
|
||||
(sin(ANGLE_Y) * x * 64 + sin(ANGLE_Y) * y * 64 - z)
|
||||
};
|
||||
}
|
||||
|
||||
@@ -76,10 +76,10 @@ struct vertex_component *info)
|
||||
for (i = 0; info->map[i].corners[0]->z != INT32_MIN; i++);
|
||||
//my_printf("zdzdzdzd\n");
|
||||
for (i--; i >= 0; i--) {
|
||||
//my_printf("mdr %i\n", i);
|
||||
corners[0] = info->map[i].corners[0]->x;
|
||||
corners[1] = info->map[i].corners[0]->y;
|
||||
corners[2] = info->map[i].corners[0]->z;
|
||||
//printf("mdr %i %i %i\n", corners[0], corners[1], corners[2]);
|
||||
//if (!info->map[i].corners[0]->y)
|
||||
// continue;
|
||||
vert0->position = get_tile_coords(corners[0], corners[1], corners[2]);
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
*/
|
||||
|
||||
#include "entity.h"
|
||||
#include "engine.h"
|
||||
#include "components/clickable_component.h"
|
||||
#include "components/transform_component.h"
|
||||
#include <stddef.h>
|
||||
@@ -29,7 +30,7 @@ void clickable_onclick(gc_engine *engine, gc_vector2 position)
|
||||
cl = GETCMP(((gc_entity *)ent->data), clickable_component);
|
||||
if (!cl->onclick)
|
||||
continue;
|
||||
if (cl->onclick(engine, ((gc_entity *)ent->data)->id))
|
||||
if (cl->onclick(engine, ((gc_entity *)ent->data)->id, position))
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user