-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGameScene.m
219 lines (146 loc) · 5.31 KB
/
GameScene.m
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//
// GameScene.m
// EG-TD
//
// Created by Gurcan Yavuz on 10/8/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "GameScene.h"
#import "Camera.h"
#import "TowerMenu.h"
@implementation GameScene
@synthesize gameMap;
@synthesize xDifference;
@synthesize yDifference;
@synthesize _location;
@synthesize towerMenu;
@synthesize touched;
@synthesize miniTowerTouched;
- (id)init {
if(self == [super init]) {
// Grab an instance of our singleton classes
_sharedDirector = [Director sharedDirector];
_sharedResourceManager = [ResourceManager sharedResourceManager];
gameMap = [[Map3D alloc] initMap3D];
towerMenu = [[TowerMenu alloc] init];
//NORMAL CAMERA GAME COORDINATE SYSTEM
gameCamera = [[Camera alloc] initWithTileLocation:Vector3fMake(0,30,-40)];
xDifference = 0;
yDifference = 0;
touched = 0;
miniTowerTouched = 0;
miniTower = CGRectMake(240, 0, 80, 80);
}
return self;
}
#pragma mark -
#pragma mark Update scene logic
- (void)updateWithDelta:(GLfloat)theDelta {
[gameCamera update:theDelta];
[towerMenu update:theDelta];
}
#pragma mark -
#pragma mark Touch events
- (void)updateWithTouchLocationBegan:(NSSet*)touches withEvent:(UIEvent*)event view:(UIView*)aView {
UITouch *touch = [[event touchesForView:aView] anyObject];
_location = [touch locationInView:aView];
if (CGRectContainsPoint(miniTower, _location) && touched == 1)
{
NSLog(@"mini tower touched");
miniTowerHash = [touch hash];
miniTowerTouched = 1;
}
CGPoint tt = [self unProject:_location];
// NSLog(@"location X : %f location Y : %f ", tt.x, tt.y);
// NSLog(@"location X : %f location Y : %f ", _location.x, _location.y);
if (_location.y < 30)
{
touched = 1;
}
else if(miniTowerTouched !=1)
{
touched = 0;
}
xDifference = 0;
yDifference = 0;
}
- (void)updateWithTouchLocationMoved:(NSSet*)touches withEvent:(UIEvent*)event view:(UIView*)aView {
UITouch *touch = [[event touchesForView:aView] anyObject];
if ([touch hash] == miniTowerHash)
{
}
if (!touched && miniTowerTouched != 1)
{
// UITouch *touch = [[event touchesForView:aView] anyObject];
CGPoint _nextLocation;
_nextLocation = [touch locationInView:aView];
xDifference = _nextLocation.x-_location.x;
yDifference = _nextLocation.y-_location.y;
}
}
- (void)updateWithTouchLocationEnded:(NSSet*)touches withEvent:(UIEvent*)event view:(UIView*)aView {
// xDifference *= 0.5;
// yDifference *= 0.5;
if(miniTowerTouched == 1 )
{
touched = 0;
miniTowerTouched = 0;
}
}
#define RAY_ITERATIONS 100
- (CGPoint) unProject: (CGPoint) point
{
GLfloat x=0, y=0, z=0;
GLfloat modelMatrix[16];
GLfloat projMatrix[16];
GLint viewport[4];
glGetFloatv(GL_MODELVIEW_MATRIX, modelMatrix);
glGetFloatv(GL_PROJECTION_MATRIX, projMatrix);
glGetIntegerv(GL_VIEWPORT, viewport);
point.y = viewport[3] - point.y;
// NSLog(@"location X : %f location Y : %f ", point.x, point.y);
EGVertex3D far3d;
EGVertex3D near3d;
EGVertex3D rayVector3d;
//Retreiving position projected on near plan
gluUnProject( point.x, point.y , 0, modelMatrix, projMatrix, viewport, &near3d.x, &near3d.y, &near3d.z);
//Retreiving position projected on far plan
gluUnProject( point.x, point.y, 1, modelMatrix, projMatrix, viewport, &far3d.x, &far3d.y, &far3d.z);
rayVector3d.x = (far3d.x - near3d.x);
rayVector3d.y = (far3d.y - near3d.y);
rayVector3d.z = (far3d.z - near3d.z);
// NSLog(@"RAY X : %f location Y : %f location Z : %f", rayVector3d.x, rayVector3d.y,rayVector3d.z);
// rayVector3d.x = near3d.x;
//rayVector3d.y = near3d.y;
//rayVector3d.z = near3d.z;
float rayLength = sqrtf(rayVector3d.x*rayVector3d.x + rayVector3d.y*rayVector3d.y + rayVector3d.z*rayVector3d.z);
NSLog(@"NEAR X : %f location Y : %f location Z : %f", near3d.x, near3d.y,near3d.z);
// NSLog(@"RAY X : %f location Y : %f location Z : %f", far3d.x, far3d.y,far3d.z);
rayVector3d.x /= rayLength;
rayVector3d.y /= rayLength;
rayVector3d.z /= rayLength;
// NSLog(@"RAY X : %f location Y : %f location Z : %f", rayVector3d.x, rayVector3d.y,rayVector3d.z);
CGPoint result = { 0.0f/0.0f, 0.0f/0.0f };
EGVertex3D collisionPoint;
//Iterating over ray vector to check collisions
for(int i = 0; i < RAY_ITERATIONS; i++)
{
// collisionPoint.x = rayVector3d.x * rayLength/RAY_ITERATIONS*i;
// collisionPoint.y = rayVector3d.y * rayLength/RAY_ITERATIONS*i;
// collisionPoint.z = rayVector3d.z * rayLength/RAY_ITERATIONS*i;
collisionPoint.x = near3d.x + rayVector3d.x*i;
collisionPoint.y = near3d.y + rayVector3d.y*i;
collisionPoint.z = near3d.z + rayVector3d.z*i;
//Checking collision
NSLog(@"RAY X : %f location Y : %f location Z : %f", collisionPoint.x, collisionPoint.y,collisionPoint.z);
}
// return result;
}
#pragma mark -
#pragma mark Render scene
- (void)render {
[gameMap render];
[gameCamera render];
[towerMenu render];
}
@end