From 4eb832749ea14f76dc0819e9607d36a968d84bce Mon Sep 17 00:00:00 2001 From: Shariq Shah Date: Fri, 10 Apr 2020 17:06:33 +1000 Subject: [PATCH] Added light flash to player weapon and fixed a transformation related bug --- assets/models/player_weapon.symbres | Bin 0 -> 2608 bytes assets/shaders/blinn_phong.frag | 2 +- src/common/version.h | 2 +- src/game/console.c | 16 ++++-- src/game/editor.c | 5 +- src/game/entity.h | 4 ++ src/game/im_render.c | 2 +- src/game/player.c | 75 ++++++++++++++++++---------- src/game/player.h | 1 + src/game/scene.c | 1 + src/game/transform.c | 17 ++----- todo.txt | 7 ++- 12 files changed, 83 insertions(+), 49 deletions(-) create mode 100644 assets/models/player_weapon.symbres diff --git a/assets/models/player_weapon.symbres b/assets/models/player_weapon.symbres new file mode 100644 index 0000000000000000000000000000000000000000..c378c2635cb92c339cd661a65921f6473c469d4d GIT binary patch literal 2608 zcmZ{lJ8u&~6onTs??8YJ5MGIaknjqSC(divJOXJbkf^9aL--Ad!Ud8$gdzpWjuawj zsZc=BcrCfkj{u1p3HSk=Gdr_;JaLLobNBAKXYSltX%~VZSP&2fJHSq`3lzX^um|h~ z`@nv102~Be;1DYo$I0MdtbD$6OgCaN&E`W<*09*ot zUd80whJmUuA!R3vF@w+@9sm6L{dRNPQhg#$5?aO`| z(7S%9$@-Mo`;Twy(c^*D#_Q)xiLaTmTGcald9C#{*}i`Z6Uj!a zI{F9eKk&6x{fsq+KGSIa&2T+hS*L`>Ma5QDgSv`cZ$q3!SIli~4A-YBW|Kt<}8li@vfu(pYDrKK2~u zJJEf$H?37q-B)Av)LOkGy$kAXP1~KSMr*Y zTwSVj`m~Pvisible = false; - console->scroll_to_bottom = true; - console->text_region_height = 30.f; - console->line_height = 20.f; + console->visible = false; + console->scroll_to_bottom = true; + console->text_region_height = 30.f; + console->line_height = 20.f; console->current_message_index = -1; console->current_history_index = 0; console->current_history_browse_index = 0; @@ -66,6 +67,7 @@ void console_init(struct Console* console) hashmap_ptr_set(console->commands, "entity_load", &console_command_entity_load); hashmap_ptr_set(console->commands, "debug_vars_toggle", &console_command_debug_vars_toggle); hashmap_ptr_set(console->commands, "debug_vars_location", &console_command_debug_vars_location_set); + hashmap_ptr_set(console->commands, "switch_camera", &console_command_switch_camera); hashmap_ptr_set(console->commands, "help", &console_command_help); struct Event_Manager* event_manager = game_state_get()->event_manager; @@ -360,3 +362,9 @@ void console_command_scene_reload(struct Console* console, const char* command) if(!scene_load(scene, filename, DIRT_INSTALL)) log_error("scene_load", "Command failed"); } + +void console_command_switch_camera(struct Console* console, const char* command) +{ + struct Scene* scene = game_state_get()->scene; + scene->active_camera_index = scene->active_camera_index == CAM_GAME ? CAM_EDITOR : CAM_GAME; +} diff --git a/src/game/editor.c b/src/game/editor.c index b2f359d..3c90ec3 100755 --- a/src/game/editor.c +++ b/src/game/editor.c @@ -233,8 +233,7 @@ void editor_render(struct Editor* editor, struct Camera * active_camera) if(light->type != LT_DIR) { quat rotation = editor->selected_entity->transform.rotation; - vec3 axis = { 1.f, 0.f, 0.f }; - quat_axis_angle(&rotation, &axis, 90.f); + quat_axis_angle(&rotation, &UNIT_X, -90.f); im_circle(light->radius, 30, false, abs_pos, rotation, editor->cursor_entity_color, 3); if(light->type == LT_SPOT) @@ -244,6 +243,8 @@ void editor_render(struct Editor* editor, struct Camera * active_camera) float half_inner_angle = light->inner_angle / 2.f; im_arc(light->radius, yaw - half_outer_angle, yaw + half_outer_angle, 15, false, abs_pos, rotation, editor->selected_entity_color, 3); im_arc(light->radius, yaw - half_inner_angle, yaw + half_inner_angle, 15, false, abs_pos, rotation, editor->cursor_entity_color, 4); + //im_arc(light->radius, -half_outer_angle, half_outer_angle, 15, false, abs_pos, rotation, editor->selected_entity_color, 3); + //im_arc(light->radius, -half_inner_angle, half_inner_angle, 15, false, abs_pos, rotation, editor->cursor_entity_color, 4); } } } diff --git a/src/game/entity.h b/src/game/entity.h index dfa36db..e07ea98 100755 --- a/src/game/entity.h +++ b/src/game/entity.h @@ -212,6 +212,10 @@ struct Player struct Sound_Source* weapon_sound; struct Sound_Source* footstep_sound; struct Sound_Source* grunt_sound; + struct Light* weapon_light; + int weapon_light_intensity_min; + int weapon_light_intensity_max; + float weapon_light_intensity_decay; int health; int key_mask; float move_speed; diff --git a/src/game/im_render.c b/src/game/im_render.c index 4bace2b..8fb5c98 100755 --- a/src/game/im_render.c +++ b/src/game/im_render.c @@ -124,7 +124,7 @@ void im_box(float x, float y, float z, vec3 position, quat rotation, vec4 color, active_geom->draw_order = draw_order; active_geom->prim_geom_index = geom_create_from_file("cube.symbres"); vec3_assign(&active_geom->position, &position); - vec3 scale = { x, y, z}; + vec3 scale = { x, y, z }; vec3_assign(&active_geom->scale, &scale); vec4_assign(&active_geom->color, &color); quat_assign(&active_geom->rotation, &rotation); diff --git a/src/game/player.c b/src/game/player.c index 68e1066..e18d0b8 100755 --- a/src/game/player.c +++ b/src/game/player.c @@ -17,9 +17,11 @@ #include "event.h" #include "sound_source.h" #include "entity.h" +#include "gui_game.h" #include #include +#include static void player_on_mousebutton_released(const struct Event* event); @@ -36,27 +38,23 @@ void player_init(struct Player* player, struct Scene* scene) player->base.bounding_box.max = (vec3){ 1.5f, 1.5f, 1.0f }; struct Hashmap* config = game_state->cvars; - player->move_speed = hashmap_float_get(config, "player_move_speed"); - player->move_speed_multiplier = hashmap_float_get(config, "player_move_speed_multiplier"); - player->turn_speed = hashmap_float_get(config, "player_turn_speed"); - player->jump_speed = hashmap_float_get(config, "player_jump_speed"); - player->gravity = hashmap_float_get(config, "player_gravity"); - player->min_downward_distance = hashmap_float_get(config, "player_min_downward_distance"); - player->min_forward_distance = hashmap_float_get(config, "player_min_forward_distance"); - player->grounded = true; - player->can_jump = true; - player->health = MAX_PLAYER_HEALTH; - player->key_mask = 0; + player->move_speed = hashmap_float_get(config, "player_move_speed"); + player->move_speed_multiplier = hashmap_float_get(config, "player_move_speed_multiplier"); + player->turn_speed = hashmap_float_get(config, "player_turn_speed"); + player->jump_speed = hashmap_float_get(config, "player_jump_speed"); + player->gravity = hashmap_float_get(config, "player_gravity"); + player->min_downward_distance = hashmap_float_get(config, "player_min_downward_distance"); + player->min_forward_distance = hashmap_float_get(config, "player_min_forward_distance"); + player->grounded = true; + player->can_jump = true; + player->health = MAX_PLAYER_HEALTH; + player->key_mask = 0; + player->weapon_light_intensity_min = 1.f; + player->weapon_light_intensity_max = 5.f; + player->weapon_light_intensity_decay = 30.f; player->body_mesh = scene_static_mesh_create(scene, "Player_Body_Mesh", player, "sphere.symbres", MAT_BLINN); - player->weapon_mesh = scene_static_mesh_create(scene, "PLayer_Weapon_Mesh", player, "player_weapon.symbres", MAT_BLINN); - player->body_mesh->base.flags |= EF_IGNORE_COLLISION | EF_ALWAYS_RENDER; - player->weapon_mesh->base.flags |= EF_IGNORE_COLLISION | EF_ALWAYS_RENDER; - vec3 translation = { 0.f, 1.f, -1.5f }; - transform_translate(player->weapon_mesh, &translation, TS_LOCAL); - transform_rotate(player->weapon_mesh, &UNIT_Y, 90.f, TS_LOCAL); - transform_scale(player->weapon_mesh, &(vec3){0.2f, 0.1f, 0.1f}); struct Camera* player_camera = &scene->cameras[CAM_GAME]; entity_rename(player_camera, "Player_Camera"); @@ -67,6 +65,20 @@ void player_init(struct Player* player, struct Scene* scene) player_camera->clear_color.w = 1.f; player->camera = player_camera; + vec3 translation = { 0.f, -0.3f, -1.75f }; + player->weapon_light = scene_light_create(scene, "Player_Weapon_Light", player_camera, LT_SPOT); + transform_translate(player->weapon_light, &translation, TS_LOCAL); + player->weapon_light->intensity = 0.f; + player->weapon_light->radius = 25.f; + player->weapon_light->outer_angle = 80.f; + player->weapon_light->inner_angle = 50.f; + vec3_fill(&player->weapon_light->color, 0.9f, 0.4f, 0.f); + + player->weapon_mesh = scene_static_mesh_create(scene, "Player_Weapon_Mesh", player_camera, "player_weapon.symbres", MAT_BLINN); + transform_translate(player->weapon_mesh, &translation, TS_LOCAL); + transform_rotate(player->weapon_mesh, &UNIT_Y, 90.f, TS_LOCAL); + transform_scale(player->weapon_mesh, &(vec3){0.3f, 0.1f, 0.1f}); + struct Sound_Source* weapon_sound = scene_sound_source_create(scene, "Player_Weapon_Sound_Source", player, "sounds/bullet_1.wav", ST_WAV, false, false); if(weapon_sound) player->weapon_sound = weapon_sound; @@ -87,10 +99,12 @@ void player_init(struct Player* player, struct Scene* scene) // Mark player camera and mesh as transient for now. We don't need to save them to file since we recreate them here anyway player->camera->base.flags |= EF_TRANSIENT; - player->body_mesh->base.flags |= EF_TRANSIENT; - player->weapon_sound->base.flags |= EF_TRANSIENT; player->footstep_sound->base.flags |= EF_TRANSIENT; player->grunt_sound->base.flags |= EF_TRANSIENT; + player->body_mesh->base.flags |= EF_TRANSIENT | EF_IGNORE_COLLISION | EF_ALWAYS_RENDER; + player->weapon_mesh->base.flags |= EF_TRANSIENT | EF_IGNORE_COLLISION | EF_ALWAYS_RENDER; + player->weapon_light->base.flags |= EF_TRANSIENT; + player->weapon_sound->base.flags |= EF_TRANSIENT; transform_parent_set(player_camera, player, true); @@ -341,13 +355,12 @@ void player_on_mousebutton_released(const struct Event* event) struct Scene* scene = game_state->scene; struct Player* player = &scene->player; - if(game_state->game_mode != GAME_MODE_GAME) + if(game_state->game_mode != GAME_MODE_GAME || game_state->gui_game->show_next_level_dialog || game_state->gui_game->show_restart_level_dialog) return; /* Aiming and Projectiles*/ if(button == MSB_LEFT) { - log_message("Right Click"); int half_width = 0, half_height = 0; window_get_drawable_size(game_state->window, &half_width, &half_height); half_width /= 2; @@ -366,11 +379,13 @@ void player_on_mousebutton_released(const struct Event* event) vec3_scale(&collision_point, &collision_point, distance); vec3_add(&collision_point, &collision_point, &bullet_ray.origin); //struct Static_Mesh* bullet = scene_static_mesh_create(game_state_get()->scene, "bullet", NULL, "cube.symbres", MAT_UNSHADED); - struct Light* bullet = entity_load("Spot", DIRT_INSTALL, true); - if(bullet) transform_set_position(bullet, &collision_point); - sound_source_play(game_state->sound, player->weapon_sound); + //struct Light* bullet = entity_load("Spot", DIRT_INSTALL, true); + //if(bullet) transform_set_position(bullet, &collision_point); } + int intensity = player->weapon_light_intensity_min + rand() % player->weapon_light_intensity_max; + player->weapon_light->intensity = intensity; + sound_source_play(game_state->sound, player->weapon_sound); } } @@ -403,3 +418,13 @@ void player_on_pickup(struct Player* player, struct Pickup* pickup) break; } } + +void player_update(struct Player* player, float dt) +{ + if(player->weapon_light->intensity > 0.f) + { + player->weapon_light->intensity -= player->weapon_light_intensity_decay * dt; + if(player->weapon_light->intensity < 0.f) + player->weapon_light->intensity = 0.f; + } +} diff --git a/src/game/player.h b/src/game/player.h index b047b24..6e686cf 100755 --- a/src/game/player.h +++ b/src/game/player.h @@ -10,5 +10,6 @@ void player_destroy(struct Player* player); void player_apply_damage(struct Player* player, struct Enemy* enemy); void player_on_pickup(struct Player* player, struct Pickup* pickup); void player_update_physics(struct Player* player, struct Scene* scene, float dt); +void player_update(struct Player* player, float dt); #endif diff --git a/src/game/scene.c b/src/game/scene.c index d0a556d..7894b7e 100755 --- a/src/game/scene.c +++ b/src/game/scene.c @@ -533,6 +533,7 @@ void scene_update(struct Scene* scene, float dt) { if(game_state_get()->game_mode == GAME_MODE_GAME) { + player_update(&scene->player, dt); for(int i = 0; i < MAX_SCENE_ENEMIES; i++) { if(scene->enemies[i].base.flags & EF_ACTIVE) diff --git a/src/game/transform.c b/src/game/transform.c index a8089da..a85d657 100755 --- a/src/game/transform.c +++ b/src/game/transform.c @@ -136,6 +136,7 @@ void transform_rotate(struct Entity* entity, quat_mul(&transform->rotation, &transform->rotation, &new_rot); else quat_mul(&transform->rotation, &new_rot, &transform->rotation); + quat_norm(&transform->rotation, &transform->rotation); transform_update_transmat(entity); } @@ -278,19 +279,9 @@ void transform_set_position(struct Entity* entity, vec3* new_position) void transform_get_absolute_position(struct Entity* entity, vec3* res) { - vec3_assign(res, &entity->transform.position); - bool done = false; - struct Entity* parent = entity->transform.parent; - while(!done) - { - if(!parent) - { - done = true; - break; - } - vec3_add(res, res, &parent->transform.position); - parent = parent->transform.parent; - } + res->x = entity->transform.trans_mat.mat[12]; + res->y = entity->transform.trans_mat.mat[13]; + res->z = entity->transform.trans_mat.mat[14]; } void transform_get_absolute_scale(struct Entity* entity, vec3* res) diff --git a/todo.txt b/todo.txt index ec6ed43..df099f2 100644 --- a/todo.txt +++ b/todo.txt @@ -1,8 +1,9 @@ Todo: - Game End - Don't save parent entity's transform when saving entity archetype. Only save the transformation values for children - - Player weapon mesh and lighting - Enemies getting hit by bullets + - Add weapon flash, muzzle mesh to turrets + - Add muzzle mesh to player - Save case sensitive file names when scene entity entries - Disbale all player actions when scene cleared dialog or scene restart dialog are active - Memory utils that provide allocation tracking @@ -430,4 +431,6 @@ Done: * Pickups * Pickup sounds * Implemented flag for ignoring collisions with certain entities - * Background music track per scene specified in scene properties \ No newline at end of file + * Background music track per scene specified in scene properties + * Fixed bug where child entities' absolute position does not change when parent rotates + * Player weapon mesh and lighting \ No newline at end of file