-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.h
177 lines (149 loc) · 4.7 KB
/
tree.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
165
166
167
168
169
170
171
172
173
174
175
176
177
// -----------------------------------------------------------------
// 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 __TREE_H
#define __TREE_H
#ifndef NULL
#define NULL 0L
#endif
/*
TREE.H
The CNode class
OpenGL Game Programming
Author: Kevin Hawkins
Date: 3/29/2001
Description: The CNode class represents a single node in the
cyclic linked list tree that holds all objects
in the game.
*/
// tree class
class CNode
{
public:
// data
CNode *parentNode; // parent node
CNode *childNode; // child node
CNode *prevNode; // previous node
CNode *nextNode; // next node
bool HasParent() { return (parentNode != NULL); } // does node have a parent?
bool HasChild() { return (childNode != NULL); } // does node have a child?
// is this node the first child?
bool IsFirstChild()
{
if (parentNode)
return (parentNode->childNode == this);
else
return false;
}
// is this node the last child?
bool IsLastChild()
{
if (parentNode)
return (parentNode->childNode->prevNode == this);
else
return false;
}
// attach this node to a parent node
void AttachTo(CNode *newParent)
{
// if this node is already attached to another node, then detach
if (parentNode)
Detach();
parentNode = newParent;
if (parentNode->childNode)
{
prevNode = parentNode->childNode->prevNode;
nextNode = parentNode->childNode;
parentNode->childNode->prevNode->nextNode = this;
parentNode->childNode->prevNode = this;
}
else
{
parentNode->childNode = this; // this is the first child
}
}
// attach a child to this node
void Attach(CNode *newChild)
{
// if the child node is already attached, then detach it
if (newChild->HasParent())
newChild->Detach();
newChild->parentNode = this;
if (childNode)
{
newChild->prevNode = childNode->prevNode;
newChild->nextNode = childNode;
childNode->prevNode->nextNode = newChild;
childNode->prevNode = newChild;
}
else
childNode = newChild;
}
// detach node from parent
void Detach()
{
// if this node is the first child of the parent (first in list)
// then the parent points to the next child in the list
if (parentNode && parentNode->childNode == this)
{
if (nextNode != this)
parentNode->childNode = nextNode;
else
parentNode->childNode = NULL; // no next child
}
// get rid of links
prevNode->nextNode = nextNode;
nextNode->prevNode = prevNode;
// now this node is not in the list
prevNode = this;
nextNode = this;
}
// count the number of nodes
int CountNodes()
{
if (childNode)
return childNode->CountNodes() + 1;
else
return 1;
}
// constructor
CNode() // setup node
{
parentNode = childNode = NULL;
prevNode = nextNode = this;
}
// constructor
CNode(CNode *node)
{
parentNode = childNode = NULL; // setup and attach this node to node
prevNode = nextNode = this;
AttachTo(node);
}
// destructor
virtual ~CNode()
{
Detach(); // detach from hierarchy
while (childNode) // delete all children
{
delete childNode;
}
}
};
#endif