From 15ff064243b2eaf206e9eb06157205d9d24ec963 Mon Sep 17 00:00:00 2001 From: shweet Date: Wed, 16 Aug 2023 09:19:53 -0400 Subject: [PATCH] oops --- CMakeLists.txt | 3 + src/COMMON.h | 16 +-- src/engine/fullscreen.c | 2 +- src/engine/mouse.c | 12 ++ src/engine/mouse.h | 3 +- src/engine/renderer.c | 2 +- src/game/ecs/component/COMPONENT_COMMON.h | 2 + .../action/component_rectangle_collide.c | 36 +++++ .../action/component_rectangle_collide.h | 38 +++++ .../ecs/component/action/component_return.c | 45 ++++++ .../ecs/component/action/component_return.h | 36 +++++ .../component/{ => action}/component_shoot.c | 26 ++-- .../component/{ => action}/component_shoot.h | 12 +- .../ecs/component/component_control_move.c | 130 ------------------ .../ecs/component/component_control_shoot.c | 17 --- .../ecs/component/component_game_object.c | 8 +- .../ecs/component/component_game_object.h | 2 - src/game/ecs/component/component_physics.h | 1 + .../component/control/component_control_aim.c | 24 ++++ .../component/control/component_control_aim.h | 31 +++++ .../control/component_control_move.c | 77 +++++++++++ .../{ => control}/component_control_move.h | 12 +- .../control/component_control_shoot.c | 39 ++++++ .../{ => control}/component_control_shoot.h | 14 +- .../ecs/component/{ => stat}/component_ammo.c | 10 +- .../ecs/component/{ => stat}/component_ammo.h | 10 +- .../component/{ => stat}/component_health.c | 8 +- .../component/{ => stat}/component_health.h | 10 +- .../component/{ => stat}/component_lifetime.c | 0 .../component/{ => stat}/component_lifetime.h | 4 +- src/game/ecs/ecs.h | 16 ++- src/game/ecs/entity/play/entity_player.c | 19 ++- src/game/ecs/entity/play/entity_player.h | 16 ++- src/game/ecs/entity/play/entity_snake.c | 13 +- src/game/ecs/entity/play/entity_snake.h | 6 +- src/game/game.c | 12 -- 36 files changed, 469 insertions(+), 243 deletions(-) create mode 100644 src/game/ecs/component/action/component_rectangle_collide.c create mode 100644 src/game/ecs/component/action/component_rectangle_collide.h create mode 100644 src/game/ecs/component/action/component_return.c create mode 100644 src/game/ecs/component/action/component_return.h rename src/game/ecs/component/{ => action}/component_shoot.c (53%) rename src/game/ecs/component/{ => action}/component_shoot.h (58%) delete mode 100644 src/game/ecs/component/component_control_move.c delete mode 100644 src/game/ecs/component/component_control_shoot.c create mode 100644 src/game/ecs/component/control/component_control_aim.c create mode 100644 src/game/ecs/component/control/component_control_aim.h create mode 100644 src/game/ecs/component/control/component_control_move.c rename src/game/ecs/component/{ => control}/component_control_move.h (70%) create mode 100644 src/game/ecs/component/control/component_control_shoot.c rename src/game/ecs/component/{ => control}/component_control_shoot.h (60%) rename src/game/ecs/component/{ => stat}/component_ammo.c (59%) rename src/game/ecs/component/{ => stat}/component_ammo.h (72%) rename src/game/ecs/component/{ => stat}/component_health.c (61%) rename src/game/ecs/component/{ => stat}/component_health.h (72%) rename src/game/ecs/component/{ => stat}/component_lifetime.c (100%) rename src/game/ecs/component/{ => stat}/component_lifetime.h (88%) diff --git a/CMakeLists.txt b/CMakeLists.txt index af65530..72da6b5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,6 +13,9 @@ file(GLOB src "${PROJECT_SOURCE_DIR}/src/game/*.c" "${PROJECT_SOURCE_DIR}/src/game/ecs/*.c" "${PROJECT_SOURCE_DIR}/src/game/ecs/component/*.c" + "${PROJECT_SOURCE_DIR}/src/game/ecs/component/action/*.c" + "${PROJECT_SOURCE_DIR}/src/game/ecs/component/control/*.c" + "${PROJECT_SOURCE_DIR}/src/game/ecs/component/stat/*.c" "${PROJECT_SOURCE_DIR}/src/game/ecs/entity/*.c" "${PROJECT_SOURCE_DIR}/src/game/ecs/entity/play/*.c" "${PROJECT_SOURCE_DIR}/src/game/ecs/entity/play/enemy*.c" diff --git a/src/COMMON.h b/src/COMMON.h index ea41472..ddab502 100644 --- a/src/COMMON.h +++ b/src/COMMON.h @@ -57,10 +57,9 @@ typedef enum OriginType ORIGIN_CENTER } OriginType; -#define DIRECTION_COUNT DIRECITON_NORTH_WEST + 1 +#define DIRECTION_COUNT DIRECTION_NORTH_WEST + 1 typedef enum Direction { - DIRECTION_NONE, DIRECTION_NORTH, DIRECTION_EAST, DIRECTION_SOUTH, @@ -73,13 +72,12 @@ typedef enum Direction static const f32 DIRECTION_ANGLES[DIRECTION_COUNT] = { - 0.0f, - PI + PI_HALF, - PI, PI - PI_HALF, + PI, + PI + PI_HALF, 0.0f, - TAU - PI_FOURTH, - PI_FOURTH, PI - PI_FOURTH, - PI + PI_FOURTH -} Direction; + PI + PI_FOURTH, + TAU - PI_FOURTH, + PI_FOURTH +}; diff --git a/src/engine/fullscreen.c b/src/engine/fullscreen.c index 960360d..4c6aacd 100644 --- a/src/engine/fullscreen.c +++ b/src/engine/fullscreen.c @@ -8,7 +8,7 @@ fullscreen_set(Window* self, Renderer* renderer) renderer_update(renderer); - SDL_SetWindowFullscreen(self->sdl, SDL_WINDOW_FULLSCREEN); + SDL_SetWindowFullscreen(self->sdl, SDL_WINDOW_FULLSCREEN_DESKTOP); } /* Exits fullscreen. */ diff --git a/src/engine/mouse.c b/src/engine/mouse.c index ccfc490..874abfd 100644 --- a/src/engine/mouse.c +++ b/src/engine/mouse.c @@ -59,3 +59,15 @@ mouse_release(Mouse* self, MouseButtonType type) { return (!self->current[type] && self->previous[type]); } + +/* Fetches the world position of the cursor. */ +void +mouse_world_position_get(Mouse* mouse, Renderer* renderer, vec2 position) +{ + vec2 mousePosition; + + mousePosition[0] = (f32)mouse->position[0]; + mousePosition[1] = (f32)mouse->position[1]; + + renderer_world_position_from_window_position(renderer, mousePosition, position); +} diff --git a/src/engine/mouse.h b/src/engine/mouse.h index 203004a..f179445 100644 --- a/src/engine/mouse.h +++ b/src/engine/mouse.h @@ -2,7 +2,7 @@ #include -#include "../COMMON.h" +#include "renderer.h" #define MOUSE_BUTTON_COUNT MOUSE_BUTTON_X2 + 1 typedef enum MouseButtonType @@ -26,3 +26,4 @@ void mouse_update(Mouse* self); bool mouse_press(Mouse* self, MouseButtonType type); bool mouse_held(Mouse* self, MouseButtonType type); bool mouse_release(Mouse* self, MouseButtonType type); +void mouse_world_position_get(Mouse* mouse, Renderer* renderer, vec2 position); diff --git a/src/engine/renderer.c b/src/engine/renderer.c index fe795b4..e58e9d1 100644 --- a/src/engine/renderer.c +++ b/src/engine/renderer.c @@ -100,5 +100,5 @@ renderer_world_position_from_window_position window_size_get(self->window, windowSize); rendererPosition[0] = (windowPosition[0] / windowSize[0]) * self->size[0]; - rendererPosition[1] = (windowPosition[1] / windowPosition[1]) * self->size[1]; + rendererPosition[1] = (windowPosition[1] / windowSize[1]) * self->size[1]; } diff --git a/src/game/ecs/component/COMPONENT_COMMON.h b/src/game/ecs/component/COMPONENT_COMMON.h index 9721f92..90bf061 100644 --- a/src/game/ecs/component/COMPONENT_COMMON.h +++ b/src/game/ecs/component/COMPONENT_COMMON.h @@ -7,8 +7,10 @@ typedef enum ECSComponentType { ECS_COMPONENT_PHYSICS, ECS_COMPONENT_SHOOT, + ECS_COMPONENT_RETURN, ECS_COMPONENT_CONTROL_MOVE, ECS_COMPONENT_CONTROL_SHOOT, + ECS_COMPONENT_CONTROL_AIM, ECS_COMPONENT_HEALTH, ECS_COMPONENT_AMMO, ECS_COMPONENT_SPRITE, diff --git a/src/game/ecs/component/action/component_rectangle_collide.c b/src/game/ecs/component/action/component_rectangle_collide.c new file mode 100644 index 0000000..690bab8 --- /dev/null +++ b/src/game/ecs/component/action/component_rectangle_collide.c @@ -0,0 +1,36 @@ +#include "component_collider.h" + +/* DEPENDENCIES: Physics */ + +/* Sets collider component info. */ +void +component_collider_init(ComponentCollider* self, vec2 offset, vec2 size) +{ + glm_vec2_copy(size, self->size); + glm_vec2_copy(offset, self->offset); + vector_init(&self->collisions, sizeof(u32); +} + +/* Ticks collider component. */ +void +component_collider_tick(ComponentCollider* self, ECS* ecs) +{ + ComponentPhysics* physics; + vec4 rectangle; + + vector_clear(&self->collisions); + + ecs_component_get(ecs, ECS_COMPONENT_PHYSICS, self->id); + + rectangle[0] = physics->position[0] + self->offset[0]; + rectangle[1] = physics->position[1] + self->offset[1]; + rectangle[2] = rectangle[0] + self->size[0]; + rectangle[3] = rectangle[1] + self->size[1]; + + for (s32 i = 0; i < ecs->lists[ECS_COMPONENT_COLLIDER]; i++) + { + + + + } +} diff --git a/src/game/ecs/component/action/component_rectangle_collide.h b/src/game/ecs/component/action/component_rectangle_collide.h new file mode 100644 index 0000000..32e177e --- /dev/null +++ b/src/game/ecs/component/action/component_rectangle_collide.h @@ -0,0 +1,38 @@ +#pragma once + +#include "../../../GAME_COMMON.h" +#include "../../ecs_entity.h" + +#include "../component_game_object.h" + +typedef void (*ProjectileInit)(ECS*, u32, vec3, u32); + +typedef struct ComponentShoot +{ + u32 id; + ProjectileInit projectileInit; + f32 offset; + f32 speed; + bool isFire; +} ComponentShoot; + +void component_shoot_init(ComponentShoot* self, ProjectileInit projectileInit, f32 speed, f32 offset); +void component_shoot_tick(ComponentShoot* self, ECS* ecs); + +static const ECSComponentInfo COMPONENT_SHOOT_INFO = +{ + .system = + { + .functions = + { + NULL, + NULL, + (ECSFunction)component_shoot_tick, + NULL + } + }, + .type = ECS_COMPONENT_SHOOT, + .size = sizeof(ComponentShoot) +}; + + diff --git a/src/game/ecs/component/action/component_return.c b/src/game/ecs/component/action/component_return.c new file mode 100644 index 0000000..be75394 --- /dev/null +++ b/src/game/ecs/component/action/component_return.c @@ -0,0 +1,45 @@ +#include "component_return.h" + +/* DEPENDENCIES: Physics */ + +/* Initializes return component. */ +void +component_return_init(ComponentReturn* self, f32 speed, f32 velocityMax, u32 lifetime, u32 senderID) +{ + self->speed = speed; + self->velocityMax = velocityMax; + self->lifetime = lifetime; + self->senderID = senderID; +} + +/* Ticks return component. */ +/* Rapidly accelerates entity towards sender. */ +void +component_return_tick(ComponentReturn* self, ECS* ecs) +{ + f32 angle; + ComponentPhysics* senderPhysics; + ComponentPhysics* physics; + + if (self->lifetime > 0) + { + self->lifetime--; + + if (self->lifetime <= 0) + { + physics = ecs_component_get(ecs, ECS_COMPONENT_PHYSICS, self->id); + + physics->velocityMax = self->velocityMax; + } + + return; + } + + senderPhysics = ecs_component_get(ecs, ECS_COMPONENT_PHYSICS, self->senderID); + physics = ecs_component_get(ecs, ECS_COMPONENT_PHYSICS, self->id); + + angle = ATAN(physics->position[0], senderPhysics->position[0], physics->position[1], senderPhysics->position[1]); + + physics->velocity[0] += cos(angle) * self->speed; + physics->velocity[1] += sin(angle) * self->speed; +} diff --git a/src/game/ecs/component/action/component_return.h b/src/game/ecs/component/action/component_return.h new file mode 100644 index 0000000..34e1155 --- /dev/null +++ b/src/game/ecs/component/action/component_return.h @@ -0,0 +1,36 @@ +#pragma once + +#include "../../../GAME_COMMON.h" +#include "../../ecs_entity.h" + +#include "../component_physics.h" + +typedef struct ComponentReturn +{ + u32 id; + f32 speed; + f32 velocityMax; + u32 lifetime; + u32 senderID; +} ComponentReturn; + +void component_return_init(ComponentReturn* self, f32 speed, f32 velocityMax, u32 lifetime, u32 senderID); +void component_return_tick(ComponentReturn* self, ECS* ecs); + +static const ECSComponentInfo COMPONENT_RETURN_INFO = +{ + .system = + { + .functions = + { + NULL, + NULL, + (ECSFunction)component_return_tick, + NULL + } + }, + .type = ECS_COMPONENT_RETURN, + .size = sizeof(ComponentReturn) +}; + + diff --git a/src/game/ecs/component/component_shoot.c b/src/game/ecs/component/action/component_shoot.c similarity index 53% rename from src/game/ecs/component/component_shoot.c rename to src/game/ecs/component/action/component_shoot.c index 8973578..d7e41db 100644 --- a/src/game/ecs/component/component_shoot.c +++ b/src/game/ecs/component/action/component_shoot.c @@ -7,7 +7,7 @@ void component_shoot_init ( ComponentShoot* self, - EntityGameObjectInit projectileInit, + ProjectileInit projectileInit, f32 speed, f32 offset ) @@ -23,20 +23,28 @@ component_shoot_tick(ComponentShoot* self, ECS* ecs) { if (self->isFire) { - //ComponentGameObject* gameObject; ComponentPhysics* physics; + ComponentPhysics* projectilePhysics; u32 projectile; vec3 position; - //vec3 velocity; - - //gameObject = ecs_component_get(ecs, ECS_COMPONENT_GAME_OBJECT, self->id); - physics = ecs_component_get(ecs, ECS_COMPONENT_PHYSICS, self->id); - - glm_vec3_copy(physics->position, position); projectile = ecs_entity_add(ecs); - self->projectileInit(ecs, projectile, position); + physics = ecs_component_get(ecs, ECS_COMPONENT_PHYSICS, self->id); + + position[0] += -cos(physics->angle) * self->offset; + position[1] += -sin(physics->angle) * self->offset; + + glm_vec3_copy(physics->position, position); + + self->projectileInit(ecs, projectile, position, self->id); + + projectilePhysics = ecs_component_get(ecs, ECS_COMPONENT_PHYSICS, projectile); + + projectilePhysics->angle = physics->angle; + + projectilePhysics->velocity[0] = -cos(projectilePhysics->angle) * self->speed; + projectilePhysics->velocity[1] = -sin(projectilePhysics->angle) * self->speed; self->isFire = false; } diff --git a/src/game/ecs/component/component_shoot.h b/src/game/ecs/component/action/component_shoot.h similarity index 58% rename from src/game/ecs/component/component_shoot.h rename to src/game/ecs/component/action/component_shoot.h index ce4776e..32e177e 100644 --- a/src/game/ecs/component/component_shoot.h +++ b/src/game/ecs/component/action/component_shoot.h @@ -1,20 +1,22 @@ #pragma once -#include "../../GAME_COMMON.h" -#include "../ecs_entity.h" +#include "../../../GAME_COMMON.h" +#include "../../ecs_entity.h" -#include "component_game_object.h" +#include "../component_game_object.h" + +typedef void (*ProjectileInit)(ECS*, u32, vec3, u32); typedef struct ComponentShoot { u32 id; - EntityGameObjectInit projectileInit; + ProjectileInit projectileInit; f32 offset; f32 speed; bool isFire; } ComponentShoot; -void component_shoot_init(ComponentShoot* self, EntityGameObjectInit projectileInit, f32 speed, f32 offset); +void component_shoot_init(ComponentShoot* self, ProjectileInit projectileInit, f32 speed, f32 offset); void component_shoot_tick(ComponentShoot* self, ECS* ecs); static const ECSComponentInfo COMPONENT_SHOOT_INFO = diff --git a/src/game/ecs/component/component_control_move.c b/src/game/ecs/component/component_control_move.c deleted file mode 100644 index 50f40f4..0000000 --- a/src/game/ecs/component/component_control_move.c +++ /dev/null @@ -1,130 +0,0 @@ -#include "component_control_move.h" - -/* DEPENDENCIES: Physics */ - -/* Ticks control_move component. */ -void -component_control_move_init(ComponentControlMove* self, f32 speed) -{ - self->speed = speed; -} - -/* Ticks control_move component. */ -void -component_control_move_tick(ComponentControlMove* self, ECS* ecs) -{ - if - ( - input_held(&ecs->game->input, INPUT_LEFT) || - input_held(&ecs->game->input, INPUT_RIGHT) || - input_held(&ecs->game->input, INPUT_UP) || - input_held(&ecs->game->input, INPUT_DOWN) - ) - { - ComponentPhysics* physics; - f32 angle; - vec2 velocity; - - angle = 0.0f; - - physics = ecs_component_get(ecs, ECS_COMPONENT_PHYSICS, self->id); - - glm_vec2_zero(velocity); - - self->previousDirection = self->direction; - self->direction = DIRECTION_NONE; - - velocity[0] = 1.0f; - velocity[1] = 0.0f; - - glm_vec2_norm(velocity); - - glm_vec2_scale(velocity, self->speed, velocity); - - - if - ( - input_held(&ecs->game->input, INPUT_LEFT) && - input_held(&ecs->game->input, INPUT_UP) - ) - { - angle = PI + PI_FOURTH; - self->direction = DIRECTION_NORTH_WEST; - } - else if - ( - input_held(&ecs->game->input, INPUT_RIGHT) && - input_held(&ecs->game->input, INPUT_UP) - ) - { - angle = TAU - PI_FOURTH; - self->direction = DIRECTION_NORTH_EAST; - } - else if - ( - input_held(&ecs->game->input, INPUT_DOWN) && - input_held(&ecs->game->input, INPUT_LEFT) - ) - { - angle = PI - PI_FOURTH; - self->direction = DIRECTION_SOUTH_WEST; - } - else if - ( - input_held(&ecs->game->input, INPUT_DOWN) && - input_held(&ecs->game->input, INPUT_RIGHT) - ) - { - angle = PI_FOURTH; - self->direction = DIRECTION_SOUTH_EAST; - } - else if - ( - input_held(&ecs->game->input, INPUT_LEFT) && - input_held(&ecs->game->input, INPUT_RIGHT) - ) - { - if - ( - self->direction = - self->previousDirection == INPUT_LEFT - ) - { - self->direction = DIRECTION_ - } - } - else if (input_held(&ecs->game->input, INPUT_LEFT)) - { - angle = PI; - self->direction = DIRECTION_WEST; - } - else if (input_held(&ecs->game->input, INPUT_RIGHT)) - { - angle = 0.0f; - self->direction = DIRECTION_EAST; - } - else if (input_held(&ecs->game->input, INPUT_UP)) - { - angle = PI + PI_HALF; - self->direction = DIRECTION_NORTH; - } - else if (input_held(&ecs->game->input, INPUT_DOWN)) - { - angle = PI - PI_HALF; - self->direction = DIRECTION_SOUTH; - } - - if (self->direction == DIRECITON_NORTH) - { - - - } - else if (self->direction == DIRECTION - - glm_vec2_rotate(velocity, angle, velocity); - - physics->velocity[0] += velocity[0]; - physics->velocity[1] += velocity[1]; - } - -} diff --git a/src/game/ecs/component/component_control_shoot.c b/src/game/ecs/component/component_control_shoot.c deleted file mode 100644 index 2d1925e..0000000 --- a/src/game/ecs/component/component_control_shoot.c +++ /dev/null @@ -1,17 +0,0 @@ -#include "component_control_shoot.h" - -/* DEPENDENCIES: Shoot */ - -/* Ticks control_shoot component. */ -void -component_control_shoot_tick(ComponentControlShoot* self, ECS* ecs) -{ - if (input_press(&ecs->game->input, INPUT_SHOOT)) - { - ComponentShoot* shoot; - - shoot = ecs_component_get(ecs, ECS_COMPONENT_SHOOT, self->id); - - shoot->isFire = true; - } -} diff --git a/src/game/ecs/component/component_game_object.c b/src/game/ecs/component/component_game_object.c index cdd6047..080f4fd 100644 --- a/src/game/ecs/component/component_game_object.c +++ b/src/game/ecs/component/component_game_object.c @@ -25,8 +25,6 @@ component_game_object_init frameSize[0] = (s32)texture.size[0]; frameSize[1] = (s32)texture.size[1]; - self->angle = 0.0f; - glm_ivec2_one(atlasSize); component_game_object_atlas_init @@ -78,12 +76,8 @@ component_game_object_atlas_init void component_game_object_add(ComponentGameObject* self, ECS* ecs) { - ComponentSprite* sprite; - ecs_component_add(ecs, ECS_COMPONENT_PHYSICS, self->id); - sprite = ecs_component_add(ecs, ECS_COMPONENT_SPRITE, self->id); - - sprite = ecs_component_get(ecs, ECS_COMPONENT_SPRITE, self->id); + ecs_component_add(ecs, ECS_COMPONENT_SPRITE, self->id); } /* Runs on game object add. */ diff --git a/src/game/ecs/component/component_game_object.h b/src/game/ecs/component/component_game_object.h index dc4043d..ba1acbe 100644 --- a/src/game/ecs/component/component_game_object.h +++ b/src/game/ecs/component/component_game_object.h @@ -7,12 +7,10 @@ #include "component_sprite.h" #include "component_physics.h" -typedef void (*EntityGameObjectInit)(ECS*, u32 id, vec3); typedef struct ComponentGameObject { u32 id; - f32 angle; } ComponentGameObject; void diff --git a/src/game/ecs/component/component_physics.h b/src/game/ecs/component/component_physics.h index 4599b86..12fb7ab 100644 --- a/src/game/ecs/component/component_physics.h +++ b/src/game/ecs/component/component_physics.h @@ -10,6 +10,7 @@ typedef struct ComponentPhysics f32 velocityMax; vec3 velocity; vec3 position; + f32 angle; } ComponentPhysics; void component_physics_init diff --git a/src/game/ecs/component/control/component_control_aim.c b/src/game/ecs/component/control/component_control_aim.c new file mode 100644 index 0000000..5c447ba --- /dev/null +++ b/src/game/ecs/component/control/component_control_aim.c @@ -0,0 +1,24 @@ +#include "component_control_aim.h" + +/* DEPENDENCIES: Physics */ + +/* Ticks control_aim component. Sets facing angle to that of the mouse. */ +void +component_control_aim_tick(ComponentControlAim* self, ECS* ecs) +{ + ComponentPhysics* physics; + f32 angle; + vec2 position; + + mouse_world_position_get(&ecs->game->input.mouse, &ecs->game->renderer, position); + + physics = ecs_component_get(ecs, ECS_COMPONENT_PHYSICS, self->id); + + angle = ATAN(position[0], physics->position[0], position[1], physics->position[1]); + + printf("MOUSE POSITION: %f %f\n", position[0], position[1]); + printf("PHYSICS POSITION: %f %f\n", physics->position[0], physics->position[1]); + printf("ANGLE: %f\n", angle); + + physics->angle = angle; +} diff --git a/src/game/ecs/component/control/component_control_aim.h b/src/game/ecs/component/control/component_control_aim.h new file mode 100644 index 0000000..14487b2 --- /dev/null +++ b/src/game/ecs/component/control/component_control_aim.h @@ -0,0 +1,31 @@ +#pragma once + +#include "../../../GAME_COMMON.h" +#include "../../ecs_entity.h" + +#include "../../../input/input.h" + +#include "../component_physics.h" + +typedef struct ComponentControlAim +{ + u32 id; +} ComponentControlAim; + +void component_control_aim_tick(ComponentControlAim* self, ECS* ecs); + +static const ECSComponentInfo COMPONENT_CONTROL_AIM_INFO = +{ + .system = + { + .functions = + { + NULL, + NULL, + (ECSFunction)component_control_aim_tick, + NULL + } + }, + .type = ECS_COMPONENT_CONTROL_AIM, + .size = sizeof(ComponentControlAim) +}; diff --git a/src/game/ecs/component/control/component_control_move.c b/src/game/ecs/component/control/component_control_move.c new file mode 100644 index 0000000..5af8e2b --- /dev/null +++ b/src/game/ecs/component/control/component_control_move.c @@ -0,0 +1,77 @@ +#include "component_control_move.h" + +/* DEPENDENCIES: Physics */ + +static void _direction_update(ComponentControlMove* self, ECS* ecs); + +/* Updates current direction. */ +static void +_direction_update(ComponentControlMove* self, ECS* ecs) +{ + if + ( + input_held(&ecs->game->input, INPUT_LEFT) && + input_held(&ecs->game->input, INPUT_UP) + ) + self->direction = DIRECTION_NORTH_WEST; + else if + ( + input_held(&ecs->game->input, INPUT_RIGHT) && + input_held(&ecs->game->input, INPUT_UP) + ) + self->direction = DIRECTION_NORTH_EAST; + else if + ( + input_held(&ecs->game->input, INPUT_DOWN) && + input_held(&ecs->game->input, INPUT_LEFT) + ) + self->direction = DIRECTION_SOUTH_WEST; + else if + ( + input_held(&ecs->game->input, INPUT_DOWN) && + input_held(&ecs->game->input, INPUT_RIGHT) + ) + self->direction = DIRECTION_SOUTH_EAST; + else if (input_held(&ecs->game->input, INPUT_UP)) + self->direction = DIRECTION_NORTH; + else if (input_held(&ecs->game->input, INPUT_DOWN)) + self->direction = DIRECTION_SOUTH; + else if (input_held(&ecs->game->input, INPUT_LEFT)) + self->direction = DIRECTION_WEST; + else if (input_held(&ecs->game->input, INPUT_RIGHT)) + self->direction = DIRECTION_EAST; +} + +/* Ticks control_move component. */ +void +component_control_move_init(ComponentControlMove* self, f32 speed) +{ + self->speed = speed; +} + +/* Ticks control_move component. */ +void +component_control_move_tick(ComponentControlMove* self, ECS* ecs) +{ + if + ( + input_held(&ecs->game->input, INPUT_LEFT) || + input_held(&ecs->game->input, INPUT_RIGHT) || + input_held(&ecs->game->input, INPUT_UP) || + input_held(&ecs->game->input, INPUT_DOWN) + ) + { + ComponentPhysics* physics; + vec2 delta; + + physics = ecs_component_get(ecs, ECS_COMPONENT_PHYSICS, self->id); + + _direction_update(self, ecs); + + delta[0] = -cos(DIRECTION_ANGLES[self->direction]) * self->speed; + delta[1] = -sin(DIRECTION_ANGLES[self->direction]) * self->speed; + + physics->velocity[0] += delta[0]; + physics->velocity[1] += delta[1]; + } +} diff --git a/src/game/ecs/component/component_control_move.h b/src/game/ecs/component/control/component_control_move.h similarity index 70% rename from src/game/ecs/component/component_control_move.h rename to src/game/ecs/component/control/component_control_move.h index b4d27ed..c804e6d 100644 --- a/src/game/ecs/component/component_control_move.h +++ b/src/game/ecs/component/control/component_control_move.h @@ -1,18 +1,18 @@ #pragma once -#include "../../GAME_COMMON.h" -#include "../ecs_entity.h" +#include "../../../GAME_COMMON.h" +#include "../../ecs_entity.h" +#include "../../../input/input.h" -#include "../../input/input.h" - -#include "component_physics.h" +#include "../component_physics.h" typedef struct ComponentControlMove { u32 id; f32 speed; Direction direction; - Direction oldDirection; + bool directionInput[DIRECTION_COUNT]; + bool previousDirectionInput[DIRECTION_COUNT]; } ComponentControlMove; void component_control_move_init(ComponentControlMove* self, f32 speed); diff --git a/src/game/ecs/component/control/component_control_shoot.c b/src/game/ecs/component/control/component_control_shoot.c new file mode 100644 index 0000000..22b8b1c --- /dev/null +++ b/src/game/ecs/component/control/component_control_shoot.c @@ -0,0 +1,39 @@ +#include "component_control_shoot.h" + +/* DEPENDENCIES: Shoot, Ammo */ + +/* Initializes control shoot component. */ +void +component_control_shoot_init(ComponentControlShoot* self, u32 delay) +{ + self->delayMax = delay; + self->delay = self->delayMax; +} + +/* Ticks control_shoot component. */ +void +component_control_shoot_tick(ComponentControlShoot* self, ECS* ecs) +{ + ComponentAmmo* ammo; + + if (self->delay > 0) + { + self->delay--; + return; + } + + ammo = ecs_component_get(ecs, ECS_COMPONENT_AMMO, self->id); + + if (input_held(&ecs->game->input, INPUT_SHOOT) && !ammo->isOut) + { + ComponentShoot* shoot; + + shoot = ecs_component_get(ecs, ECS_COMPONENT_SHOOT, self->id); + + shoot->isFire = true; + + self->delay = self->delayMax; + + ammo->value--; + } +} diff --git a/src/game/ecs/component/component_control_shoot.h b/src/game/ecs/component/control/component_control_shoot.h similarity index 60% rename from src/game/ecs/component/component_control_shoot.h rename to src/game/ecs/component/control/component_control_shoot.h index 9bcb062..7312c03 100644 --- a/src/game/ecs/component/component_control_shoot.h +++ b/src/game/ecs/component/control/component_control_shoot.h @@ -1,17 +1,19 @@ #pragma once -#include "../../GAME_COMMON.h" -#include "../ecs_entity.h" - -#include "../../input/input.h" - -#include "component_shoot.h" +#include "../../../GAME_COMMON.h" +#include "../../ecs_entity.h" +#include "../../../input/input.h" +#include "../action/component_shoot.h" +#include "../stat/component_ammo.h" typedef struct ComponentControlShoot { u32 id; + u32 delayMax; + u32 delay; } ComponentControlShoot; +void component_control_shoot_init(ComponentControlShoot* self, u32 delay); void component_control_shoot_tick(ComponentControlShoot* self, ECS* ecs); static const ECSComponentInfo COMPONENT_CONTROL_SHOOT_INFO = diff --git a/src/game/ecs/component/component_ammo.c b/src/game/ecs/component/stat/component_ammo.c similarity index 59% rename from src/game/ecs/component/component_ammo.c rename to src/game/ecs/component/stat/component_ammo.c index a8448c9..f8c1125 100644 --- a/src/game/ecs/component/component_ammo.c +++ b/src/game/ecs/component/stat/component_ammo.c @@ -2,16 +2,18 @@ /* Sets ammo component info. */ void -component_ammo_init(ComponentAmmo* self, u32 ammo) +component_ammo_init(ComponentAmmo* self, u32 max) { - self->ammoMax = ammo; - self->ammo = self->ammoMax; + self->max = max; + self->value = self->max; } /* Ticks ammo component. If ammo is depleted, marks as out. */ void component_ammo_tick(ComponentAmmo* self, ECS* ecs) { - if (self->ammo <= 0) + if (self->value <= 0) self->isOut = true; + else + self->isOut = false; } diff --git a/src/game/ecs/component/component_ammo.h b/src/game/ecs/component/stat/component_ammo.h similarity index 72% rename from src/game/ecs/component/component_ammo.h rename to src/game/ecs/component/stat/component_ammo.h index 8972f13..3af12eb 100644 --- a/src/game/ecs/component/component_ammo.h +++ b/src/game/ecs/component/stat/component_ammo.h @@ -1,17 +1,17 @@ #pragma once -#include "../../GAME_COMMON.h" -#include "../ecs_entity.h" +#include "../../../GAME_COMMON.h" +#include "../../ecs_entity.h" typedef struct ComponentAmmo { u32 id; - u32 ammoMax; - u32 ammo; + u32 max; + u32 value; bool isOut; } ComponentAmmo; -void component_ammo_init(ComponentAmmo* self, u32 ammo); +void component_ammo_init(ComponentAmmo* self, u32 max); void component_ammo_tick(ComponentAmmo* self, ECS* ecs); static const ECSComponentInfo COMPONENT_AMMO_INFO = diff --git a/src/game/ecs/component/component_health.c b/src/game/ecs/component/stat/component_health.c similarity index 61% rename from src/game/ecs/component/component_health.c rename to src/game/ecs/component/stat/component_health.c index fa6a49a..30830f5 100644 --- a/src/game/ecs/component/component_health.c +++ b/src/game/ecs/component/stat/component_health.c @@ -2,16 +2,16 @@ /* Sets health component info. */ void -component_health_init(ComponentHealth* self, u32 health) +component_health_init(ComponentHealth* self, u32 max) { - self->healthMax = health; - self->health = self->healthMax; + self->max = max; + self->value = self->max; } /* Ticks health component. If health is depleted, marked dead. */ void component_health_tick(ComponentHealth* self, ECS* ecs) { - if (self->health <= 0) + if (self->value <= 0) self->isDead = true; } diff --git a/src/game/ecs/component/component_health.h b/src/game/ecs/component/stat/component_health.h similarity index 72% rename from src/game/ecs/component/component_health.h rename to src/game/ecs/component/stat/component_health.h index fbff33b..786ae88 100644 --- a/src/game/ecs/component/component_health.h +++ b/src/game/ecs/component/stat/component_health.h @@ -1,17 +1,17 @@ #pragma once -#include "../../GAME_COMMON.h" -#include "../ecs_entity.h" +#include "../../../GAME_COMMON.h" +#include "../../ecs_entity.h" typedef struct ComponentHealth { u32 id; - u32 healthMax; - u32 health; + u32 max; + u32 value; bool isDead; } ComponentHealth; -void component_health_init(ComponentHealth* self, u32 health); +void component_health_init(ComponentHealth* self, u32 max); void component_health_tick(ComponentHealth* self, ECS* ecs); static const ECSComponentInfo COMPONENT_HEALTH_INFO = diff --git a/src/game/ecs/component/component_lifetime.c b/src/game/ecs/component/stat/component_lifetime.c similarity index 100% rename from src/game/ecs/component/component_lifetime.c rename to src/game/ecs/component/stat/component_lifetime.c diff --git a/src/game/ecs/component/component_lifetime.h b/src/game/ecs/component/stat/component_lifetime.h similarity index 88% rename from src/game/ecs/component/component_lifetime.h rename to src/game/ecs/component/stat/component_lifetime.h index bb543eb..a1e2c42 100644 --- a/src/game/ecs/component/component_lifetime.h +++ b/src/game/ecs/component/stat/component_lifetime.h @@ -1,7 +1,7 @@ #pragma once -#include "../../GAME_COMMON.h" -#include "../ecs_entity.h" +#include "../../../GAME_COMMON.h" +#include "../../ecs_entity.h" typedef struct ComponentLifetime { diff --git a/src/game/ecs/ecs.h b/src/game/ecs/ecs.h index 32c9866..bcf52f4 100644 --- a/src/game/ecs/ecs.h +++ b/src/game/ecs/ecs.h @@ -1,14 +1,16 @@ #pragma once -#include "component/component_ammo.h" -#include "component/component_shoot.h" -#include "component/component_control_shoot.h" -#include "component/component_control_move.h" +#include "component/action/component_shoot.h" +#include "component/action/component_return.h" #include "component/component_game_object.h" -#include "component/component_health.h" -#include "component/component_lifetime.h" #include "component/component_physics.h" #include "component/component_sprite.h" +#include "component/control/component_control_aim.h" +#include "component/control/component_control_move.h" +#include "component/control/component_control_shoot.h" +#include "component/stat/component_ammo.h" +#include "component/stat/component_health.h" +#include "component/stat/component_lifetime.h" #include "ecs_component.h" @@ -16,8 +18,10 @@ static const ECSComponentInfo ECS_COMPONENT_INFO[ECS_COMPONENT_COUNT] = { COMPONENT_PHYSICS_INFO, COMPONENT_SHOOT_INFO, + COMPONENT_RETURN_INFO, COMPONENT_CONTROL_MOVE_INFO, COMPONENT_CONTROL_SHOOT_INFO, + COMPONENT_CONTROL_AIM_INFO, COMPONENT_HEALTH_INFO, COMPONENT_AMMO_INFO, COMPONENT_SPRITE_INFO, diff --git a/src/game/ecs/entity/play/entity_player.c b/src/game/ecs/entity/play/entity_player.c index 710b779..d054abd 100644 --- a/src/game/ecs/entity/play/entity_player.c +++ b/src/game/ecs/entity/play/entity_player.c @@ -4,15 +4,21 @@ void entity_player_init(ECS* ecs, u32 id, vec3 position) { - ComponentGameObject* gameObject; + ComponentAmmo* ammo; ComponentControlMove* controlMove; + ComponentControlShoot* controlShoot; + ComponentGameObject* gameObject; + ComponentHealth* health; ComponentShoot* shoot; - gameObject = ecs_component_add(ecs, ECS_COMPONENT_GAME_OBJECT, id); + ammo = ecs_component_add(ecs, ECS_COMPONENT_AMMO, id); controlMove = ecs_component_add(ecs, ECS_COMPONENT_CONTROL_MOVE, id); + controlShoot = ecs_component_add(ecs, ECS_COMPONENT_CONTROL_SHOOT, id); + gameObject = ecs_component_add(ecs, ECS_COMPONENT_GAME_OBJECT, id); + health = ecs_component_add(ecs, ECS_COMPONENT_HEALTH, id); shoot = ecs_component_add(ecs, ECS_COMPONENT_SHOOT, id); - ecs_component_add(ecs, ECS_COMPONENT_CONTROL_SHOOT, id); + ecs_component_add(ecs, ECS_COMPONENT_CONTROL_AIM, id); component_game_object_init ( @@ -33,8 +39,13 @@ entity_player_init(ECS* ecs, u32 id, vec3 position) component_shoot_init ( shoot, - (EntityGameObjectInit)entity_snake_init, + (ProjectileInit)entity_snake_init, PLAYER_SHOOT_SPEED, PLAYER_SHOOT_OFFSET ); + + component_control_shoot_init(controlShoot, PLAYER_CONTROL_SHOOT_DELAY); + + component_health_init(health, PLAYER_HEALTH_MAX); + component_ammo_init(ammo, PLAYER_AMMO_MAX); } diff --git a/src/game/ecs/entity/play/entity_player.h b/src/game/ecs/entity/play/entity_player.h index 2c2feb3..8fa4cb0 100644 --- a/src/game/ecs/entity/play/entity_player.h +++ b/src/game/ecs/entity/play/entity_player.h @@ -1,11 +1,14 @@ #pragma once -#include "../../ecs_component.h" +#include "../../component/action/component_shoot.h" #include "../../component/component_game_object.h" -#include "../../component/component_control_move.h" -#include "../../component/component_control_shoot.h" -#include "../../component/component_shoot.h" -#include "../../component/component_lifetime.h" +#include "../../component/control/component_control_aim.h" +#include "../../component/control/component_control_move.h" +#include "../../component/control/component_control_shoot.h" +#include "../../component/stat/component_ammo.h" +#include "../../component/stat/component_health.h" +#include "../../component/stat/component_lifetime.h" +#include "../../ecs_component.h" #include "entity_snake.h" @@ -14,6 +17,9 @@ #define PLAYER_VELOCITY_MAX 6 #define PLAYER_SHOOT_SPEED 5 #define PLAYER_SHOOT_OFFSET 32 +#define PLAYER_CONTROL_SHOOT_DELAY 15 +#define PLAYER_AMMO_MAX 1 +#define PLAYER_HEALTH_MAX 3 static const vec2 PLAYER_SIZE = {50.0f, 100.0f}; void entity_player_init(ECS* ecs, u32 id, vec3 position); diff --git a/src/game/ecs/entity/play/entity_snake.c b/src/game/ecs/entity/play/entity_snake.c index 52ded84..d371c82 100644 --- a/src/game/ecs/entity/play/entity_snake.c +++ b/src/game/ecs/entity/play/entity_snake.c @@ -2,11 +2,13 @@ /* Initializes a snake entity. */ void -entity_snake_init(ECS* ecs, u32 id, vec3 position) +entity_snake_init(ECS* ecs, u32 id, vec3 position, u32 sender) { ComponentGameObject* gameObject; + ComponentReturn* returnComponent; gameObject = ecs_component_add(ecs, ECS_COMPONENT_GAME_OBJECT, id); + returnComponent = ecs_component_add(ecs, ECS_COMPONENT_RETURN, id); component_game_object_init ( @@ -21,4 +23,13 @@ entity_snake_init(ECS* ecs, u32 id, vec3 position) SNAKE_FRICTION, SNAKE_VELOCITY_MAX ); + + component_return_init + ( + returnComponent, + SNAKE_RETURN_SPEED, + SNAKE_RETURN_VELOCITY_MAX, + SNAKE_RETURN_LIFETIME, + sender + ); } diff --git a/src/game/ecs/entity/play/entity_snake.h b/src/game/ecs/entity/play/entity_snake.h index 8ef4a0f..ef6ef86 100644 --- a/src/game/ecs/entity/play/entity_snake.h +++ b/src/game/ecs/entity/play/entity_snake.h @@ -2,10 +2,14 @@ #include "../../ecs_component.h" #include "../../component/component_game_object.h" +#include "../../component/action/component_return.h" #define SNAKE_SPEED 1 #define SNAKE_FRICTION 1 #define SNAKE_VELOCITY_MAX 50 +#define SNAKE_RETURN_SPEED 1 +#define SNAKE_RETURN_VELOCITY_MAX 100 +#define SNAKE_RETURN_LIFETIME 20 static const vec2 SNAKE_SIZE = {25.0f, 25.0f}; -void entity_snake_init(ECS* ecs, u32 id, vec3 position); +void entity_snake_init(ECS* ecs, u32 id, vec3 position, u32 sender); diff --git a/src/game/game.c b/src/game/game.c index 0df27bd..a1a7a95 100644 --- a/src/game/game.c +++ b/src/game/game.c @@ -34,18 +34,6 @@ _game_tick(Game* self) if (event_press(&self->input.event, EVENT_QUIT)) _game_quit(self); - if (self->isFullscreen) - { - if (keyboard_press(&self->input.keyboard, KEYBOARD_KEY_F)) - fullscreen_exit(&self->window, &self->renderer); - } - else - { - if (keyboard_press(&self->input.keyboard, KEYBOARD_KEY_F)) - fullscreen_set(&self->window, &self->renderer); - } - - vec2 position; vec2 mousePosition;