1 /******************************************************************************
2 * irrlamb - https://github.com/jazztickets/irrlamb
3 * Copyright (C) 2019  Alan Witkowski
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 *******************************************************************************/
18 #pragma once
19 #include <ode/common.h>
20 #include <ode/objects.h>
21 #include <irrTypes.h>
22 #include <vector3d.h>
23 #include <ISceneNode.h>
24 #include <string>
25 #include <glm/vec3.hpp>
26 #include <glm/gtc/quaternion.hpp>
27 
28 // Forward Declarations
29 class _AudioSource;
30 struct _ObjectSpawn;
31 struct _ConstraintSpawn;
32 struct _Template;
33 struct _ObjectCollision;
34 
35 // Classes
36 class _Object {
37 
38 	public:
39 
40 		enum ObjectType {
41 			NONE,
42 			PLAYER,
43 			ORB,
44 			COLLISION,
45 			PLANE,
46 			SPHERE,
47 			BOX,
48 			CYLINDER,
49 			TERRAIN,
50 			CONSTRAINT_FIXED,
51 			CONSTRAINT_HINGE,
52 			CONSTRAINT_D6,
53 			ZONE,
54 		};
55 
56 		_Object(const _Template *Template);
57 		virtual ~_Object();
58 
59 		void PrintOrientation();
60 
61 		// Updates
62 		virtual void Update(float FrameTime);
63 		void BeginFrame();
64 		virtual void EndFrame();
65 		void InterpolateOrientation(float BlendFactor);
66 
67 		// Replays
68 		virtual void UpdateReplay(float FrameTime);
ReadyForReplayUpdate()69 		bool ReadyForReplayUpdate() const { return NeedsReplayPacket; }
WroteReplayPacket()70 		void WroteReplayPacket() { NeedsReplayPacket = false; }
UpdateAudio(const glm::vec3 & Position,float Speed)71 		virtual void UpdateAudio(const glm::vec3 &Position, float Speed) { }
72 
73 		// Object properties
SetID(int Value)74 		void SetID(int Value) { ID = Value; }
SetDeleted(bool Value)75 		void SetDeleted(bool Value) { Deleted = Value; }
SetLifetime(float Value)76 		void SetLifetime(float Value) { Lifetime = Timer + Value; }
77 		void SetSleep(int State);
78 
GetName()79 		std::string GetName() const { return Name; }
GetDeleted()80 		bool GetDeleted() const { return Deleted; }
GetLifetime()81 		float GetLifetime() const { return Lifetime; }
GetType()82 		int GetType() const { return Type; }
GetID()83 		const uint16_t &GetID() const { return ID; }
GetTemplate()84 		const _Template *GetTemplate() const { return Template; }
85 
86 		// Rigid body
87 		void Stop();
88 
89 		virtual void SetPosition(const glm::vec3 &Position);
90 		virtual void SetPositionFromReplay(const irr::core::vector3df &Position);
91 		virtual glm::vec3 GetPosition() const;
GetDrawPosition()92 		const glm::vec3 &GetDrawPosition() const { return DrawPosition; }
93 
94 		virtual void SetQuaternion(const glm::quat &Quaternion);
95 		virtual glm::quat GetQuaternion() const;
96 
SetLinearVelocity(const glm::vec3 & Velocity)97 		void SetLinearVelocity(const glm::vec3 &Velocity) { dBodyEnable(Body); dBodySetLinearVel(Body, Velocity[0], Velocity[1], Velocity[2]); }
98 		glm::vec3 GetLinearVelocity() const;
99 
SetAngularVelocity(const glm::vec3 & Velocity)100 		void SetAngularVelocity(const glm::vec3 &Velocity) { dBodyEnable(Body); dBodySetAngularVel(Body, Velocity[0], Velocity[1], Velocity[2]); }
101 		glm::vec3 GetAngularVelocity() const;
102 
103 		void SetScale(const glm::vec3 &Scale);
SetShape(const glm::vec3 & Shape)104 		virtual void SetShape(const glm::vec3 &Shape) { }
105 
GetNode()106 		irr::scene::ISceneNode *GetNode() { return Node; }
GetBody()107 		dBodyID GetBody() { return Body; }
108 
109 		virtual void HandleCollision(const _ObjectCollision &ObjectCollision);
IsTouchingGround()110 		bool IsTouchingGround() const { return TouchingGroundTimer > 0.0f; }
111 
112 	protected:
113 
114 		// Physics
115 		irr::scene::ISceneNode *LoadMesh(const std::string &MeshFile);
116 		void CreateRigidBody(const _ObjectSpawn &Object, dGeomID Geometry, bool SetTransform=true);
117 		void SetProperties(const _ObjectSpawn &Object, bool SetTransform=true);
118 		void SetProperties(const _ConstraintSpawn &Object);
119 
120 		// Attributes
121 		std::string Name;
122 		const _Template *Template;
123 		int Type;
124 		uint16_t ID;
125 
126 		// State
127 		bool Deleted;
128 
129 		// Life
130 		float Timer, Lifetime;
131 
132 		// Physics and graphics
133 		irr::scene::ISceneNode *Node;
134 		glm::vec3 LastPosition;
135 		glm::quat LastRotation;
136 		glm::vec3 DrawPosition;
137 		dBodyID Body;
138 		dGeomID Geometry;
139 
140 		// Replays
141 		bool NeedsReplayPacket;
142 
143 		// Collision
144 		std::string CollisionCallback;
145 		float TouchingGroundTimer;
146 		bool TouchingGround;
147 
148 };
149