Skip to content

Commit

Permalink
Merge pull request #200 from ucsd-cse125-sp24/feat/gradient-torches
Browse files Browse the repository at this point in the history
Feat/gradient torches
  • Loading branch information
Tyler-Lentz authored Jun 5, 2024
2 parents 145f0d4 + 0b703c2 commit 9840f14
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 28 deletions.
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"lobby_name": "Hope you're doing well!1",
"lobby_broadcast": true,
"max_players": 1,
"disable_dm": true,
"disable_dm": false,
"skip_intro": true
},
"client": {
Expand Down
6 changes: 5 additions & 1 deletion include/server/game/servergamestate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,15 @@ class ServerGameState {
* cell
* @param cell is a single cell of the maze's grid where a wall + torch
* should be placed
* @param orb_pos position of the orb so that we can calculate the distance
* for coloring purposes
* @param exit_pos position of the exit so that we can calculate the distance
* for coloring purposes
* @param cellType determines which direction the torch should face.
* This means that only TorchUp, TorchDown, TorchRight, and TorchLeft
* are acceptable values of cellType
*/
void spawnTorch(GridCell *cell);
void spawnTorch(GridCell *cell, glm::vec3 orb_pos, glm::vec3 exit_pos);

/**
* @brief helper function to spawn a fireball trap
Expand Down
4 changes: 3 additions & 1 deletion include/server/game/torchlight.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ class Torchlight : public Object {
/**
* Creates a torchight with default lighting properties.
* @param corner Corner position of the surface
* @param dist_orb distance to orb, to see if it should be shaded blue
* @param dist_exit distance to the exit, to see if it should be shaded white
*/
explicit Torchlight(glm::vec3 corner);
explicit Torchlight(glm::vec3 corner, float dist_orb, float dist_exit);

/**
* @param corner Corner position of the surface
Expand Down
35 changes: 32 additions & 3 deletions src/server/game/servergamestate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1865,6 +1865,29 @@ void ServerGameState::loadMaze(const Grid& grid) {
}
}

std::optional<glm::vec3> orb_pos;
std::optional<glm::vec3> exit_pos;
// go through and mark distance to orb and exit
for (int c = 0; c < this->grid.getColumns(); c++) {
for (int r = 0; r < this->grid.getRows(); r++) {
CellType type = this->grid.getCell(c, r)->type;
if (type == CellType::Orb || type == CellType::Exit) {
glm::vec3 corner(c * Grid::grid_cell_width, 0.0f, r * Grid::grid_cell_width);

if (type == CellType::Orb) {
orb_pos = corner;
} else {
exit_pos = corner;
}

}
}

if (orb_pos.has_value() && exit_pos.has_value()) {
break; // early exit, not really needed though probably
}
}

// create multiple floor and ceiling objects to populate the size of the entire maze.
// currently doing this to stretch out the floor texture by the desired factor. here
// in the maze generation we can have a lot of control over how frequently the floor texture
Expand Down Expand Up @@ -2097,7 +2120,10 @@ void ServerGameState::loadMaze(const Grid& grid) {
case CellType::TorchDown:
case CellType::TorchRight:
case CellType::TorchLeft: {
this->spawnTorch(cell);
// if no orb or exit in maze then just tell it that they are very far off so not
// considered in color calculations
const glm::vec3 FAR_OFF_POS(10000, 10000, 10000);
this->spawnTorch(cell, orb_pos.value_or(FAR_OFF_POS), exit_pos.value_or(FAR_OFF_POS));
this->spawnWall(cell, col, row, internal_walls.contains(glm::ivec2(col, row)));
break;
}
Expand Down Expand Up @@ -2227,14 +2253,17 @@ void ServerGameState::spawnWall(GridCell* cell, int col, int row, bool is_intern
}
}

void ServerGameState::spawnTorch(GridCell *cell) {
void ServerGameState::spawnTorch(GridCell *cell, glm::vec3 orb_pos, glm::vec3 exit_pos) {
glm::vec3 dimensions = Object::models.at(ModelType::Torchlight);
glm::vec3 corner(
cell->x * this->grid.grid_cell_width,
MAZE_CEILING_HEIGHT / 2.0f,
cell->y * this->grid.grid_cell_width
);

float orb_dist = glm::distance(corner, orb_pos);
float exit_dist = glm::distance(corner, exit_pos);

switch (cell->type) {
case CellType::TorchDown: {
corner.x += (this->grid.grid_cell_width / 2.0f) - (dimensions.x / 2.0f);
Expand Down Expand Up @@ -2271,7 +2300,7 @@ void ServerGameState::spawnTorch(GridCell *cell) {
true
));

this->objects.createObject(new Torchlight(corner));
this->objects.createObject(new Torchlight(corner, orb_dist, exit_dist));
}

Trap* ServerGameState::spawnFireballTrap(GridCell *cell) {
Expand Down
94 changes: 72 additions & 22 deletions src/server/game/torchlight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "server/game/object.hpp"
#include "shared/game/sharedobject.hpp"
#include "shared/utilities/rng.hpp"
#include "server/game/grid.hpp"

SharedObject Torchlight::toShared() {
auto so = Object::toShared();
Expand All @@ -20,34 +21,83 @@ SharedObject Torchlight::toShared() {
}

Torchlight::Torchlight(
glm::vec3 corner):
glm::vec3 corner, float dist_orb, float dist_exit):
Object(ObjectType::Torchlight, Physics(false,
Collider::Box, corner, glm::vec3(0.0f), glm::vec3(1.0f)),
ModelType::Torchlight)
{
// for amber orange lights
this->properties = TorchlightProperties {
.flickering = true,
.min_intensity = 0.3f,
.max_intensity = 1.0f,
.ambient_color = glm::vec3(0.05f, 0.05f, 0.05f),
.diffuse_color = glm::vec3(1.0f, 0.5f, 0.03f),
.specular_color = glm::vec3(0.5f, 0.25f, 0.015f),
.attenuation_linear = 0.07f,
.attenuation_quadratic = 0.017f
const float MIN_ORB_DIST = Grid::grid_cell_width * 60.0f;
const float MIN_EXIT_DIST = Grid::grid_cell_width * 60.0f; // min distance to start shade white

const float AMBER_MIN_INTENSITY = 0.3f;
const float AMBER_MAX_INTENSITY = 1.0f;
const glm::vec3 AMBER_AMBIENT(0.05f, 0.05f, 0.05f);
const glm::vec3 AMBER_DIFFUSE(1.0f, 0.5f, 0.03f);
const glm::vec3 AMBER_SPECULAR(0.5f, 0.25f, 0.015f);

const float BLUE_MIN_INTENSITY = 0.1f;
const float BLUE_MAX_INTENSITY = 0.5f;
const glm::vec3 BLUE_AMBIENT(0.0f, 0.75f, 0.67f);
const glm::vec3 BLUE_DIFFUSE(0.0f, 0.75f, 0.67f);
const glm::vec3 BLUE_SPECULAR(0.0f, 0.35, 0.33f);

const float WHITE_MIN_INTENSITY = 0.1f;
const float WHITE_MAX_INTENSITY = 0.3f;
const glm::vec3 WHITE_AMBIENT(1.05f, 1.05f, 1.05f);
const glm::vec3 WHITE_DIFFUSE(1.0f, 1.0f, 1.0f);
const glm::vec3 WHITE_SPECULAR(0.5f, 0.5f, 0.5f);

const float ATTEN_LINEAR = 0.07f;
const float ATTEN_QUAD = 0.017f;

// higher intensity takes you closer to color2
const auto interpolateColor = [](float intensity, glm::vec3 color1, glm::vec3 color2) {
return color1 + intensity * (color2 - color1);
};

if (dist_orb < MIN_ORB_DIST) {
// the closer you get, the higher the factor to make it more white is multiplied
const float blue_intensity = (MIN_ORB_DIST - dist_orb) / MIN_ORB_DIST;

// close to orb, so shade blue
this->properties = TorchlightProperties {
.flickering = true,
.min_intensity = BLUE_MIN_INTENSITY,
.max_intensity = BLUE_MAX_INTENSITY,
.ambient_color = interpolateColor(blue_intensity, AMBER_AMBIENT, BLUE_DIFFUSE),
.diffuse_color = interpolateColor(blue_intensity, AMBER_DIFFUSE, BLUE_DIFFUSE),
.specular_color = interpolateColor(blue_intensity, AMBER_SPECULAR, BLUE_SPECULAR),
.attenuation_linear = ATTEN_LINEAR,
.attenuation_quadratic = ATTEN_QUAD
};
} else if (dist_exit < MIN_EXIT_DIST) {
const float white_intensity = (MIN_EXIT_DIST - dist_exit) / MIN_EXIT_DIST;
// close to exit, so shade white
// TEMP: still amber
this->properties = TorchlightProperties {
.flickering = true,
.min_intensity = WHITE_MIN_INTENSITY,
.max_intensity = WHITE_MAX_INTENSITY,
.ambient_color = interpolateColor(white_intensity, AMBER_AMBIENT, WHITE_AMBIENT),
.diffuse_color = interpolateColor(white_intensity, AMBER_DIFFUSE, WHITE_DIFFUSE),
.specular_color = interpolateColor(white_intensity, AMBER_SPECULAR, WHITE_SPECULAR),
.attenuation_linear = ATTEN_LINEAR,
.attenuation_quadratic = ATTEN_QUAD
};
} else {
// shade normal amber
this->properties = TorchlightProperties {
.flickering = true,
.min_intensity = AMBER_MIN_INTENSITY,
.max_intensity = AMBER_MAX_INTENSITY,
.ambient_color = AMBER_AMBIENT,
.diffuse_color = AMBER_DIFFUSE,
.specular_color = AMBER_SPECULAR,
.attenuation_linear = ATTEN_LINEAR,
.attenuation_quadratic = ATTEN_QUAD
};
}

// for blue lights
// this->properties = TorchlightProperties {
// .flickering = true,
// .min_intensity = 0.1f,
// .max_intensity = 0.5f,
// .ambient_color = glm::vec3(0.0f, 0.75f, 0.67f),
// .diffuse_color = glm::vec3(0.0f, 0.75f, 0.67f),
// .specular_color = glm::vec3(0.0f, 0.35f, 0.33f),
// .attenuation_linear = 0.07f,
// .attenuation_quadratic = 0.017f
// };
init();
}

Expand Down

0 comments on commit 9840f14

Please sign in to comment.