vectozavr-shooter/engine/Object.h

84 lines
2.9 KiB
C
Raw Normal View History

2021-09-13 15:53:43 +03:00
//
// Created by Иван Ильин on 15.03.2021.
//
#ifndef ENGINE_OBJECT_H
#define ENGINE_OBJECT_H
2021-10-02 20:36:07 +03:00
#include <map>
#include "Vec3D.h"
2021-10-09 13:41:12 +03:00
#include <string>
2021-10-17 10:21:10 +03:00
#include <utility>
2021-10-23 15:02:25 +03:00
#include <memory>
2021-10-29 23:44:37 +03:00
#include "Matrix4x4.h"
#include <SFML/OpenGL.hpp>
2021-10-17 10:21:10 +03:00
2021-10-17 11:41:58 +03:00
class ObjectNameTag final {
2021-10-17 10:21:10 +03:00
private:
const std::string _name;
public:
explicit ObjectNameTag(std::string name = "") : _name(std::move(name)) {}
[[nodiscard]] std::string str() const { return _name; }
bool operator==(const ObjectNameTag& tag) const { return _name == tag._name; }
bool operator!=(const ObjectNameTag& tag) const { return _name != tag._name; }
bool operator<(const ObjectNameTag& tag) const { return _name < tag._name; }
};
2021-09-13 15:53:43 +03:00
class Object {
2021-10-28 16:58:02 +03:00
private:
bool checkIfAttached(Object* obj);
const ObjectNameTag _nameTag;
2021-10-29 23:44:37 +03:00
Matrix4x4 _transformMatrix = Matrix4x4::Identity();
2021-10-02 20:36:07 +03:00
2021-10-28 16:58:02 +03:00
std::map<ObjectNameTag, std::weak_ptr<Object>> _attachedObjects;
2021-09-13 15:53:43 +03:00
2021-10-30 11:18:31 +03:00
Vec3D _position {0, 0, 0};
2021-10-28 16:58:02 +03:00
Vec3D _angle {0, 0, 0};
Vec3D _angleLeftUpLookAt{0, 0, 0};
2021-09-13 15:53:43 +03:00
public:
2021-10-29 23:44:37 +03:00
explicit Object(ObjectNameTag nameTag) : _nameTag(std::move(nameTag)) {};
2021-10-30 12:13:22 +03:00
Object(const Object& object) : _nameTag(object.name()), _transformMatrix(object.model()) {};
2021-10-29 23:44:37 +03:00
2021-10-30 11:18:31 +03:00
// TODO: add transform(const Matrix4x4& t)
// TODO: add transformRelativePoint(const Vec3D &point, const Matrix4x4& transform)
// TODO: implement every transform method using transform() & transformRelativePoint()
// TODO: implement rotations using quaternions
2021-10-29 23:44:37 +03:00
void translate(const Vec3D& dv);
void translateToPoint(const Vec3D& point);
void scale(const Vec3D& s);
void rotate(const Vec3D& r);
void rotate(const Vec3D& v, double rv);
void rotateToAngle(const Vec3D& v);
void rotateRelativePoint(const Vec3D& s, const Vec3D& r);
void rotateRelativePoint(const Vec3D& s, const Vec3D& v, double r);
void rotateLeft(double rl);
void rotateUp(double ru);
void rotateLookAt(double rlAt);
2021-10-29 23:44:37 +03:00
[[nodiscard]] Vec3D left() const { return _transformMatrix.x(); }
[[nodiscard]] Vec3D up() const { return _transformMatrix.y(); }
[[nodiscard]] Vec3D lookAt() const { return _transformMatrix.z(); }
2021-10-30 11:18:31 +03:00
[[nodiscard]] Vec3D position() const { return _position; }
2021-10-29 23:44:37 +03:00
2021-10-28 16:58:02 +03:00
[[nodiscard]] Vec3D angle() const { return _angle; }
[[nodiscard]] Vec3D angleLeftUpLookAt() const { return _angleLeftUpLookAt; }
2021-10-28 16:58:02 +03:00
void attach(std::shared_ptr<Object> object);
2021-10-17 10:21:10 +03:00
void unattach(const ObjectNameTag& tag);
std::shared_ptr<Object> attached(const ObjectNameTag& tag);
2021-10-16 20:22:55 +03:00
2021-10-29 23:44:37 +03:00
[[nodiscard]] ObjectNameTag name() const { return _nameTag; }
2021-10-30 12:13:22 +03:00
[[nodiscard]] Matrix4x4 model() const { return Matrix4x4::Translation(_position) * _transformMatrix; }
2021-10-29 23:44:37 +03:00
// OpenGL function
[[nodiscard]] GLfloat* glModel() const;
[[nodiscard]] GLfloat* glView() const;
2021-10-28 16:58:02 +03:00
2021-10-16 20:22:55 +03:00
virtual ~Object();
2021-10-02 20:36:07 +03:00
};
2021-09-13 15:53:43 +03:00
#endif //MINECRAFT_3DZAVR_OBJECT_H