Added player module, player init, update and visual representation. Fixed bugs in assigning default texture at material init and setting parent when creating entities in scene

dev
Shariq Shah 7 years ago
parent 4a2b6e723c
commit 6b105f467c
  1. 4
      README.md
  2. 3
      src/game/config_vars.c
  3. 3
      src/libsymmetry/editor.c
  4. 1
      src/libsymmetry/editor.h
  5. 5
      src/libsymmetry/entity.h
  6. 41
      src/libsymmetry/game.c
  7. 2
      src/libsymmetry/game.h
  8. 4
      src/libsymmetry/material.c
  9. 62
      src/libsymmetry/player.c
  10. 10
      src/libsymmetry/player.h
  11. 30
      src/libsymmetry/scene.c
  12. 2
      src/libsymmetry/scene.h

@ -155,6 +155,9 @@
- ## TODO
- Switching between editor and game mode/cameras
- Player projectiles and sounds
- NPR and cross-hatching
- Remove model and replace all usages with static mesh
- Get editor camera speed and other settings from config file
- Re-Implement player logic
@ -397,3 +400,4 @@
* Completed Phase 1 of codebase refactoring
* Improved editor camera handling
* Re-implemented showing all the entities in the editor
* Player init, update, visual representation and movement

@ -33,6 +33,9 @@ void config_vars_init(void)
hashmap_int_set(cvars, "video_driver_linux", VD_WAYLAND);
hashmap_int_set(cvars, "debug_draw_mode", 0);
hashmap_vec4_setf(cvars, "debug_draw_color", 1.f, 0.f, 0.f, 1.f);
hashmap_float_set(cvars, "player_move_speed", 10.f);
hashmap_float_set(cvars, "player_move_speed_multiplier", 2.f);
hashmap_float_set(cvars, "player_turn_speed", 5.f);
}
void config_vars_cleanup(void)

@ -77,7 +77,10 @@ void editor_init(void)
editor_state.camera_sprint_multiplier = 2.f;
debug_vars_list = array_new(struct Debug_Variable);
empty_indices = array_new(int);
}
void editor_init_camera(void)
{
struct Camera* editor_camera = &game_state_get()->scene->cameras[CAM_EDITOR];
entity_rename(editor_camera, "Editor_Camera");
editor_camera->base.active = true;

@ -4,6 +4,7 @@
#include "../common/linmath.h"
void editor_init(void);
void editor_init_camera(void);
void editor_update(float dt);
void editor_toggle(void);
void editor_cleanup(void);

@ -144,6 +144,11 @@ struct Static_Mesh
struct Player
{
struct Entity base;
struct Static_Mesh* mesh;
struct Camera* camera_node;
float move_speed;
float move_speed_multiplier;
float turn_speed;
};
void entity_init(struct Entity* entity, const char* name, struct Entity* parent);

@ -40,12 +40,12 @@
#define MAX_FRAME_TIME 0.5f
static bool run(void);
static void update(float dt, bool* window_should_close);
static void render(void);
static void debug(float dt);
static void debug_gui(float dt);
static void scene_setup(void);
static bool game_run(void);
static void game_update(float dt, bool* window_should_close);
static void game_render(void);
static void game_debug(float dt);
static void game_debug_gui(float dt);
static void game_scene_setup(void);
static void on_box_move(Rigidbody body);
static void on_collision_test(struct Entity* this_ent, struct Entity* other_ent, Rigidbody body, Rigidbody body2);
@ -74,8 +74,6 @@ bool game_init(struct Window* window, struct Platform_Api* platform_api)
else
{
game_state->window = window;
game_state->player_node = NULL;
game_state->player_pitch_node = NULL;
game_state->is_initialized = false;
game_state->renderer = malloc(sizeof(*game_state->renderer));
game_state->scene = malloc(sizeof(*game_state->scene));
@ -102,18 +100,18 @@ bool game_init(struct Window* window, struct Platform_Api* platform_api)
platform->physics.body_set_moved_callback(entity_rigidbody_on_move);
platform->physics.body_set_collision_callback(entity_rigidbody_on_collision);
scene_init(game_state->scene);
editor_init();
renderer_init(game_state->renderer);
scene_init(game_state->scene);
}
/* Debug scene setup */
scene_setup();
game_scene_setup();
game_state->is_initialized = true;
return run();
return game_run();
}
void scene_setup(void)
void game_scene_setup(void)
{
// struct Entity* player = scene_add_new("player", ET_CAMERA);
// game_state->player_node = player->id;
@ -294,7 +292,7 @@ void scene_setup(void)
transform_translate(light, &light_pos, TS_WORLD);
}
void debug(float dt)
void game_debug(float dt)
{
if(input_is_key_pressed(KEY_SPACE))
{
@ -460,7 +458,7 @@ void debug(float dt)
}
}
bool run(void)
bool game_run(void)
{
uint32 last_time = platform->ticks_get();
bool should_window_close = 0;
@ -475,9 +473,9 @@ bool run(void)
platform->poll_events(&should_window_close);
gui_input_end();
update(delta_time, &should_window_close);
game_update(delta_time, &should_window_close);
platform->physics.step(delta_time);
render();
game_render();
platform->window.swap_buffers(game_state->window);
scene_post_update(game_state->scene);
platform->sound.update_3d();
@ -485,7 +483,7 @@ bool run(void)
return true;
}
void update(float dt, bool* window_should_close)
void game_update(float dt, bool* window_should_close)
{
if(input_is_key_pressed(KEY_ESCAPE)) *window_should_close = true;
if(input_map_state_get("Editor_Toggle", KS_RELEASED)) editor_toggle();
@ -498,14 +496,15 @@ void update(float dt, bool* window_should_close)
return;
}
debug(dt);
game_debug(dt);
//debug_gui(dt);
scene_update(game_state->scene, dt);
editor_update(dt);
input_update(); /* This should always be the last thing. Probably
input_update(); /* This should always be the last thing(Why??). Probably
* put this in post update? */
}
void debug_gui(float dt)
void game_debug_gui(float dt)
{
struct Gui_State* gui_state = gui_state_get();
struct nk_context* ctx = &gui_state->context;
@ -1709,7 +1708,7 @@ void debug_gui(float dt)
}
void render(void)
void game_render(void)
{
renderer_draw(game_state->renderer, game_state->scene);
}

@ -20,8 +20,6 @@ struct Game_State
{
bool is_initialized;
struct Window* window;
struct Player* player_node;
struct Entity* player_pitch_node;
struct Renderer* renderer;
struct Scene* scene;
};

@ -127,7 +127,7 @@ bool material_register_static_mesh(struct Material* material, struct Static_Mesh
{
variant_assign_vec4f(&mesh->model.material_params[MMP_DIFFUSE_COL], 1.f, 0.f, 1.f, 1.f);
variant_assign_float(&mesh->model.material_params[MMP_DIFFUSE], 1.f);
variant_assign_int(&mesh->model.material_params[MMP_DIFFUSE_TEX], texture_find("default.tga"));
variant_assign_int(&mesh->model.material_params[MMP_DIFFUSE_TEX], texture_create_from_file("default.tga", TU_DIFFUSE));
variant_assign_float(&mesh->model.material_params[MMP_SPECULAR], 1.f);
variant_assign_float(&mesh->model.material_params[MMP_SPECULAR_STRENGTH], 50.f);
}
@ -135,7 +135,7 @@ bool material_register_static_mesh(struct Material* material, struct Static_Mesh
case MAT_UNSHADED:
{
variant_assign_vec4f(&mesh->model.material_params[MMP_DIFFUSE_COL], 1.f, 0.f, 1.f, 1.f);
variant_assign_int(&mesh->model.material_params[MMP_DIFFUSE_TEX], texture_find("default.tga"));
variant_assign_int(&mesh->model.material_params[MMP_DIFFUSE_TEX], texture_create_from_file("default.tga", TU_DIFFUSE));
}
break;
default:

@ -0,0 +1,62 @@
#include "player.h"
#include "scene.h"
#include "input.h"
#include "../common/utils.h"
#include "transform.h"
#include "../common/common.h"
void player_init(struct Player* player, struct Scene* scene)
{
entity_init(player, "Player", &scene->root_entity);
player->base.active = true;
player->base.id = 1;
player->base.type = ET_PLAYER;
struct Hashmap* config = platform->config.get();
player->move_speed = hashmap_int_get(config, "player_move_speed");
player->move_speed_multiplier = hashmap_int_get(config, "player_move_speed_multiplier");
player->turn_speed = hashmap_int_get(config, "player_turn_speed");
player->mesh = scene_static_mesh_create(scene, "Player_Mesh", player, "default.pamesh", MAT_BLINN);
struct Camera* player_camera = &scene->cameras[CAM_GAME];
entity_rename(player_camera, "Player_Camera");
player_camera->base.active = true;
player_camera->clear_color.x = 0.6f;
player_camera->clear_color.y = 0.6f;
player_camera->clear_color.z = 0.9f;
player_camera->clear_color.w = 1.f;
player->camera_node = player_camera;
int render_width = hashmap_int_get(config, "render_width");
int render_height = hashmap_int_get(config, "render_height");
camera_attach_fbo(player_camera, render_width, render_height, true, true, true);
transform_parent_set(player_camera, player, true);
}
void player_destroy(struct Player* player)
{
entity_reset(player, player->base.id);
player->base.active = false;
}
void player_update(struct Player* player, struct Scene* scene, float dt)
{
float move_speed = player->move_speed;
vec3 offset = {0.f, 0.f, 0.f};
/* Movement */
if(input_map_state_get("Sprint", KS_PRESSED)) move_speed *= player->move_speed_multiplier;
if(input_map_state_get("Move_Forward", KS_PRESSED)) offset.z -= move_speed;
if(input_map_state_get("Move_Backward", KS_PRESSED)) offset.z += move_speed;
if(input_map_state_get("Move_Left", KS_PRESSED)) offset.x -= move_speed;
if(input_map_state_get("Move_Right", KS_PRESSED)) offset.x += move_speed;
if(input_map_state_get("Move_Up", KS_PRESSED)) offset.y += move_speed;
if(input_map_state_get("Move_Down", KS_PRESSED)) offset.y -= move_speed;
vec3_scale(&offset, &offset, dt);
if(offset.x != 0 || offset.y != 0 || offset.z != 0)
{
transform_translate(player, &offset, TS_LOCAL);
}
}

@ -0,0 +1,10 @@
#ifndef PLAYER_H
#define PLAYER_H
struct Player;
void player_init(struct Player* player, struct Scene* scene);
void player_destroy(struct Player* player);
void player_update(struct Player* player, struct Scene* scene, float dt);
#endif

@ -8,6 +8,7 @@
#include "../common/parser.h"
#include "model.h"
#include "light.h"
#include "player.h"
#include <assert.h>
#include <string.h>
@ -22,12 +23,6 @@ void scene_init(struct Scene* scene)
scene->root_entity.id = 0;
scene->root_entity.type = ET_ROOT;
//Initialize player
entity_init(&scene->player, "Player", &scene->root_entity);
scene->player.base.active = true;
scene->player.base.id = 1;
scene->player.base.type = ET_PLAYER;
for(int i = 0; i < MAX_ENTITIES; i++) entity_reset(&scene->entities[i], i);
for(int i = 0; i < MAX_LIGHTS; i++)
{
@ -52,6 +47,9 @@ void scene_init(struct Scene* scene)
scene->cameras[i].base.id = i;
}
player_init(&scene->player, scene);
editor_init_camera();
scene->active_camera_index = CAM_EDITOR;
}
@ -67,10 +65,16 @@ void scene_destroy(struct Scene* scene)
for(int i = 0; i < MAX_LIGHTS; i++) scene_light_remove(scene, &scene->lights[i]);
for(int i = 0; i < MAX_STATIC_MESHES; i++) scene_static_mesh_remove(scene, &scene->static_meshes[i]);
for(int i = 0; i < MAX_SOUND_SOURCES; i++) scene_sound_source_remove(scene, &scene->sound_sources[i]);
player_destroy(&scene->player);
entity_reset(&scene->root_entity, 0);
scene->root_entity.active = false;
}
void scene_update(struct Scene* scene, float dt)
{
player_update(&scene->player, scene, dt);
}
void scene_post_update(struct Scene* scene)
{
assert(scene);
@ -235,7 +239,7 @@ struct Light* scene_light_create(struct Scene* scene, const char* name, struct E
if(new_light)
{
entity_init(&new_light->base, name, parent);
entity_init(&new_light->base, name, parent ? parent : &scene->root_entity);
new_light->base.type = ET_LIGHT;
light_init(new_light, light_type);
}
@ -263,7 +267,7 @@ struct Camera* scene_camera_create(struct Scene* scene, const char* name, struct
if(new_camera)
{
entity_init(&new_camera->base, name, parent);
entity_init(&new_camera->base, name, parent ? parent : &scene->root_entity);
new_camera->base.type = ET_CAMERA;
camera_init(new_camera, width, height);
}
@ -291,7 +295,7 @@ struct Static_Model* scene_static_mesh_create(struct Scene* scene, const char* n
if(new_static_mesh)
{
entity_init(&new_static_mesh->base, name, parent);
entity_init(&new_static_mesh->base, name, parent ? parent : &scene->root_entity);
new_static_mesh->base.type = ET_STATIC_MESH;
model_init(&new_static_mesh->model, new_static_mesh, geometry_name, material_type);
// TODO: handle creating collision mesh for the model at creation
@ -320,7 +324,7 @@ struct Sound_Source* scene_sound_source_create(struct Scene* scene, const char*
if(new_sound_source)
{
entity_init(&new_sound_source->base, name, parent);
entity_init(&new_sound_source->base, name, parent ? parent : &scene->root_entity);
new_sound_source->base.type = ET_SOUND_SOURCE;
struct Entity* entity = &new_sound_source->base;
@ -366,12 +370,6 @@ struct Sound_Source* scene_sound_source_create(struct Scene* scene, const char*
return new_sound_source;
}
struct Player* scene_player_get(struct Scene* scene)
{
assert(scene);
return &scene->player;
}
void scene_entity_remove(struct Scene* scene, struct Entity* entity)
{
assert(scene && entity && entity->id >= 0);

@ -27,6 +27,7 @@ void scene_init(struct Scene* scene);
bool scene_load(struct Scene* scene, const char* filename, int dir_type);
bool scene_save(struct Scene* scene, const char* filename, int dir_type);
void scene_destroy(struct Scene* scene);
void scene_update(struct Scene* scene, float dt);
void scene_post_update(struct Scene* scene);
struct Entity* scene_entity_create(struct Scene* scene, const char* name, struct Entity* parent);
@ -34,7 +35,6 @@ struct Light* scene_light_create(struct Scene* scene, const char* name, s
struct Camera* scene_camera_create(struct Scene* scene, const char* name, struct Entity* parent, int width, int height);
struct Static_Model* scene_static_mesh_create(struct Scene* scene, const char* name, struct Entity* parent, const char* geometry_name, int material_type);
struct Sound_Source* scene_sound_source_create(struct Scene* scene, const char* name, struct Entity* parent, const char* filename, int type, bool loop, bool play);
struct Player* scene_player_get(struct Scene* scene);
void scene_entity_remove(struct Scene* scene, struct Entity* entity);
void scene_light_remove(struct Scene* scene, struct Light* light);

Loading…
Cancel
Save