forked from botanegg/pseudo3DEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Camera.h
150 lines (115 loc) · 4.86 KB
/
Camera.h
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
//
// Created by ivan- on 18.01.2020.
//
#ifndef PSEUDO3DENGINE_CAMERA_H
#define PSEUDO3DENGINE_CAMERA_H
#include <algorithm>
#include "Object2D.h"
#include "World.h"
#include "settings.h"
#include "Menu.h"
#include "Weapon.h"
struct RGB {
int RED = 255;
int GREEN = 255;
int BLUE = 255;
};
struct RayCastStructure {
double distance; // How far is this texture
double progress; // progress defines the point of texture we should load
std::string object; // particular object. We need this to show particular texture.
double height; // objects has different height
std::vector<RayCastStructure> v_mirrorRayCast; // When we have mirror, we should know about all objects we can see
};
struct CollisionInformation {
double distance;
Point2D collisionPoint;
std::pair<Point2D, Point2D> edge;
};
class Camera : public Circle2D {
private:
std::vector<std::vector<RayCastStructure>> v_distances;
//For collision detection
std::vector<CollisionInformation> allCollisions;
double d_direction;
double d_fieldOfView;
double d_depth;
double d_walkSpeed;
double d_viewSpeed;
double i_health;
bool b_collision = true;
World& W_world;
sf::Vector2i localMousePosition;
bool b_textures = true;
bool b_smooth = false;
std::vector<Weapon> v_weapons;
int i_selectedWeapon = 0;
sf::SoundBuffer walkSoundBuffer;
sf::Sound walkSound;
std::string s_lastKill;
void objectsRayCrossed(std::pair<Point2D, Point2D> ray, std::vector<RayCastStructure>& v_rayCastStruct, const std::string& name, int reflections = 0);
void drawVerticalStrip(sf::RenderWindow& window, const RayCastStructure& obj, int shift, int f);
void recursiveDrawing(sf::RenderWindow& window, const std::vector<RayCastStructure>& v_RayCastStructure, int shift, int rec = 1);
static void recursiveIncreaseDistance(std::vector<RayCastStructure>& v_RayCastStructure, double distance);
static double scalarWithNormal(Point2D edge, Point2D vector);
void fire();
std::pair<std::string, double> cameraRayCheck(RayCastStructure& structure);
std::map<std::string, Camera&> m_playersOnTheScreen;
static void drawHealth(sf::RenderWindow& window, int x, int y, int width, int health);
public:
explicit Camera(World& world, Point2D position, double direction = 0, std::string texture = SKIN, int health = 100, double fieldOfView = PI/2, double depth = 25, double walkSpeed = 1.7, double viewSpeed = .005)
: W_world(world), Circle2D(COLLISION_DISTANCE, position, 0.5, texture, 4), d_direction(direction), d_fieldOfView(fieldOfView), d_depth(depth), d_walkSpeed(walkSpeed), d_viewSpeed(viewSpeed), i_health(health) {
Weapon weapon1(100000);
weapon1.choiceWeapon("shotgun");
v_weapons.push_back(weapon1);
walkSoundBuffer.loadFromFile(WALK_SOUND);
walkSound.setBuffer(walkSoundBuffer);
walkSound.setLoop(true);
walkSound.setVolume(50.f);
}
Camera(const Camera& camera) : W_world(camera.W_world) { // copy constructor
d_height = camera.d_height;
v_points2D = camera.v_points2D;
T_texture = camera.T_texture;
s_texture = camera.s_texture;
p_position = camera.p_position;
v_distances = camera.v_distances;
allCollisions = camera.allCollisions;
d_direction = camera.d_direction;
d_depth = camera.d_depth;
d_fieldOfView = camera.d_fieldOfView;
d_walkSpeed = camera.d_walkSpeed;
d_viewSpeed = camera.d_viewSpeed;
i_health = camera.i_health;
b_collision = camera.b_collision;
b_textures = camera.b_textures;
b_smooth = camera.b_smooth;
localMousePosition = camera.localMousePosition;
v_weapons = camera.v_weapons;
i_selectedWeapon = camera.i_selectedWeapon;
walkSoundBuffer = camera.walkSoundBuffer;
walkSound = camera.walkSound;
setName(camera.getName());
}
void updateDistances(const World& world);
void drawCameraView(sf::RenderWindow& window);
void draw(sf::RenderWindow& window) override;
bool keyboardControl(double elapsedTime, sf::RenderWindow& window);
void shiftPrecise(Point2D vector);
bool isSmooth() { return b_smooth; }
void switchSmooth() { b_smooth = !b_smooth; }
bool isCollision() { return b_collision; }
void switchCollision() { b_collision = !b_collision; }
bool isTextures() { return b_textures; }
void switchTextures() { b_textures = !b_textures; }
void previousWeapon();
void nextWeapon();
double health() const { return i_health; }
bool reduceHealth(double damage = 0);
void fullHealth () { i_health = 100; }
void setHealth(double h) {i_health = h; }
int type() override { return 1; }
std::string lastKill() { return s_lastKill;}
void cleanLastKill(){s_lastKill = "";}
};
#endif //PSEUDO3DENGINE_CAMERA_H