Repository navigation
Expand file tree
/
Copy pathMainService.cpp
More file actions
111 lines (81 loc) · 2.39 KB
/
Copy pathMainService.cpp
File metadata and controls
111 lines (81 loc) · 2.39 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#pragma once
// Local includes
#include "MainService.h"
#include "cBall.h"
#include "cBlock.h"
#include "cPaddle.h"
#include "constants.h"
// AST-Utilities includes
#include <AstuSuite2D.h>
using namespace astu::suite2d;
using namespace astu2d;
using namespace astu;
using namespace std;
#define SPEED 1.0f
const EntityFamily MainService::FAMILY = EntityFamily::Create<cBall, CPose>();
MainService::MainService(int updatePriority)
: BaseService("Main Service")
{
// Intentionally left empty.
}
void MainService::OnStartup()
{
GetCamera().ShowFitting(WORLD_WIDTH,WORLD_HEIGHT);
numLifes = 3;
AddEntity("Ball");
AddEntity("Paddle");
PlaceBoundaries();
}
void MainService::OnShutdown()
{
// Cleanup
ASTU_SERVICE(SceneGraph).GetRoot()->DetachAll();
}
void MainService::UpdateLife(int deltalife){
numLifes += deltalife;
if(numLifes < 0){
std::cout <<"Well, you lost. Feels bad man. Would you quit please?" << endl;
std::cout <<"Nothing is going to happen if you press spacebar again, I swear" << endl;
}
else{
std::cout <<"current hp: " << numLifes << endl;
std::cout <<"Press Space to keep playing" << endl;
}
}
void MainService::UpdateNrBlocks(int nr){
numOfBlocks = nr;
}
void MainService::Win(){
std::cout <<"EYYYYY YOU WON" << endl;
std::cout << "Gj, you deserve this ☆⌒(*^-゜)v"<< endl;
}
Vector2f MainService::GetCenter()
{
auto& winSrv = ASTU_SERVICE(WindowService);
return Vector2f((float) winSrv.GetWidth() / 2, (float) winSrv.GetHeight() / 2);
}
bool MainService::OnSignal(const GameEvent &signal){
switch(signal.type){
case GameEvent::Type::BallOut:
UpdateLife(-1);
break;
case GameEvent::Type::BlocksCreated:
UpdateNrBlocks(signal.intValue);
break;
case GameEvent::Type::BlockDestroyed:
if(--numOfBlocks<=0){
EmitSignal(GameEvent::Type::AllBlocksDestroyed);
}
break;
case GameEvent::Type::AllBlocksDestroyed:
Win();
break;
}
return false;
}
void MainService::PlaceBoundaries(){
AddEntity("VBoundary",-WORLD_HALF_WIDTH-WORLD_WIDTH/2,0);
AddEntity("VBoundary",WORLD_HALF_WIDTH+WORLD_WIDTH/2,0);
AddEntity("DBoundary",0,WORLD_HALF_HEIGHT+BOUNDARY_THICKNESS/2);
AddEntity("HBoundary",0,-WORLD_HALF_HEIGHT-BOUNDARY_THICKNESS/2);
}