Temporary commit

This commit is contained in:
S David 2024-05-23 00:59:37 -04:00
parent 8d338d130b
commit 9a91f6eb4a
8 changed files with 271 additions and 9 deletions

View File

@ -0,0 +1,44 @@
{
"main" : {
"type": "GameScene",
"kind": "StaticViewPort",
"dimensions": [ 1280, 720 ],
"layers": [ "background", "foreground" ],
"entities": {
"backdrop" : {
"type": "Image",
"position": [ 0, 0 ],
"size": [ 1280, 720 ],
"layer": "background",
"image": "background.png"
},
"Player" : {
"type": "Player",
"position": [ 0, 296 ],
"size": [ 64, 128 ],
"layer": "foreground"
},
"Enemy" : {
"position": [ 656, 296 ],
"size": [ 64, 128 ],
"layer": "foreground",
"scripts": "Enemy.js"
},
"Ball" : {
"position": [ 640, 360 ],
"size": [ 32, 32 ],
"layer": "foreground",
"scripts": "Ball.js"
}
},
"scripts": {
"Score.js" : {
"layer": "foreground"
},
"Match.js" : {
"layer": "foreground"
}
}
}
}

View File

@ -0,0 +1,41 @@
let previousTime = Date.now();
function updateBall(Ball) {
const currentTime = Date.now();
const deltaTime = (currentTime - previousTime) / 1000; // Convert milliseconds to seconds
previousTime= currentTime;
let x = Ball.x;
let y = Ball.y
let velocityX = Ball.velocityX;
let velocityY = Ball.velocityY;
// Apply acceleration to velocity
velocityX += Ball.acceleration * deltaTime;
velocityY += Ball.acceleration * deltaTime;
// Update ball position based on updated velocity
x += velocityX * deltaTime;
y += velocityY * deltaTime;
// Bounce off the walls
let right_threshold = 720 - Ball.width;
let bottom_threshold = 1280 - Ball.height;
if (x < 0 || x > right_threshold) {
velocityX = -velocityX;
x = Math.max(0, Math.min(x, right_threshold));
}
if (y < 0 || y > bottom_threshold) {
velocityY = -velocityY;
y = Math.max(0, Math.min(y, bottom_threshold));
}
Ball.x = x;
Ball.y = y;
Ball.velocityX = velocityX;
Ball.velocityY = velocityY;
return Ball
}

View File

@ -0,0 +1,34 @@
/* Entity.cpp
* Copyright © 2024 Saul D. Beniquez
* License: Mozilla Public License v2.0 (MPL2)
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v.2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "Entity.hpp"
#include "IOCore/Exception.hpp"
#include <string>
#include <vector>
namespace elemental {
Entity::Entity(
const std::string& type, const std::vector<int>& position,
const std::vector<int>& size, const std::string& layer
)
: type(type), position(position), size(size), layer(layer)
{
}
Entity::~Entity() = default;
void Entity::loadBehavior(const std::string& script)
{
throw IOCore::NotImplementedException();
}
} // namespace elemental

View File

@ -0,0 +1,37 @@
/* Entity.hpp
* Copyright © 2024 Saul D. Beniquez
* License: Mozilla Public License v2.0 (MPL2)
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v.2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at https://mozilla.org/MPL/2.0/.
*/
#pragma once
#include <string>
#include <vector>
namespace elemental {
class Entity {
public:
Entity(
const std::string& type, const std::vector<int>& position,
const std::vector<int>& size, const std::string& layer
);
virtual ~Entity();
// Placeholder method for behaviors
void loadBehavior(const std::string& script);
private:
std::string type;
std::vector<int> position;
std::vector<int> size;
std::string layer;
};
}
// clang-format off
// vim: set foldmethod=syntax foldminlines=10 textwidth=80 ts=8 sts=0 sw=8 noexpandtab ft=cpp.doxygen :

View File

@ -0,0 +1,50 @@
/* Scene.cpp
* Copyright © 2024 Saul D. Beniquez
* License: Mozilla Public License v2.0 (MPL2)
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v.2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "Scene.hpp"
#include "JsonConfigFile.hpp"
namespace elemental {
using configuration::JsonConfigFile;
Scene::Scene(SDL_Renderer* renderer, JsonConfigFile& configFile)
: renderer(renderer)
{
auto config = configFile.jsonDataRef();
dimensions = config["dimensions"].get<std::vector<int>>();
layers = config["layers"].get<std::vector<std::string>>();
for (const auto& entity : config["entities"]) {
entities[entity["name"]] = std::make_shared<Entity>(
entity["type"],
entity["position"],
entity["size"],
entity["layer"]
);
}
for (const auto& script : config["scripts"]) {
scripts[script["name"]] = script["path"];
}
}
Scene::~Scene() = default;
void Scene::loadResources()
{
for (const auto& script : scripts) {
load_script(script.second);
}
}
void Scene::setupEntities()
{
for (const auto& entity : entities) {
entity.second->loadBehavior(scripts[entity.first]);
}
}

View File

@ -0,0 +1,56 @@
/* Scene.hpp
* Copyright © 2024 Saul D. Beniquez
* License: Mozilla Public License v2.0 (MPL2)
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v.2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at https://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "JsonConfigFile.hpp"
#include "types/rendering.hpp"
#include <SDL.h>
#include <nlohmann/json.hpp>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
namespace elemental {
namespace {
using configuration::JsonConfigFile;
}
class Entity;
class Scene {
using Dimensions = Area;
public:
// Constructor from JSON
Scene(SDL_Renderer* renderer, JsonConfigFile& config);
virtual ~Scene();
// Resource loading and entity setup methods
void loadResources();
void setupEntities();
private:
SDL_Renderer* renderer;
Dimensions dimensions;
std::vector<std::string> layers;
std::unordered_map<std::string, std::shared_ptr<Entity>> entities;
std::unordered_map<std::string, std::string> scripts;
// Placeholder for script handling
void load_script(const std::string& script);
};
} // namespace elemental
// clang-format off
// vim: set foldmethod=syntax foldminlines=10 textwidth=80 ts=8 sts=0 sw=8 noexpandtab ft=cpp.doxygen :

View File

@ -32,10 +32,10 @@ class JsonConfigFile : public FileResource {
auto read() -> nlohmann::json&;
void write();
template<typename T_>
[[nodiscard]] auto get() const -> T_
template<typename TData>
[[nodiscard]] auto get() const -> const TData&
{
return config_json.get<T_>();
return config_json.get<TData>();
}
template<typename T_>
@ -44,7 +44,7 @@ class JsonConfigFile : public FileResource {
config_json = value;
}
auto getJsonData() -> nlohmann::json& { return this->config_json; };
auto jsonDataRef() -> nlohmann::json& { return this->config_json; };
protected:
nlohmann::json config_json;

View File

@ -90,7 +90,7 @@ BEGIN_TEST_SUITE("elemental::JsonConfigFile")
SECTION("JsonConfigFile w/ valid path")
{
auto config = JsonConfigFile(kINPUT_FILE_PATH);
auto& config_data = config.getJsonData();
auto& config_data = config.jsonDataRef();
REQUIRE(config_data.empty());
}
@ -106,7 +106,7 @@ BEGIN_TEST_SUITE("elemental::JsonConfigFile")
FIXTURE_TEST("JsonConfigFile::Read")
{
auto config_file = JsonConfigFile(kINPUT_FILE_PATH);
auto& json_data = config_file.getJsonData();
auto& json_data = config_file.jsonDataRef();
SECTION("JsonConfigFile::Read w/ valid file")
{
@ -143,7 +143,7 @@ BEGIN_TEST_SUITE("elemental::JsonConfigFile")
SECTION("Create File and Read It back In")
{
auto config_file = JsonConfigFile(kINPUT_FILE_PATH);
auto& test_data = config_file.getJsonData();
auto& test_data = config_file.jsonDataRef();
test_data["one"] = "1";
test_data["resolution"] = "1280x720";
@ -179,7 +179,7 @@ BEGIN_TEST_SUITE("elemental::JsonConfigFile")
)
{
auto config_file = JsonConfigFile(kINPUT_FILE_PATH);
auto& json_data = config_file.getJsonData();
auto& json_data = config_file.jsonDataRef();
config_file.read();
auto obtained_data =
@ -201,7 +201,7 @@ BEGIN_TEST_SUITE("elemental::JsonConfigFile")
config_file.set(test_data);
auto& json_data = config_file.getJsonData();
auto& json_data = config_file.jsonDataRef();
REQUIRE(json_data["one"] == test_data["one"]);
REQUIRE(json_data["resolution"] == test_data["resolution"]);