feat: implemented level functions

and refactored Enemy and Player to spawn using level files
This commit is contained in:
Sara 2024-01-25 00:07:34 +01:00
parent 6035de1ad4
commit 05451c6ca3
8 changed files with 231 additions and 19 deletions

View file

@ -1,11 +1,13 @@
#include "Player.h"
#include "Enemy.h"
#include "level.h"
#include "program.h"
static
void play() {
SpawnPlayer(ZeroVector);
SpawnEnemy(MakeVector(1.f, 0.0f), EnemyIdle());
level_register_spawner("Player", SpawnPlayer);
level_register_spawner("Enemy", SpawnEnemy);
level_load_file("assets/test.sc");
}
static

View file

@ -6,6 +6,7 @@
#include "physics_world.h"
#include "program.h"
#include "sprite.h"
#include "variant.h"
START_REFLECT(Enemy)
REFLECT_TYPECLASS(Enemy, Transformable)
@ -77,19 +78,18 @@ Enemy* MakeEnemy() {
self->idleAnim = animation_sprite_new(self->sprite, spritesheet_load("assets/Player_Idle.png", IVectorFrom(512)), 1.5f, LoopMode_Loop);
self->walkAnim = animation_sprite_new(self->sprite, spritesheet_load("assets/Player_Walk.png", IVectorFrom(512)), 1.5f, LoopMode_Loop);
self->hurtAnim = animation_sprite_new(self->sprite, spritesheet_load("assets/Player_Hurt.png", IVectorFrom(512)), 5.f, LoopMode_Stop);
self->behaviour = state_machine_init(self, EnemyIdle());
return self;
}
Enemy* SpawnEnemy(Vector location, const State* entryState) {
BehaviourEntity SpawnEnemy(Dictionary* args) {
Variant value;
Enemy* self = MakeEnemy();
self->behaviour = state_machine_init(self, entryState);
// spawn at location
rigidbody_get_transform(self->rigidbody)->position = location;
// register into game world
game_world_add_entity(Enemy_as_BehaviourEntity(self));
physics_world_add_entity(Enemy_as_PhysicsEntity(self));
return self;
if(dictionary_try_get(args, "position", &value) && value.type == Variant_Vector)
rigidbody_get_transform(self->rigidbody)->position = value.as_vector;
return Enemy_as_BehaviourEntity(self);
}
void EnemyStart(Enemy* self) {}

View file

@ -1,6 +1,7 @@
#ifndef FIGHT_ENEMY_H
#define FIGHT_ENEMY_H
#include "dictionary.h"
#include "mirror.h"
#include "transform.h"
#include "state_machine.h"
@ -41,7 +42,7 @@ typedef struct Enemy {
} Enemy;
extern Enemy* MakeEnemy();
extern Enemy* SpawnEnemy(Vector location, const State* entryState);
extern BehaviourEntity SpawnEnemy(Dictionary* args);
extern void EnemyStart(Enemy* self);
extern void EnemyUpdate(Enemy* self, float deltaTime);

View file

@ -1,12 +1,13 @@
#include "Player.h"
#include "debug.h"
#include "game_world.h"
#include "dictionary.h"
#include "input_axis.h"
#include "physics_world.h"
#include "PlayerStates.h"
#include "Layers.h"
#include "program.h"
#include "variant.h"
const Vector PLAYER_SPEED = { 1.0f, 0.50f };
static const float PLAYER_INPUT_RATE = 1.f/15.f;
@ -131,15 +132,13 @@ Player* MakePlayer() {
return self;
}
Player* SpawnPlayer(Vector location) {
BehaviourEntity SpawnPlayer(Dictionary* args) {
Variant arg;
Player* self = MakePlayer();
self->transform.position = location;
game_world_add_entity(Player_as_BehaviourEntity(self));
physics_world_add_entity(Player_as_PhysicsEntity(self));
Internal_PlayerInitInput(self);
return self;
if(dictionary_try_get(args, "position", &arg) && arg.type == Variant_Vector)
self->transform.position = arg.as_vector;
return Player_as_BehaviourEntity(self);
}
void DestroyPlayer(Player* self) {

View file

@ -1,6 +1,7 @@
#ifndef FIGHT_PLAYER_H
#define FIGHT_PLAYER_H
#include "dictionary.h"
#include "state_machine.h"
#include "mirror.h"
#include "behaviour_entity.h"
@ -64,7 +65,7 @@ typedef struct Player {
} Player;
Player* MakePlayer();
Player* SpawnPlayer(Vector location);
BehaviourEntity SpawnPlayer(Dictionary* args);
void DestroyPlayer(Player* self);
void PlayerStart(Player* self);