-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject.h
164 lines (131 loc) · 4.88 KB
/
object.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// -----------------------------------------------------------------
// Learning Team B
// Members:
// Adam LeMmon
// Faith Satterthwaite
// Tom Fletcher
// Justin Ball
// CS 4280 – 11:30 am
// Final Project
// Dr. Rague
// Due: 12/06/12
// Version: 2.4
// -----------------------------------------------------------------
// We made five major improvements to this game
// 1) New controls
// 2) Enemy attack
// 3) HUD (heads up display)
// 4) Enemy health bars
// 5) New Weapon
// -----------------------------------------------------------------
#ifndef __OBJECT_H
#define __OBJECT_H
#include "tree.h"
#include "vector.h"
#include "camera.h"
/*
OBJECT.H
The CObject class
OpenGL Game Programming
Author: Kevin Hawkins
Date: 3/29/2001
Description: The CObject class is derived from CNode and
represents an object in the game world.
*/
class CObject : public CNode
{
protected:
// perform basic physics on the object
virtual void OnAnimate(scalar_t deltaTime)
{
position += velocity * deltaTime;
velocity += acceleration * deltaTime;
}
// draw the object given the camera position
virtual void OnDraw(CCamera *camera) {}
// collide with other objects
virtual void OnCollision(CObject *collisionObject) {}
// perform collision and other preparations for this object
virtual void OnPrepare()
{
ProcessCollisions(FindRoot()); // perform collisions starting with
// root world object
}
public:
CVector position; // object position
CVector velocity; // velocity of object
CVector acceleration; // acceleration
scalar_t size; // size of bounding sphere (radius)
bool isDead;
CObject() { isDead = false; }
~CObject() {}
virtual void Load() {}
virtual void Unload() {}
// draw object
void Draw(CCamera *camera)
{
// push modelview matrix on stack
glPushMatrix();
OnDraw(camera); // draw this object
if (HasChild()) // draw children
((CObject*)childNode)->Draw(camera);
glPopMatrix();
// draw siblings
if (HasParent() && !IsLastChild())
((CObject*)nextNode)->Draw(camera);
}
// animate object
void Animate(scalar_t deltaTime)
{
OnAnimate(deltaTime); // animate this object
// animate children
if (HasChild())
((CObject*)childNode)->Animate(deltaTime);
// animate siblings
if (HasParent() && !IsLastChild())
((CObject*)nextNode)->Animate(deltaTime);
if (isDead)
delete this;
}
// perform collision detection
void ProcessCollisions(CObject *obj)
{
// if this object's bounding sphere collides with obj's sphere
// and obj is not this object
if (((obj->position - position).Length() <= (obj->size + size)) &&
(obj != ((CObject*)this)))
{
OnCollision(obj); // perform this object's collision with obj
// test child collisions with obj
if (HasChild())
((CObject*)childNode)->ProcessCollisions(obj);
// test sibling collisions with obj
if (HasParent() && !IsLastChild())
((CObject*)nextNode)->ProcessCollisions(obj);
}
// if obj has children, check collisions with these children
if (obj->HasChild())
ProcessCollisions((CObject*)(obj->childNode));
// if obj has siblings, check collisions with these siblings
if (obj->HasParent() && !obj->IsLastChild())
ProcessCollisions((CObject*)(obj->nextNode));
}
// prepare object
void Prepare()
{
OnPrepare(); // prepare this object
if (HasChild()) // prepare children
((CObject*)childNode)->Prepare();
if (HasParent() && !IsLastChild()) // prepare siblings
((CObject*)nextNode)->Prepare();
}
// find root object of cyclic linked list tree
CObject *FindRoot()
{
// if this object has a parent node, return the root of the parent node
if (parentNode)
return ((CObject*)parentNode)->FindRoot();
return this;
}
};
#endif