From 14e006344e43c6b951b784699c098d10791c5689 Mon Sep 17 00:00:00 2001 From: Sara Date: Fri, 24 Nov 2023 23:20:25 +0100 Subject: [PATCH] feat(game): added prop object to represent simple static props --- game/src/Prop.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++ game/src/Prop.h | 56 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 game/src/Prop.c create mode 100644 game/src/Prop.h diff --git a/game/src/Prop.c b/game/src/Prop.c new file mode 100644 index 0000000..50d3c31 --- /dev/null +++ b/game/src/Prop.c @@ -0,0 +1,66 @@ +#include "Prop.h" +#include "debug.h" +#include "game_world.h" +#include "physics_world.h" + +Prop* MakeProp(Sprite* sprite, Shape* shape) { + Prop* self = malloc(sizeof(Prop)); + ASSERT_RETURN(self != NULL, NULL, "Failed to allocate space for Prop instance"); + *self = (Prop) { + .transform = IdentityTransform, + .sprite = sprite, + .rigidbody = NULL, + .collisionShape = shape + }; + self->rigidbody = rigidbody_make(Prop_as_Transformable(self)); + rigidbody_set_static(self->rigidbody, 1); + sprite_set_origin(self->sprite, MakeVector(0.5f, 1.0f)); + return self; +} + +Prop* SpawnProp(Vector location, Sprite* sprite, Shape* shape) { + Prop* self = MakeProp(sprite, shape); + + self->transform.position + = rigidbody_get_transform(self->rigidbody)->position + = location; + + game_world_add_entity(Prop_as_BehaviourEntity(self)); + physics_world_add_entity(Prop_as_PhysicsEntity(self)); + + return self; +} + +void DestroyProp(Prop* self) { + sprite_destroy(self->sprite); + rigidbody_destroy(self->rigidbody); + shape_destroy(self->collisionShape); + free(self); +} + +void PropStart(Prop* self) {} +void PropUpdate(Prop* self, float deltaTime) { + Vector vel = rigidbody_get_velocity(self->rigidbody); + LOG_INFO("Velocity: %f %f", vel.x, vel.y); + LOG_INFO("Position: %f %f", self->transform.position.x, self->transform.position.y); +} + +void PropDraw(Prop* self) { + sprite_draw(self->sprite, self->transform); + shape_draw(self->collisionShape, self->transform); +} + +void PropOnCollision(Prop* self, Collision collision) {} +void PropOnOverlap(Prop* self, PhysicsEntity other) {} + +Transform* PropGetTransform(Prop* self) { + return &self->transform; +} + +RigidBody* PropGetRigidBody(Prop* self) { + return self->rigidbody; +} + +Shape* PropGetCollisionShape(Prop* self) { + return self->collisionShape; +} diff --git a/game/src/Prop.h b/game/src/Prop.h new file mode 100644 index 0000000..333fcfe --- /dev/null +++ b/game/src/Prop.h @@ -0,0 +1,56 @@ +#ifndef FIGHT_PROP_H +#define FIGHT_PROP_H + +#include "physics_entity.h" +#include "behaviour_entity.h" +#include "transformable.h" +#include "drop.h" +#include "transform.h" +#include "sprite.h" +#include "rigidbody.h" +#include "shape.h" + +typedef struct Prop { + Transform transform; + Sprite* sprite; + RigidBody* rigidbody; + Shape* collisionShape; +} Prop; + +Prop* MakeProp(Sprite* sprite, Shape* shape); +Prop* SpawnProp(Vector location, Sprite* sprite, Shape* shape); +void DestroyProp(Prop* self); + +void PropStart(Prop* self); +void PropUpdate(Prop* self, float deltaTime); +void PropDraw(Prop* self); + +void PropOnCollision(Prop* self, Collision collision); +void PropOnOverlap(Prop* self, PhysicsEntity other); + +Transform* PropGetTransform(Prop* self); +RigidBody* PropGetRigidBody(Prop* self); +Shape* PropGetCollisionShape(Prop* self); + +impl_Transformable_for(Prop, + PropGetTransform +) + +impl_PhysicsEntity_for(Prop, + PropGetRigidBody, + PropGetCollisionShape, + PropOnCollision, + PropOnOverlap +) + +impl_Drop_for(Prop, + DestroyProp +) + +impl_BehaviourEntity_for(Prop, + PropStart, + PropUpdate, + PropDraw +) + +#endif // !FIGHT_PROP_H