539
|
1 #include <math.h>
|
|
2 #include "SceneGraphRoot.h"
|
|
3 #include "vacuum.h"
|
|
4 #include "SGList.h"
|
|
5 #define SELECT 2
|
|
6
|
|
7 void
|
|
8 cube_collision(SceneGraphPtr node, int screen_w, int screen_h,
|
|
9 SceneGraphPtr tree)
|
|
10 {
|
|
11 if (node->frame > 120) {
|
|
12 cube_split(node,tree);
|
|
13 }
|
|
14 }
|
|
15
|
|
16 void
|
|
17 cube_move_left(SceneGraphPtr node, int screen_w, int screen_h)
|
|
18 {
|
|
19 node->xyz[0] -= node->stack_xyz[0];
|
|
20 node->xyz[1] += node->stack_xyz[1];
|
|
21
|
|
22 if (node->xyz[0] < 0) {
|
|
23 node->set_move_collision(cube_move_right, cube_collision);
|
|
24 }
|
|
25
|
|
26 if (node->xyz[1] < 0 || node->xyz[1] > screen_h) {
|
|
27 node->stack_xyz[1] = -node->stack_xyz[1];
|
|
28 }
|
|
29 }
|
|
30
|
|
31 void
|
|
32 cube_rotate(SceneGraphPtr node, int w, int h)
|
|
33 {
|
|
34 node->angle[0] += 2.0f;
|
|
35 node->angle[1] += 2.0f;
|
|
36 node->angle[2] += 2.0f;
|
|
37 }
|
|
38
|
|
39 void
|
|
40 cube_move_right(SceneGraphPtr node, int screen_w, int screen_h)
|
|
41 {
|
|
42 node->xyz[0] += node->stack_xyz[0];
|
|
43 node->xyz[1] += node->stack_xyz[1];
|
|
44
|
|
45 if (node->xyz[0] > screen_w) {
|
|
46 node->set_move_collision(cube_move_left, cube_collision);
|
|
47 }
|
|
48
|
|
49 if (node->xyz[1] < 0 || node->xyz[1] > screen_h) {
|
|
50 node->stack_xyz[1] = -node->stack_xyz[1];
|
|
51 }
|
|
52
|
|
53 }
|
|
54
|
|
55
|
|
56 void
|
|
57 cube_split(SceneGraphPtr root,SceneGraphPtr tree)
|
|
58 {
|
|
59
|
|
60 SceneGraphPtr p;
|
|
61 // SceneGraphPtr common_move = sgroot->createSceneGraph();
|
|
62 // SceneGraphPtr root_common_move = root->parent;
|
|
63
|
|
64 if(random()%SELECT) {
|
|
65 p = sgroot->createSceneGraph(REDCUBE);
|
|
66 }
|
|
67 else {
|
|
68 p = sgroot->createSceneGraph(ENEMY);
|
|
69 }
|
|
70
|
|
71 root->set_move_collision(cube_move_right, cube_collision);
|
|
72 p->set_move_collision(cube_move_left, cube_collision);
|
|
73
|
|
74 root->frame = 0;
|
|
75 p->frame = 0;
|
|
76
|
|
77 p->xyz[0] = root->xyz[0] + 2;
|
|
78 p->xyz[1] = root->xyz[1];
|
|
79 p->xyz[2] = root->xyz[2];
|
|
80
|
|
81 p->stack_xyz[0] = 2.0f;
|
|
82 p->stack_xyz[1] = random()%3-1;
|
|
83 p->stack_xyz[2] = 0.0f;
|
|
84
|
|
85 root->xyz[0] -= 2;
|
|
86 root->stack_xyz[0] = 2.0f;
|
|
87 root->stack_xyz[1] = random()%3-1;
|
|
88
|
|
89 //common_move->addChild(p);
|
|
90 root->addBrother(p);
|
|
91
|
|
92 }
|
|
93
|
|
94
|
|
95 void
|
|
96 collision_red(SceneGraphIteratorPtr it,SceneGraphPtr node)
|
|
97 {
|
|
98 float dx, dy,ddx,ddy, r;
|
|
99 float q = 0;
|
|
100
|
|
101 for (; it->hasNext(REDCUBE);) {
|
|
102
|
|
103 it->next(REDCUBE);
|
|
104 SceneGraphPtr mcube = it->get();
|
|
105 dx = node->xyz[0] - mcube->xyz[0];
|
|
106 dy = node->xyz[1] - mcube->xyz[1];
|
|
107
|
|
108 ddx = dx*dx;
|
|
109 ddy = dy*dy;
|
|
110
|
|
111 if(sqrt(ddx) < 10 && sqrt(ddy) < 10) {
|
|
112 mcube->remove();
|
|
113 continue;
|
|
114 }
|
|
115 r = sqrt(ddx + ddy);
|
|
116 if (r >= 1) q = 200/r;
|
|
117 if (dx == 0) {
|
|
118 if(mcube->xyz[1] > node->xyz[1]) {
|
|
119 mcube->stack_xyz[1] -= q;
|
|
120 } else if(mcube->xyz[1] < node->xyz[1]) {
|
|
121 mcube->stack_xyz[1] += q;
|
|
122 }
|
|
123 } else {
|
|
124 if(mcube->xyz[0] > node->xyz[0]) {
|
|
125 mcube->xyz[0] -= q*cos(atan(dy/dx));
|
|
126 mcube->xyz[1] -= q*sin(atan(dy/dx));
|
|
127 } else if(mcube->xyz[0] < node->xyz[0]) {
|
|
128 mcube->xyz[0] += q*cos(atan(dy/dx));
|
|
129 mcube->xyz[1] += q*sin(atan(dy/dx));
|
|
130 }
|
|
131 }
|
|
132 }
|
|
133 }
|
|
134
|
|
135 void
|
|
136 collision_purple(SceneGraphIteratorPtr it,SceneGraphPtr node,int w,int h)
|
|
137 {
|
|
138 float dx, dy,ddx,ddy, r;
|
|
139 float q = 0;
|
|
140
|
|
141 for (; it->hasNext(ENEMY);) {
|
|
142 it->next(ENEMY);
|
|
143 SceneGraphPtr mcube = it->get();
|
|
144
|
|
145 dx = node->xyz[0] - mcube->xyz[0];
|
|
146 dy = node->xyz[1] - mcube->xyz[1];
|
|
147 ddx = dx*dx;
|
|
148 ddy = dy*dy;
|
|
149
|
|
150 if(sqrt(ddx) < 10 && sqrt(ddy) < 10) {
|
|
151 gameover_scene(w,h,mcube);
|
|
152 node->remove();
|
|
153 break;
|
|
154 }
|
|
155 r = sqrt(ddx + ddy);
|
|
156 if (r >= 1) q = 200/r;
|
|
157 if (dx == 0) {
|
|
158 if(mcube->xyz[1] > node->xyz[1]) {
|
|
159 mcube->stack_xyz[1] -= q;
|
|
160 } else if(mcube->xyz[1] < node->xyz[1]) {
|
|
161 mcube->stack_xyz[1] += q;
|
|
162 }
|
|
163 } else {
|
|
164
|
|
165 if(mcube->xyz[0] > node->xyz[0]) {
|
|
166 mcube->xyz[0] -= q*cos(atan(dy/dx));
|
|
167 mcube->xyz[1] -= q*sin(atan(dy/dx));
|
|
168 } else if(mcube->xyz[0] < node->xyz[0]) {
|
|
169 mcube->xyz[0] += q*cos(atan(dy/dx));
|
|
170 mcube->xyz[1] += q*sin(atan(dy/dx));
|
|
171 }
|
|
172 }
|
|
173 }
|
|
174 }
|