2021-09-13 15:53:43 +03:00
|
|
|
//
|
|
|
|
// Created by Иван Ильин on 14.03.2021.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef SHOOTER_PLAYER_H
|
|
|
|
#define SHOOTER_PLAYER_H
|
|
|
|
|
|
|
|
#include <SFML/Audio/Sound.hpp>
|
|
|
|
#include <utility>
|
|
|
|
#include <ResourceManager.h>
|
|
|
|
#include "Camera.h"
|
|
|
|
#include "World.h"
|
2021-09-14 13:47:53 +03:00
|
|
|
#include "weapon/Ak47.h"
|
|
|
|
#include "weapon/Shotgun.h"
|
|
|
|
#include "weapon/Gun.h"
|
|
|
|
#include "weapon/Gold_Ak47.h"
|
|
|
|
#include "weapon/Rifle.h"
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-09-19 11:25:10 +03:00
|
|
|
class Player : public RigidBody{
|
2021-09-13 15:53:43 +03:00
|
|
|
private:
|
|
|
|
double _healthMax = 100;
|
|
|
|
double _health = _healthMax;
|
|
|
|
|
|
|
|
double _abilityMax = 10;
|
|
|
|
double _ability = _abilityMax;
|
|
|
|
|
|
|
|
double _headAngle = 0;
|
|
|
|
|
|
|
|
unsigned _kills = 0;
|
|
|
|
unsigned _deaths = 0;
|
|
|
|
|
2021-09-19 11:25:10 +03:00
|
|
|
double _g = 35;
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
std::shared_ptr<Camera> _camera;
|
|
|
|
std::shared_ptr<Screen> _screen;
|
|
|
|
|
|
|
|
std::shared_ptr<World> _world;
|
|
|
|
|
|
|
|
// sounds
|
2021-09-19 11:25:10 +03:00
|
|
|
sf::Sound _killSound;
|
|
|
|
sf::Sound _deathSound;
|
|
|
|
sf::Sound _changeWeaponSound;
|
|
|
|
sf::Sound _fullHealthSound;
|
|
|
|
sf::Sound _fullAbilitySound;
|
2021-09-14 13:47:53 +03:00
|
|
|
|
2021-09-13 15:53:43 +03:00
|
|
|
std::string _name = "im";
|
|
|
|
|
|
|
|
std::vector<std::shared_ptr<Weapon>> _weapons;
|
|
|
|
uint8_t _selectedWeapon = 0;
|
|
|
|
|
2021-09-19 11:25:10 +03:00
|
|
|
std::function<void(sf::Uint16 targetId, double)> _damagePlayerCallBack;
|
|
|
|
std::function<void(const Point4D&, const Point4D&)> _addTraceCallBack;
|
|
|
|
std::function<void(const std::string&)> _takeBonusCallBack;
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
Player() {
|
|
|
|
loadObj("../obj/cube.obj", "", Point4D{0.5, 1.9, 0.5});
|
2021-09-19 11:25:10 +03:00
|
|
|
setAcceleration(Point4D{0, -_g, 0});
|
2021-09-13 15:53:43 +03:00
|
|
|
setCollision(true);
|
|
|
|
setVisible(false);
|
|
|
|
setColor({240, 168, 168});
|
2021-09-19 11:25:10 +03:00
|
|
|
|
|
|
|
_changeWeaponSound.setBuffer(*ResourceManager::loadSoundBuffer("../sound/weapons/change_weapon.ogg"));
|
|
|
|
|
|
|
|
_fullHealthSound.setBuffer(*ResourceManager::loadSoundBuffer("../sound/fullHealth.ogg"));
|
|
|
|
_fullAbilitySound.setBuffer(*ResourceManager::loadSoundBuffer("../sound/fullAbility.ogg"));
|
|
|
|
|
|
|
|
setCollisionCallBack([this](const std::string& objName, std::shared_ptr<RigidBody> obj) {collisionWithObject(objName, obj);});
|
2021-09-13 15:53:43 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
void update();
|
|
|
|
|
2021-09-14 13:47:53 +03:00
|
|
|
void attachCamera(std::shared_ptr<Camera> camera, std::shared_ptr<Screen> screen) {
|
2021-09-19 11:25:10 +03:00
|
|
|
_camera = camera;
|
|
|
|
_screen = screen;
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-09-14 13:47:53 +03:00
|
|
|
_camera->translateToPoint(position() + Point4D{0, 1.8, 0});
|
|
|
|
this->attach(_camera);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
2021-09-14 13:47:53 +03:00
|
|
|
void attachWorld(std::shared_ptr<World> world, const Point4D& pos = Point4D{0, 30, 0}) {
|
2021-09-19 11:25:10 +03:00
|
|
|
_world = world;
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
translate(pos);
|
|
|
|
|
|
|
|
initWeapons();
|
|
|
|
}
|
|
|
|
|
|
|
|
void setHealth(double h) {
|
|
|
|
_health = h;
|
|
|
|
}
|
|
|
|
void setAbility(double a) {
|
|
|
|
_ability = a;
|
|
|
|
}
|
|
|
|
|
2021-09-19 15:44:31 +03:00
|
|
|
void nextWeapon();
|
|
|
|
void previousWeapon();
|
|
|
|
void fire();
|
|
|
|
void reload();
|
|
|
|
|
|
|
|
[[nodiscard]] double ability() const { return _ability; }
|
|
|
|
|
2021-09-13 15:53:43 +03:00
|
|
|
void setFullHealth() {
|
|
|
|
_health = _healthMax;
|
2021-09-19 11:25:10 +03:00
|
|
|
_fullHealthSound.play();
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
void setFullAbility() {
|
|
|
|
_ability = _abilityMax;
|
2021-09-19 11:25:10 +03:00
|
|
|
_fullAbilitySound.play();
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void setDamagePlayerCallBack(std::function<void(sf::Uint16 targetId, double)> hit) {
|
2021-09-19 11:25:10 +03:00
|
|
|
_damagePlayerCallBack = std::move(hit);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void setAddTraceCallBack(std::function<void(const Point4D&, const Point4D&)> add) {
|
2021-09-19 11:25:10 +03:00
|
|
|
_addTraceCallBack = std::move(add);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void setTakeBonusCallBack(std::function<void(const std::string&)> take) {
|
2021-09-19 11:25:10 +03:00
|
|
|
_takeBonusCallBack = std::move(take);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] double health() const { return _health; }
|
|
|
|
[[nodiscard]] std::string name() const { return "Player_" + _name; }
|
|
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<Camera> camera() { return _camera; }
|
|
|
|
|
|
|
|
// This is for situation when you want to store the position of the head but you dont have attached camera
|
|
|
|
void setHeadAngle(double a) { _headAngle = a; }
|
|
|
|
[[nodiscard]] double headAngle() const { return _headAngle; };
|
|
|
|
|
|
|
|
void rotateWeaponsRelativePoint(const Point4D& point4D, const Point4D& v, double val);
|
|
|
|
|
|
|
|
void drawStats();
|
|
|
|
|
|
|
|
void addKill() { _kills++; }
|
|
|
|
void addDeath() { _deaths++; }
|
|
|
|
|
|
|
|
void playDeath();
|
|
|
|
void playKill();
|
|
|
|
|
2021-09-19 11:25:10 +03:00
|
|
|
void collisionWithObject(const std::string& objName, std::shared_ptr<RigidBody> obj);
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-09-14 13:47:53 +03:00
|
|
|
void addWeapon(std::shared_ptr<Weapon> weapon);
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
void initWeapons();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif //MINECRAFT_3DZAVR_PLAYER_H
|