-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cpp
168 lines (142 loc) · 4.3 KB
/
Game.cpp
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include"Game.h"
namespace nnk {
Game::Game() {
this->initWindow("NNK RPG");
}
Game::~Game() {
delete this->renderWindow;
}
void Game::init() {
this->gameState = GameState::Running;
}
void Game::render() {
this->renderWindow->clear(sf::Color::Green);
//glClearColor(1.0f, 1.0f, 0.0f, 0.0f);
//camera 줌
this->view.setCenter(this->character.getPoint());
this->view.move(this->character.getPoint());
this->renderWindow->setView(view);
//
this->screen.renderScreen(this->renderWindow);
this->character.render(this->renderWindow);
this->cursor.render(this->renderWindow);
//glClear(GL_COLOR_BUFFER_BIT);
this->renderWindow->display();
}
void Game::update() {
if (this->gameState != GameState::End) {
this->pollEvent();
this->screen.updateScreen();
this->character.update();
this->cursor.update();
}
}
void Game::setView() {
this->view.setCenter(this->character.getPoint());
//this->view = sf::View(sf::FloatRect(200.f, 100.f, 150.f, 150.f));
}
void Game::initWindow(std::string windowName) {
this->renderWindow = new sf::RenderWindow(sf::VideoMode(1920, 1080), windowName, sf::Style::Close | sf::Style::Titlebar);
}
void Game::initCommunication() {
#ifdef SERVER
this->server.listen(PORT_NO);
this->communicationMode = CommunicationMode::Receive;
#elif defined CLIENT
client.conncect(PORT_NO);
this->communicationMode = CommunicationMode::Send;
#endif
}
void Game::communication() {
#ifdef SERVER
if (this->communicationMode == CommunicationMode::Send) {
this->server.send();
this->communicationMode = CommunicationMode::Receive;
}
else if (this->communicationMode == CommunicationMode::Receive) {
this->server.receive();
if (this->server.getReceived() > 0) {
// Communication Test
std::cout << "Received: " << this->server.getBuffer() << std::endl;
this->communicationMode = CommunicationMode::Send;
}
}
#elif defined CLIENT
if (this->communicationMode == CommunicationMode::Send) {
this->client.send();
this->communicationMode = CommunicationMode::Receive;
}
else if(this->communicationMode == CommunicationMode::Receive){
this->client.receive();
if (this->client.getReceived() > 0) {
// Communication Test
std::cout << "Received: " << this->client.getBuffer() << std::endl;
this->communicationMode = CommunicationMode::Send;
}
}
#endif
}
void Game::setGameState(GameState gameState) {
this->gameState = gameState;
}
GameState Game::getGameState() {
return this->gameState;
}
sf::RenderWindow* Game::getRenderWindow() {
return this->renderWindow;
}
bool Game::getWindowIsOpen() {
return this->renderWindow->isOpen();
}
void Game::pollEvent() {
bool bLShiftPressed = false;
float step=0.0f;
while (this->renderWindow->pollEvent(this->gameEvent)) {
if (this->gameEvent.type == sf::Event::Closed) {
// 정말 종료하시겠습니까? 이벤트 넣기
this->renderWindow->close();
}
else if (this->gameEvent.type == sf::Event::Resized) {
}
else if (this->gameEvent.type == sf::Event::KeyPressed) {
//keyboard WASD
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
this->character.move(-this->character.getStep(), 0.0f);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
this->character.move(this->character.getStep(), 0.0f);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
this->character.move(0.0f, this->character.getStep());
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
this->character.move(0.0f, -this->character.getStep());
}
//ctrl + key
if (sf::Keyboard::isKeyPressed(sf::Keyboard::LControl)) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
//Game Save
}
}
}
else if (this->gameEvent.type == sf::Event::MouseButtonPressed) {
this->screen.setScreen(new screen::StockCenterScreen());
}
else if (this->gameEvent.type == sf::Event::MouseMoved) {
}
else if (this->gameEvent.type == sf::Event::MouseWheelScrolled) {
}
}
}
//attribute
void Game::addNPC(character::Character npc){
this->npcList.push_back(npc);
}
void Game::deleteNPC(std::string npcName) {
for (std::list<character::Character>::iterator itr = this->npcList.begin(); itr != this->npcList.end(); ++itr) {
if ((*itr).getName() == npcName) {
this->npcList.erase(itr);
}
}
}
}