Skip to content

Commit

Permalink
Refactor: Cube factory atta 0.1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
brenocq committed Aug 18, 2022
1 parent 9465872 commit 6f020b0
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 35 deletions.
8 changes: 8 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
Language: Cpp
BasedOnStyle: LLVM
PointerAlignment: Left
AlwaysBreakTemplateDeclarations: true
ColumnLimit: 150
IndentWidth: 4
...
2 changes: 1 addition & 1 deletion cube-factory/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.12)
project(cube-factory VERSION 1.0.0 LANGUAGES CXX)
find_package(atta 0.0.0.3 REQUIRED)
find_package(atta 0.1.0.0 REQUIRED)

atta_add_target(cubeScript "src/cubeScript.cpp")
Binary file modified cube-factory/cube-factory.atta
Binary file not shown.
63 changes: 39 additions & 24 deletions cube-factory/src/cubeScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,59 @@
// By Breno Cunha Queiroz
//--------------------------------------------------
#include "cubeScript.h"
#include <atta/componentSystem/components/transformComponent.h>
#include <atta/componentSystem/components/materialComponent.h>
#include <atta/componentSystem/components/nameComponent.h>
#include <atta/component/components/material.h>
#include <atta/component/components/name.h>
#include <atta/component/components/transform.h>
#include <atta/resource/interface.h>
#include <atta/resource/resources/material.h>

#define QTY_CLONES 100
namespace scr = atta::script;
namespace cmp = atta::component;
namespace rsc = atta::resource;

void CubeScript::update(atta::Entity entity, float dt)
{
// Update time
void CubeScript::update(cmp::Entity entity, float dt) {
// The static variables are shared among all CubeScripts
static const cmp::Entity prototype(1);
static unsigned numClones = 0;
static float time = 0;

int cloneId = entity.getCloneId();

// This static variable is shared among all clones, we are using the clone 0 to update the time
if(cloneId == 0)
{
time += dt*0.1;
if(time > 2*M_PI) time -= 2*M_PI;

//----- Get number of clones -----//
if (numClones == 0)
numClones = cmp::getFactory(prototype)->getNumClones();

//----- Advance time -----//
// Use clone 0 to advance the time
if (cloneId == 0) {
time += dt * 0.1;
if (time > 2 * M_PI)
time -= 2 * M_PI;
}

//----- Transform Component -----//
atta::TransformComponent* t = entity.getComponent<atta::TransformComponent>();
// Using cloneId to calculate clone specific transform
unsigned row = cloneId % (unsigned)sqrt(QTY_CLONES);
unsigned col = cloneId / (unsigned)sqrt(QTY_CLONES);
float offset = 8*M_PI/QTY_CLONES * (col+row)+time;
unsigned row = cloneId % (unsigned)sqrt(numClones);
unsigned col = cloneId / (unsigned)sqrt(numClones);
float offset = 8 * M_PI / numClones * (col + row) + time;

// Update clone transform
cmp::Transform* t = entity.get<cmp::Transform>();
t->position.x = row;
t->position.y = col;
t->position.z = sin(offset);

//----- Material Component -----//
atta::MaterialComponent* m = entity.getComponent<atta::MaterialComponent>();
m->albedo.x = (t->position.z+1.0f)*0.5f;
m->albedo.y = 1.0f-m->albedo.x;
m->albedo.z = 1.0f;
// Set material resource
std::string materialName = "Material " + std::to_string(cloneId);
rsc::Material* m = rsc::get<rsc::Material>(materialName);
entity.get<cmp::Material>()->set(m);

// Update material color
m->color.x = (t->position.z + 1.0f) * 0.5f;
m->color.y = 1.0f - m->color.x;
m->color.z = 1.0f;

//----- Name Component -----//
atta::NameComponent* n = entity.getComponent<atta::NameComponent>();
strcpy(n->name, ("Cube clone "+std::to_string(cloneId)).c_str());
cmp::Name* n = entity.get<cmp::Name>();
strcpy(n->name, ("Cube clone " + std::to_string(cloneId)).c_str());
}
10 changes: 6 additions & 4 deletions cube-factory/src/cubeScript.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
//--------------------------------------------------
#ifndef CUBE_SCRIPT_H
#define CUBE_SCRIPT_H
#include <atta/pch.h>
#include <atta/scriptSystem/script.h>
#include <atta/script/script.h>

class CubeScript : public atta::Script
namespace scr = atta::script;
namespace cmp = atta::component;

class CubeScript : public scr::Script
{
public:
void update(atta::Entity entity, float dt) override;
void update(cmp::Entity entity, float dt) override;
};

ATTA_REGISTER_SCRIPT(CubeScript)
Expand Down
4 changes: 2 additions & 2 deletions logging/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.12)
cmake_minimum_required(VERSION 3.14)
project(logging VERSION 1.0.0 LANGUAGES CXX)
find_package(atta 0.0.0.3 REQUIRED)
find_package(atta 0.1.0.0 REQUIRED)

atta_add_target(projectScript "src/projectScript.cpp")
Binary file modified logging/logging.atta
Binary file not shown.
1 change: 0 additions & 1 deletion logging/src/projectScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// By Breno Cunha Queiroz
//--------------------------------------------------
#include "projectScript.h"
#include <atta/core/math/math.h>

//---------------------------------//
//---------- Description ----------//
Expand Down
7 changes: 4 additions & 3 deletions logging/src/projectScript.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
//--------------------------------------------------
#ifndef PROJECT_SCRIPT_H
#define PROJECT_SCRIPT_H
#include <atta/pch.h>
#include <atta/scriptSystem/projectScript.h>
#include <atta/script/projectScript.h>

class Project : public atta::ProjectScript
namespace scr = atta::script;

class Project : public scr::ProjectScript
{
public:
void onLoad() override;
Expand Down

0 comments on commit 6f020b0

Please sign in to comment.