Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ add_executable(${PROJECT_NAME}
src/game/components/health.cpp
src/game/components/player.cpp
src/game/game-registration.cpp
src/game/systems/collision-system.cpp

src/common/systems/forward-renderer.cpp
)
Expand Down
306 changes: 306 additions & 0 deletions config/json-configs/collision.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,306 @@
{
"start-scene": "menu",
"window": {
"title": "Default Game Window",
"size": {
"width": 1280,
"height": 720
},
"fullscreen": false
},
"scene": {
"renderer": {
"sky": "assets/textures/sky.jpg",
"postprocess": "assets/shaders/postprocess/vignette.frag"
},
"assets": {
"shaders": {
"tinted": {
"vs": "assets/shaders/tinted.vert",
"fs": "assets/shaders/tinted.frag"
},
"textured": {
"vs": "assets/shaders/textured.vert",
"fs": "assets/shaders/textured.frag"
}
},
"textures": {
"moon": "assets/textures/moon.jpg",
"grass": "assets/textures/grass_ground_d.jpg",
"wood": "assets/textures/wood.jpg",
"glass": "assets/textures/glass-panels.png",
"monkey": "assets/textures/monkey.png"
},
"meshes": {
"cube": "assets/models/cube.obj",
"monkey": "assets/models/monkey.obj",
"plane": "assets/models/plane.obj",
"sphere": "assets/models/sphere.obj"
},
"samplers": {
"default": {},
"pixelated": {
"MAG_FILTER": "GL_NEAREST"
}
},
"materials": {
"metal": {
"type": "tinted",
"shader": "tinted",
"pipelineState": {
"faceCulling": {
"enabled": false
},
"depthTesting": {
"enabled": true
}
},
"tint": [0.45, 0.4, 0.5, 1]
},
"glass": {
"type": "textured",
"shader": "textured",
"pipelineState": {
"faceCulling": {
"enabled": false
},
"depthTesting": {
"enabled": true
},
"blending": {
"enabled": true,
"sourceFactor": "GL_SRC_ALPHA",
"destinationFactor": "GL_ONE_MINUS_SRC_ALPHA"
},
"depthMask": false
},
"transparent": true,
"tint": [1, 1, 1, 1],
"texture": "glass",
"sampler": "pixelated"
},
"grass": {
"type": "textured",
"shader": "textured",
"pipelineState": {
"faceCulling": {
"enabled": false
},
"depthTesting": {
"enabled": true
}
},
"tint": [1, 1, 1, 1],
"texture": "grass",
"sampler": "default"
},
"monkey": {
"type": "textured",
"shader": "textured",
"pipelineState": {
"faceCulling": {
"enabled": false
},
"depthTesting": {
"enabled": true
}
},
"tint": [1, 1, 1, 1],
"texture": "monkey",
"sampler": "default"
},
"moon": {
"type": "textured",
"shader": "textured",
"pipelineState": {
"faceCulling": {
"enabled": false
},
"depthTesting": {
"enabled": true
}
},
"tint": [1, 1, 1, 1],
"texture": "moon",
"sampler": "default"
}
}
},
"world": [
{
"position": [0, 0, 10],
"name": "Player Entity",
"components": [
{
"type": "Camera"
},
{
"type": "Free Camera Controller"
},
{
"type": "Collider",
"shape": "Capsule",
"height": 1.8,
"radius": 0.4,
"layer": "player"
}
],
"children": [
{
"name": "Player Sword",
"position": [1, -1, -1],
"rotation": [45, 45, 0],
"scale": [0.1, 0.1, 1.0],
"components": [
{
"type": "Mesh Renderer",
"mesh": "cube",
"material": "metal"
},
{
"type": "Collider",
"shape": "Capsule",
"height": 1.0,
"radius": 0.1,
"layer": "player"
}
]
}
]
},
{
"rotation": [-45, 0, 0],
"components": [
{
"type": "Mesh Renderer",
"mesh": "monkey",
"material": "monkey"
},
{
"type": "Movement",
"linearVelocity": [0, 0.1, 0],
"angularVelocity": [0, 45, 0]
}
]
},
{
"position": [0, -1, 0],
"rotation": [-90, 0, 0],
"scale": [10, 10, 1],
"components": [
{
"type": "Mesh Renderer",
"mesh": "plane",
"material": "grass"
}
]
},
{
"position": [0, 1, 2],
"rotation": [0, 0, 0],
"scale": [2, 2, 2],
"components": [
{
"type": "Mesh Renderer",
"mesh": "plane",
"material": "glass"
}
]
},
{
"position": [0, 1, -2],
"rotation": [0, 0, 0],
"scale": [2, 2, 2],
"components": [
{
"type": "Mesh Renderer",
"mesh": "plane",
"material": "glass"
}
]
},
{
"position": [2, 1, 0],
"rotation": [0, 90, 0],
"scale": [2, 2, 2],
"components": [
{
"type": "Mesh Renderer",
"mesh": "plane",
"material": "glass"
}
]
},
{
"position": [-2, 1, 0],
"rotation": [0, 90, 0],
"scale": [2, 2, 2],
"components": [
{
"type": "Mesh Renderer",
"mesh": "plane",
"material": "glass"
}
]
},
{
"position": [0, 3, 0],
"rotation": [90, 0, 0],
"scale": [2, 2, 2],
"components": [
{
"type": "Mesh Renderer",
"mesh": "plane",
"material": "glass"
}
]
},
{
"name": "Moon Entity",
"position": [0, 3, 0],
"rotation": [45, 45, 0],
"scale": [5, 5, 5],
"components": [
{
"type": "Mesh Renderer",
"mesh": "sphere",
"material": "moon"
},
{
"type": "Movement",
"angularVelocity": [0, 90, 0]
},
{
"type": "Collider",
"shape": "Sphere",
"radius": 1,
"layer": "enemy"
}
]
},
{
"name": "Second Moon Entity",
"position": [30, 3, 0],
"rotation": [45, 45, 0],
"scale": [5, 5, 5],
"components": [
{
"type": "Mesh Renderer",
"mesh": "sphere",
"material": "moon"
},
{
"type": "Movement",
"angularVelocity": [0, 90, 0]
},
{
"type": "Collider",
"shape": "Sphere",
"radius": 1,
"layer": "enemy"
}
]
}
]
}
}
11 changes: 10 additions & 1 deletion src/game/components/collider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,24 @@ static gameplay::ColliderShape parseColliderShape(const std::string& value, game
return fallback;
}

static inline gameplay::CollisionLayer layerStringToGroup(const std::string& layer) {
if (layer == "player") return gameplay::CollisionLayer::LAYER_PLAYER;
if (layer == "enemy") return gameplay::CollisionLayer::LAYER_ENEMY;
if (layer == "projectile") return gameplay::CollisionLayer::LAYER_PROJECTILE;
if (layer == "trigger") return gameplay::CollisionLayer::LAYER_TRIGGER;
return gameplay::CollisionLayer::LAYER_ENVIRONMENT; // environment is the default layer
}

namespace gameplay {

void ColliderComponent::deserialize(const nlohmann::json& data) {
if (!data.is_object()) return;
shape = parseColliderShape(data.value("shape", std::string("Sphere")), shape);
layer = data.value("layer", layer);
radius = data.value("radius", radius);
height = data.value("height", height);
isTrigger = data.value("isTrigger", isTrigger);
std::string stringLayer = data.value("layer", "");
layer = layerStringToGroup(stringLayer);
}

} // namespace gameplay
12 changes: 10 additions & 2 deletions src/game/components/collider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@ namespace gameplay {

enum class ColliderShape { Sphere, Capsule };

enum CollisionLayer : short {
LAYER_PLAYER = 1 << 0, // bit 0: 0000 0001
LAYER_ENEMY = 1 << 1, // bit 1: 0000 0010
LAYER_ENVIRONMENT = 1 << 2, // bit 2: 0000 0100
LAYER_PROJECTILE = 1 << 3, // bit 3: 0000 1000
LAYER_TRIGGER = 1 << 4, // bit 4: 0001 0000
};

class ColliderComponent : public our::Component {
public:
ColliderShape shape = ColliderShape::Sphere;
std::string layer = "default";
CollisionLayer layer = CollisionLayer::LAYER_ENVIRONMENT;
float radius = 0.5f;
float height = 1.0f;
float height = 1.0f; // must be the total height
bool isTrigger = false;

static std::string getID() {
Expand Down
Loading