-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
126 lines (100 loc) · 3.96 KB
/
Game.cpp
File metadata and controls
126 lines (100 loc) · 3.96 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
#include "Game.h"
#include "Field.h"
#include <fstream>
static const float VIEW_HEIGHT = 512.0f;
Game::Game(sf::RenderWindow& window):
window(window)
{
}
Game::~Game()
{
}
int Game::load()
{
return 0;
}
int Game::save()
{
return 0;
}
void Game::resizeView(const sf::RenderWindow& window, sf::View& view)
{
float windowRatio = (float)window.getSize().x / window.getSize().y;
view.setSize(VIEW_HEIGHT * windowRatio, VIEW_HEIGHT);
}
void Game::start()
{
sf::View view(sf::Vector2f(0.0f, 0.0f), sf::Vector2f((float)window.getSize().x, (float)window.getSize().y)); // getSize îêíà
sf::Texture wallTexture, floorTexture;
wallTexture.loadFromFile("textures/wall.png");
floorTexture.loadFromFile("textures/floor.png");
sf::Texture* textures[2] = {&floorTexture, &wallTexture};
Field field(textures);
float switchTime = 0.3f; // Ñêîðîñòü ñìåíû ñïðàéòîâ â ñåêóíäó
float speed = 100.0f; // ñêîðîñòü èçìåðÿåòñÿ â ... ?
sf::Texture playerTexture;
playerTexture.loadFromFile("textures/frisk.png");
Player& player = Player::getInstance(); // Ðàçìåð èçîáðàæåíèÿ â ñïðàéòàõ - 2*4, õ*ó
player.create(&playerTexture, sf::Vector2u(4, 3), switchTime, speed); // Ýòî SINGLETON !!!
sf::Texture wallUATexture;
wallUATexture.loadFromFile("textures/test5.png");
Platform wallUA(&wallUATexture, sf::Vector2f(100.0f, 100.0f), sf::Vector2f(150.0f, 300.0f));
sf::Texture wallRUTexture;
wallRUTexture.loadFromFile("textures/test6.png");
Platform wallRU(&wallRUTexture, sf::Vector2f(100.0f, 100.0f), sf::Vector2f(300.0f, 150.0f));
float deltaTime = 0.0f;
sf::Clock clock;
while (window.isOpen()) // Ñäåëàòü ñìåíó òèêîâ == 50
{
deltaTime = clock.restart().asSeconds();
sf::Event event;
while (window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
window.close();
break;
case sf::Event::Resized:
std::cout << "\nNew size is:\t" << event.size.width << "\t" << event.size.height << std::endl;
resizeView(window, view);
break;
case sf::Event::TextEntered:
std::cout << (char)event.text.unicode;
break;
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::F))
{
fullScreen = !fullScreen;
if (fullScreen)
{
window.create(sf::VideoMode::getDesktopMode(), "OOP Game", sf::Style::Fullscreen);
resizeView(window, view);
}
else
{
window.create(sf::VideoMode(512, 512), "OOP Game", sf::Style::Close | sf::Style::Resize);
resizeView(window, view);
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
{
return; // ÂÛÕÎÄ Â ÌÅÍÞ
}
wallRU.getCollider().checkCollision(player.getCollider(), 0.6f);
wallUA.getCollider().checkCollision(player.getCollider(), 0.6f);
wallRU.getCollider().checkCollision(wallUA.getCollider(), 0.6f);
wallUA.getCollider().checkCollision(wallRU.getCollider(), 0.6f);
player.update(deltaTime);
view.setCenter(player.getPosition());
window.clear(sf::Color(190, 242, 138));
window.setView(view);
field.draw(window);
player.draw(window);
wallUA.draw(window);
wallRU.draw(window);
window.display();
}
}