Added light flash to player weapon and fixed a transformation related bug

dev
Shariq Shah 5 years ago
parent 902d7a3a57
commit 4eb832749e
  1. BIN
      assets/models/player_weapon.symbres
  2. 2
      assets/shaders/blinn_phong.frag
  3. 2
      src/common/version.h
  4. 16
      src/game/console.c
  5. 5
      src/game/editor.c
  6. 4
      src/game/entity.h
  7. 2
      src/game/im_render.c
  8. 75
      src/game/player.c
  9. 1
      src/game/player.h
  10. 1
      src/game/scene.c
  11. 17
      src/game/transform.c
  12. 5
      todo.txt

@ -46,7 +46,7 @@ vec3 calc_point_light(in Light light)
{
diffuse_comp = light.color * diffuse * cos_ang_incidence;
vec3 vertex_to_eye = normalize(camera_pos - vertex);
vec3 halfway = normalize(light.direction + vertex_to_eye);
vec3 halfway = normalize(light_direction + vertex_to_eye);
float specular_factor = max(0.0, dot(normalized_normal, halfway));
specular_factor = pow(specular_factor, specular_strength);
specular_comp = light.color * specular * specular_factor;

@ -4,7 +4,7 @@
/* Auto generated version file. DO NOT MODIFY */
#define SYMMETRY_VERSION_MAJOR 0
#define SYMMETRY_VERSION_MINOR 2
#define SYMMETRY_VERSION_REVISION 360
#define SYMMETRY_VERSION_REVISION 361
#define SYMMETRY_VERSION_BRANCH "dev"
#endif

@ -27,6 +27,7 @@ static void console_command_entity_save(struct Console* console, const char* com
static void console_command_entity_load(struct Console* console, const char* command);
static void console_command_debug_vars_toggle(struct Console* console, const char* command);
static void console_command_debug_vars_location_set(struct Console* console, const char* command);
static void console_command_switch_camera(struct Console* console, const char* command);
static void console_command_help(struct Console* console, const char* command);
void console_init(struct Console* console)
@ -39,10 +40,10 @@ void console_init(struct Console* console)
console_message_color[CMT_COMMAND] = nk_rgb(114, 173, 224);
console_message_color[CMT_NONE] = nk_rgb(255, 0, 255);
console->visible = 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;
}

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

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

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

@ -17,9 +17,11 @@
#include "event.h"
#include "sound_source.h"
#include "entity.h"
#include "gui_game.h"
#include <float.h>
#include <string.h>
#include <stdlib.h>
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;
}
}

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

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

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

@ -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
@ -431,3 +432,5 @@ Done:
* Pickup sounds
* Implemented flag for ignoring collisions with certain entities
* 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
Loading…
Cancel
Save