Skip to content

Commit

Permalink
Merge pull request #217 from ucsd-cse125-sp24/feat/torch-anim
Browse files Browse the repository at this point in the history
Feat/torch anim
  • Loading branch information
Tyler-Lentz authored Jun 7, 2024
2 parents 2ce00a7 + 2131fb4 commit 5230783
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 65 deletions.
16 changes: 8 additions & 8 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@
"disable_enemies": false
},
"network": {
"server_ip": "localhost",
"server_port": 2355
"server_ip": "localhost",
"server_port": 2355
},
"server": {
"lobby_name": "Hope you're doing well!",
"lobby_broadcast": true,
"max_players": 1,
"disable_dm": false,
"skip_intro": true
"lobby_name": "Hope you're doing well!",
"lobby_broadcast": true,
"max_players": 4,
"disable_dm": false,
"skip_intro": true
},
"client": {
"default_name": "Conan O'Brien",
"lobby_discovery": true,
"fullscreen": true,
"fullscreen": false,
"draw_bboxes": false,
"fps_counter": true,
"presentation": false,
Expand Down
6 changes: 4 additions & 2 deletions include/client/animationmanager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@
#include "client/bone.hpp"
#include "shared/game/sharedobject.hpp"

#define MAX_FRAME_TIME 0.033

class AnimationManager
{
public:
explicit AnimationManager();

void updateAnimation(float dt);

Model* updateFrameAnimation(float time);
Model* updateFrameAnimation(float dt);

void playAnimation(Animation* pAnimation);

Expand All @@ -36,7 +38,7 @@ class AnimationManager

private:
std::vector<glm::mat4> m_finalBoneMatrices;
std::unordered_map<EntityID, std::pair<int, Animation*>> entityAnimFrameMap;
std::unordered_map<EntityID, std::pair<float, Animation*>> entityAnimFrameMap;
std::unordered_map<EntityID, std::pair<float, Animation*>> entityAnimMap;
std::unordered_map<ModelType, std::unordered_map<AnimState, Animation*>> objAnimMap;
Animation* m_currentAnimation;
Expand Down
1 change: 1 addition & 0 deletions include/client/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ class Client {
float mouse_ypos = 0.0f;

double lastTime = 0.0;
double lastFrameTime = 0.0;

GameConfig config;
tcp::resolver resolver;
Expand Down
9 changes: 7 additions & 2 deletions include/server/game/projectile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,14 @@ class Projectile : public Object {
*/
bool doTick(ServerGameState& state);

virtual SharedObject toShared() override;


private:
Options opt;
std::optional<ServerSFX> destroy_sound;
PointLightProperties properties;

};

class HomingFireball : public Projectile {
Expand All @@ -90,7 +95,7 @@ class HomingFireball : public Projectile {
inline static const int HOMING_DURATION_TICKS = 80; // 2.4s

HomingFireball(glm::vec3 corner, glm::vec3 facing, std::optional<EntityID> target):
Projectile(corner, facing, glm::vec3(0.4f, 0.4f, 0.4f), ModelType::Cube, ServerSFX::FireballImpact,
Projectile(corner, facing, glm::vec3(0.4f, 0.4f, 0.4f), ModelType::Fireball, ServerSFX::FireballImpact,
Options(false, DAMAGE, H_MULT, V_MULT, true, HOMING_STRENGTH, HOMING_DURATION_TICKS, target))
{
this->physics.feels_gravity = false;
Expand Down Expand Up @@ -149,7 +154,7 @@ class SpellOrb : public Projectile {
SpellType sType;

SpellOrb(glm::vec3 corner, glm::vec3 facing, SpellType type) :
Projectile(corner, facing, glm::vec3(0.4f, 0.4f, 0.4f), ModelType::Cube, ServerSFX::FireballImpact,
Projectile(corner, facing, glm::vec3(0.4f, 0.4f, 0.4f), ModelType::SpellOrb, ServerSFX::FireballImpact,
Options(true, DAMAGE, H_MULT, V_MULT, false, 0.0f, 0, {}))
{
this->sType = type;
Expand Down
2 changes: 2 additions & 0 deletions include/shared/game/sharedmodel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ enum class ModelType {
FireSpell,
HealSpell,
TeleportSpell,
SpellOrb,
Frame,
Orb,
FloorSpikeHorizontal,
Expand All @@ -37,6 +38,7 @@ enum class ModelType {
Lightning,
Arrow,
ArrowTrap,
Fireball,
FireballTrapLeft,
FireballTrapRight,
FireballTrapUp,
Expand Down
10 changes: 5 additions & 5 deletions maps/test/anim_test.maze
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
###[###
#######
#.....#
{..3..}
#..3..#
#.....#
{.....}
#.....#
#.@...#
#.....#
{.@...}
#.....#
###]###
#######
18 changes: 6 additions & 12 deletions src/client/animationmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,13 @@ void AnimationManager::updateAnimation(float dt) {
}
}

Model* AnimationManager::updateFrameAnimation(float time) {
Model* AnimationManager::updateFrameAnimation(float dt) {
if (entityAnimFrameMap[currEntity].second) {
m_currentAnimation = entityAnimFrameMap[currEntity].second;
int currFrame = entityAnimFrameMap[currEntity].first + 1;

// /* Change this to a constant */
// if (time - lastFrameTime >= 0.01667) {
// std::cout << "time: " << time << ", lastTime: " << lastFrameTime << ", diff: " << (time - lastFrameTime) << ", currFrame: " << currFrame << std::endl;
// currFrame += 1;
// std::cout << currFrame << std::endl;
// lastFrameTime = time;
// }
entityAnimFrameMap[currEntity].first = currFrame;
m_currentTime = entityAnimFrameMap[currEntity].first;
m_currentTime += dt;
int currFrame = static_cast<int> (m_currentTime / MAX_FRAME_TIME);
entityAnimFrameMap[currEntity].first = m_currentTime;
return m_currentAnimation->getFrame(currFrame);
} else {
return nullptr;
Expand Down Expand Up @@ -97,7 +91,7 @@ void AnimationManager::setFrameAnimation(EntityID id, ModelType modelType, AnimS
static std::random_device dev;
static std::mt19937 rng(dev());
static std::uniform_int_distribution<std::mt19937::result_type> dist(0,51);
entityAnimFrameMap[id] = std::make_pair(dist(rng), objAnimMap[modelType][animState]);
entityAnimFrameMap[id] = std::make_pair(dist(rng) * MAX_FRAME_TIME, objAnimMap[modelType][animState]);
}
currEntity = id;
}
Expand Down
115 changes: 82 additions & 33 deletions src/client/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ bool Client::init() {
auto player_atk_path = graphics_assets_dir / "animations/slash.fbx";
auto player_use_potion_path = graphics_assets_dir / "animations/drink.fbx";
auto torchlight_anim_path = graphics_assets_dir / "animations/fire-fix";

auto fireball_anim_path = graphics_assets_dir / "animations/fireball";

auto fire_player_model_path = player_models_dir / "char_1_rename/char1.fbx";
auto lightning_player_model_path = player_models_dir / "char_2_rename/char2.fbx";
Expand All @@ -294,7 +294,9 @@ bool Client::init() {
animManager = new AnimationManager();
Animation* torchlight_animation = new Animation(torchlight_anim_path.string(), "fire-fix", 45);
animManager->addAnimation(torchlight_animation, ModelType::Torchlight, AnimState::IdleAnim);

Animation* fireball_animation = new Animation(fireball_anim_path.string(), "fireball", 60);
animManager->addAnimation(fireball_animation, ModelType::Fireball, AnimState::IdleAnim);
animManager->addAnimation(fireball_animation, ModelType::SpellOrb, AnimState::IdleAnim);
for (int i = 0; i < NUM_PLAYER_MODELS; i++) {
Animation* player_walk = new Animation(player_walk_path.string(), player_models.at(i).second);
Animation* player_jump = new Animation(player_jump_path.string(), player_models.at(i).second);
Expand Down Expand Up @@ -694,6 +696,9 @@ void Client::processServerInput(bool allow_defer) {
}
EntityID light_id = updated_light_source.lightSources[i].value().eid;
this->closest_light_sources[i] = this->gameState.objects[light_id];
if (!this->closest_light_sources[i].has_value()) {
continue;
}
// update intensity with incoming intensity
this->closest_light_sources[i]->pointLightInfo->intensity = updated_light_source.lightSources[i]->intensity;
this->closest_light_sources[i]->pointLightInfo->is_cut = updated_light_source.lightSources[i]->is_cut;
Expand Down Expand Up @@ -1112,12 +1117,15 @@ void Client::geometryPass() {
this->arrow_model->draw(this->deferred_geometry_shader.get(),
this->cam->getPos(), true);
break;
}
}
// case ModelType::Fireball: {
// break;
// }
default:
this->spike_trap_model->setDimensions(sharedObject->physics.dimensions);
this->spike_trap_model->translateAbsolute(sharedObject->physics.getCenterPosition());
this->spike_trap_model->draw(this->deferred_geometry_shader.get(),
this->cam->getPos(), true);
// this->spike_trap_model->setDimensions(sharedObject->physics.dimensions);
// this->spike_trap_model->translateAbsolute(sharedObject->physics.getCenterPosition());
// this->spike_trap_model->draw(this->deferred_geometry_shader.get(),
// this->cam->getPos(), true);
break;
}
break;
Expand Down Expand Up @@ -1340,6 +1348,14 @@ void Client::lightingPass() {
glm::vec3 FAR_AWAY(10000, 10000, 10000);
lighting_shader->setVec3("pointLights[" + std::to_string(i) + "].position", FAR_AWAY);
continue;
} else if (!this->gameState.objects.contains(curr_source->globalID)) {
glm::vec3 FAR_AWAY(10000, 10000, 10000);
lighting_shader->setVec3("pointLights[" + std::to_string(i) + "].position", FAR_AWAY);
continue;
} else if (!this->gameState.objects.at(curr_source->globalID).has_value()) {
glm::vec3 FAR_AWAY(10000, 10000, 10000);
lighting_shader->setVec3("pointLights[" + std::to_string(i) + "].position", FAR_AWAY);
continue;
}

SharedPointLightInfo& properties = curr_source->pointLightInfo.value();
Expand Down Expand Up @@ -1397,6 +1413,10 @@ void Client::lightingPass() {
glBlitFramebuffer(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, GL_DEPTH_BUFFER_BIT, GL_NEAREST);
glBindFramebuffer(GL_FRAMEBUFFER, 0);

double currentTime = glfwGetTime();
double timeElapsed = currentTime - lastFrameTime;
lastFrameTime = currentTime;

// render torch lights on top of scene
this->deferred_light_box_shader->use();
glm::mat4 viewProj = this->cam->getViewProj();
Expand All @@ -1406,10 +1426,6 @@ void Client::lightingPass() {
continue;
}

if (sharedObject->type != ObjectType::Torchlight) {
continue;
}

auto dist = glm::distance(sharedObject->physics.corner, my_pos);
if (!is_dm && dist > this->config.client.render) {
continue;
Expand All @@ -1418,37 +1434,70 @@ void Client::lightingPass() {
if (!sharedObject->pointLightInfo.has_value()) {
std::cout << "got a torch without point light info for some reason" << std::endl;
continue;
}
}

SharedPointLightInfo& properties = sharedObject->pointLightInfo.value();

this->deferred_light_box_shader->use();

glm::vec3 v(1.0f);
switch (sharedObject->type) {
case ObjectType::Torchlight: {

glm::vec3 v(1.0f);

animManager->setFrameAnimation(sharedObject->globalID, sharedObject->modelType, sharedObject->animState);
animManager->setFrameAnimation(sharedObject->globalID, sharedObject->modelType, sharedObject->animState);
Model* torch_frame_model = animManager->updateFrameAnimation(timeElapsed);
glm::vec3 dims = torch_frame_model->getDimensions();

/* Update model animation */
Model* torch_frame_model = animManager->updateFrameAnimation(glfwGetTime());
glm::vec3 dims = torch_frame_model->getDimensions();
this->deferred_light_box_shader->use();
glm::vec3 flame_color;

glm::vec3 flame_color;
if (sharedObject->pointLightInfo->is_cut) {
glm::vec3 black(0.1f, 0.1f, 0.1f);
flame_color = black;
} else {
flame_color = properties.diffuse_color;
}

if (sharedObject->pointLightInfo->is_cut) {
glm::vec3 black(0.1f, 0.1f, 0.1f);
flame_color = black;
} else {
flame_color = properties.diffuse_color;
}
this->deferred_light_box_shader->setVec3("lightColor", flame_color);
torch_frame_model->setDimensions(2.0f * sharedObject->physics.dimensions);
torch_frame_model->translateAbsolute(sharedObject->physics.getCenterPosition());
torch_frame_model->draw(this->deferred_light_box_shader.get(), this->cam->getPos(), true, flame_color);
break;
}
case ObjectType::Projectile: {
if (sharedObject->modelType == ModelType::Fireball) {
glm::vec3 flame_color = properties.diffuse_color;

this->deferred_light_box_shader->setVec3("lightColor", flame_color);
animManager->setFrameAnimation(sharedObject->globalID, sharedObject->modelType, sharedObject->animState);
Model* fireball_frame_model = animManager->updateFrameAnimation(timeElapsed);

// this->torchlight_model->setDimensions(2.0f * sharedObject->physics.dimensions);
// this->torchlight_model->translateAbsolute(sharedObject->physics.getCenterPosition());
// this->torchlight_model->draw(this->deferred_light_box_shader.get(), this->cam->getPos(), true);
torch_frame_model->setDimensions(2.0f * sharedObject->physics.dimensions);
torch_frame_model->translateAbsolute(sharedObject->physics.getCenterPosition());
// torch_frame_model->draw(this->deferred_light_box_shader.get(), this->cam->getPos(), true);
torch_frame_model->draw(this->deferred_light_box_shader.get(), this->cam->getPos(), true, flame_color);
this->deferred_light_box_shader->setVec3("lightColor", flame_color);

fireball_frame_model->setDimensions(sharedObject->physics.dimensions);
fireball_frame_model->translateAbsolute(sharedObject->physics.getCenterPosition());
fireball_frame_model->draw(this->deferred_light_box_shader.get(),
this->cam->getPos(), true);
break;
} else if (sharedObject->modelType == ModelType::SpellOrb) {
glm::vec3 flame_color = properties.diffuse_color;

animManager->setFrameAnimation(sharedObject->globalID, sharedObject->modelType, sharedObject->animState);
Model* fireball_frame_model = animManager->updateFrameAnimation(timeElapsed);

this->deferred_light_box_shader->setVec3("lightColor", flame_color);

fireball_frame_model->setDimensions(sharedObject->physics.dimensions);
fireball_frame_model->translateAbsolute(sharedObject->physics.getCenterPosition());
fireball_frame_model->draw(this->deferred_light_box_shader.get(),
this->cam->getPos(), true);
break;
}
break;
}
default:
continue;
}

}

}
Expand Down
2 changes: 2 additions & 0 deletions src/server/game/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ std::unordered_map<ModelType, glm::vec3> Object::models ({
{ModelType::Lightning, glm::vec3(3.0f, 100.0f, 3.0f)},
{ModelType::Orb, glm::vec3(0.887116, 0.941508, 0.950092)},
{ModelType::Chest, glm::vec3(1.377020, 1.355794, 1.092905)},
{ModelType::Fireball, glm::vec3(0.4f, 0.4f, 0.4f)},
{ModelType::SpellOrb, glm::vec3(0.4f, 0.4f, 0.4f)},
{ModelType::TeleporterTrap, glm::vec3(1.0f, 3.0f, 1.0f)}
});

Expand Down
Loading

0 comments on commit 5230783

Please sign in to comment.