feat(game): added prop object to represent simple static props

This commit is contained in:
Sara 2023-11-24 23:20:25 +01:00
parent b2dda356a1
commit 14e006344e
2 changed files with 122 additions and 0 deletions

66
game/src/Prop.c Normal file
View file

@ -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;
}

56
game/src/Prop.h Normal file
View file

@ -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