This repository has been archived by the owner on Aug 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Game.hpp
87 lines (64 loc) · 2.38 KB
/
Game.hpp
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
#pragma once
#include <glm/glm.hpp>
#include <string>
#include <list>
#include <random>
struct Connection;
//Game state, separate from rendering.
//Currently set up for a "client sends controls" / "server sends whole state" situation.
enum class Message : uint8_t {
C2S_Controls = 1, //Greg!
S2C_State = 's',
//...
};
//used to represent a control input:
struct Button {
uint8_t downs = 0; //times the button has been pressed
bool pressed = false; //is the button pressed now
};
//state of one player in the game:
struct Player {
//player inputs (sent from client):
struct Controls {
Button left, right, up, down, jump;
void send_controls_message(Connection *connection) const;
//returns 'false' if no message or not a controls message,
//returns 'true' if read a controls message,
//throws on malformed controls message
bool recv_controls_message(Connection *connection);
} controls;
//player state (sent from server):
glm::vec2 position = glm::vec2(0.0f, 0.0f);
glm::vec2 velocity = glm::vec2(0.0f, 0.0f);
glm::vec3 color = glm::vec3(1.0f, 1.0f, 1.0f);
std::string name = "";
};
struct Game {
std::list< Player > players; //(using list so they can have stable addresses)
Player *spawn_player(); //add player the end of the players list (may also, e.g., play some spawn anim)
void remove_player(Player *); //remove player from game (may also, e.g., play some despawn anim)
std::mt19937 mt; //used for spawning players
uint32_t next_player_number = 1; //used for naming players
Game();
//state update function:
void update(float elapsed);
//constants:
//the update rate on the server:
inline static constexpr float Tick = 1.0f / 30.0f;
//arena size:
inline static constexpr glm::vec2 ArenaMin = glm::vec2(-0.75f, -1.0f);
inline static constexpr glm::vec2 ArenaMax = glm::vec2( 0.75f, 1.0f);
//player constants:
inline static constexpr float PlayerRadius = 0.06f;
inline static constexpr float PlayerSpeed = 2.0f;
inline static constexpr float PlayerAccelHalflife = 0.25f;
//---- communication helpers ----
//used by client:
//set game state from data in connection buffer
// (return true if data was read)
bool recv_state_message(Connection *connection);
//used by server:
//send game state.
// Will move "connection_player" to the front of the front of the sent list.
void send_state_message(Connection *connection, Player *connection_player = nullptr) const;
};