Engine Code Example

posted in: CyberEnginePost | 0

Below is the code example of how the new entity can be created. I have snipped Sprite Entity class from my engine. More code examples will be added soon.

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "Sprite.h"
#include "Utilities.h"
#include "ShaderManager.h"
#include
#include "PlayerBehaviour.h"

Sprite::Sprite()
{
    m_position.x = 0.0f;
    m_position.y = 0.0f;
    m_position.z = 0.0f;

    m_scale.x = 1.0f;
    m_scale.y = 1.0f;
    m_scale.z = 1.0f;
}
Sprite::~Sprite()
{
}
void Sprite::Init(std::string fileName, RenderingOrder Order, int ColumnNumber, int RowNumber, float posX, float posY)
{
    m_position.x = posX;
    m_position.y = posY;

    AddComponent(new TransformComponentData(m_position, m_scale, "SPRITE"));
    AddComponent(new SpriteRendererComponentData(
        fileName,
        RowNumber,
        ColumnNumber,
        1,
        1,
        60
    ));

    AddComponent(new WindowsSoundComponentData("c:\\windows\\media\\Alarm05.wav"));
    if (HasComponent(BEHAVIOUR_COMPONENT))
    {
        GetComponent()->_behaviourComponent->Start();
    }
}

void Sprite::Update() //IEntity
{
    if (HasComponent(COLLISION_BOX_COMPONENT))
    {
        //Update POsition of the box collider
        m_position = GetComponent()->_position;

        GetComponent()->_position = m_position;
        GetComponent()->_width = GetComponent()->_Mesh2D.GetWidth();
        GetComponent()->_width = GetComponent()->_Mesh2D.GetHeight();

    }
    if (HasComponent(BEHAVIOUR_COMPONENT))
    {
        GetComponent()->_behaviourComponent->Update();
    }
}

RenderingOrder Sprite::GetRenderingOrder()
{
    return m_renderingOrder;
}

this is the example code how we can use this entity inside our game project


1
2
3
4
5
sprite_test = make_shared();
sprite_test->Init("BoneSprite.png", RenderingOrder::RO_FIRST, 5, 1, 100, 20);
sprite_test->GetComponent()->_AutoAnimate = true;
sprite_test->GetComponent()->_UpdateFrameRate = 15;
EntityManager::GetManager()->RegisterEntity(sprite_test);

This is the definition of one of the standard entity, which can be used. Although There is also possible to create custom Entity inside the game project and assign wanted components to it.

To separate entity logic and make our project cleaner we can attach behavior script to this entity and put our logic in it. The standard behavior script have to inherit from IBehaviourComponentClass and implement Start() and Update() function. Start Function is called only once per game ,where update is called every frame.

Below is the code snipped from the PlayerBehaviourScript


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#pragma once
#include "IBehaviourComponent.h"
#include "IEntity.h"

class PlayerBehaviour : public IBehaviourComponent
{
public:
    PlayerBehaviour();
    PlayerBehaviour(IEntity* entity);
    ~PlayerBehaviour();
   
    //IBehaviourComponent
    void Start();
    void Update();
    void SetOwner(IEntity* entity) { _entity = entity; }

private:
    IEntity* _entity;
};

//***************************************************************************************
PlayerBehaviour.cpp
//***************************************************************************************

#include "PlayerBehaviour.h"
#include "GLWindow.h"
#include "EntityManager.h"

PlayerBehaviour::PlayerBehaviour()
{

}

PlayerBehaviour::PlayerBehaviour(IEntity* entity)
{
    _entity = entity;
}

PlayerBehaviour::~PlayerBehaviour()
{
    if (_entity)
        delete _entity;
}

//IBehaviourComponent
void PlayerBehaviour::Start()
{

}

void PlayerBehaviour::Update()
{
    if ((GetKeyState(VK_LBUTTON) & 0x80) != 0)
    {
        EntityManager::GetManager()->GetEntity("Enemy")->GetComponent()->_position.x += 2.0f; _entity->GetComponent()->_ChangeSprite = true;
        _entity->GetComponent()->_position.x += 1;
    }
}

And this is how the Bhaviour script can be assigned to the specific Entity


1
2
3
4
5
6
shared_ptr NewSprite = make_shared();
NewSprite->SetName(name);
// ScriptFactory::GetBehaviourScript(ScriptName);
NewSprite->AddScript(ScriptFactory::GetNewScript(ScriptName));
NewSprite->Init("BoneSprite.png", RenderingOrder::RO_FIRST, 5, 1, 100, 20);
EntityManager::GetManager()->RegisterEntity(NewSprite);

We can get different entity from our script and access its functionality. This make its easy to implement our game logic.

Like above we access Enemy position from PlayerBehaviour script.

Leave a Reply

Your email address will not be published. Required fields are marked *