Skip to content

Commit

Permalink
Hero moves after killing an enemy and label for lives
Browse files Browse the repository at this point in the history
  • Loading branch information
qpetty committed Dec 9, 2014
1 parent 238c42d commit e6fb4fd
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Ronin-Prefix.pch
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@
#define RANDOM_NUMBER_0_TO_1 ((float)(rand() % 1001) / 1000.0)
#define RANDOM_NUMBER_NEG1_TO_1 ((float)(rand() % 2001) / 1000.0 - 1.0)

#define PRINT_VEC3(vec) NSLog(@"(vec): (%f, %f, %f)", (vec).x, (vec).y, (vec).z);
#define PRINT_VEC4(vec) NSLog(@"vec: (%f, %f, %f, %f)", (vec).x, (vec).y, (vec).z, (vec).w);

#endif
13 changes: 11 additions & 2 deletions Ronin/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="FEU-vK-F1z">
<rect key="frame" x="16" y="23" width="42" height="21"/>
<rect key="frame" x="16" y="28" width="42" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
Expand All @@ -32,17 +32,26 @@
<action selector="startGame:" destination="BV1-FR-VrT" eventType="touchUpInside" id="bMV-uq-bfE"/>
</connections>
</button>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="3qg-pX-5Y2">
<rect key="frame" x="542" y="28" width="42" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<constraints>
<constraint firstAttribute="centerX" secondItem="bTL-hH-RMY" secondAttribute="centerX" id="1SQ-Ip-GHJ"/>
<constraint firstAttribute="centerY" secondItem="bTL-hH-RMY" secondAttribute="centerY" id="RRR-08-crP"/>
<constraint firstItem="FEU-vK-F1z" firstAttribute="top" secondItem="8aa-yV-Osq" secondAttribute="bottom" constant="3" id="WLT-VX-vuO"/>
<constraint firstItem="FEU-vK-F1z" firstAttribute="top" secondItem="8aa-yV-Osq" secondAttribute="bottom" constant="8" id="WLT-VX-vuO"/>
<constraint firstItem="FEU-vK-F1z" firstAttribute="leading" secondItem="3se-qz-xqx" secondAttribute="leadingMargin" id="buY-rY-iAw"/>
<constraint firstItem="3qg-pX-5Y2" firstAttribute="top" secondItem="8aa-yV-Osq" secondAttribute="bottom" constant="8" id="nu4-wy-7pb"/>
<constraint firstItem="3qg-pX-5Y2" firstAttribute="trailing" secondItem="3se-qz-xqx" secondAttribute="trailingMargin" id="t8X-VU-qx6"/>
</constraints>
</view>
<connections>
<outlet property="highScore" destination="FEU-vK-F1z" id="BPU-eI-rWB"/>
<outlet property="lifeDisplay" destination="3qg-pX-5Y2" id="F88-Hc-3CC"/>
<outlet property="startButton" destination="bTL-hH-RMY" id="yNF-Z6-H8a"/>
</connections>
</viewController>
Expand Down
2 changes: 2 additions & 0 deletions Ronin/GameViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
@interface GameViewController : GLKViewController

@property (weak) IBOutlet UILabel *highScore;
@property (weak) IBOutlet UILabel *lifeDisplay;

@property (weak) IBOutlet UIButton *startButton;

@end
14 changes: 14 additions & 0 deletions Ronin/GameViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ - (void)tearDownGL

-(void)updateHUD {
self.highScore.text = [NSString stringWithFormat:@"Score: %lu", enemiesKilled];
if (hero.health < 0) { hero.health = 0; }
self.lifeDisplay.text = [NSString stringWithFormat:@"Lives: %lu", hero.health];
}

#pragma mark - GLKView and GLKViewController delegate methods
Expand All @@ -239,10 +241,21 @@ - (void)update
float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height);
_projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 0.1f, 100.0f);

[hero update];

GLKVector3 worldMove = GLKVector3Subtract(GLKVector3Make(0.0f, 0.0f, 0.0f), hero.location);
worldMove = GLKVector3DivideScalar(worldMove, 80.0);
worldMove.z = 0;
hero.location = GLKVector3Add(hero.location, worldMove);
hero.destination = GLKVector3Add(hero.destination, worldMove);

for (Enemy *en in allEnemies) {
[en update];
en.location = GLKVector3Add(en.location, worldMove);
}

[self updateHUD];

if (hero.health <= 0) {
[self gameOver];
}
Expand Down Expand Up @@ -321,6 +334,7 @@ - (void)userInteractionEvent:(UIGestureRecognizer*)sender {

if ([oneEnemy hitEnemyAt:hitPoint] == YES) {
enemiesKilled++;
[hero killedCharacter:oneEnemy];
[self updateHUD];
}
}
Expand Down
5 changes: 5 additions & 0 deletions Ronin/Hero.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@

@interface Hero : Character

@property GLKVector3 destination;
@property GLKVector3 movementInterval;

-(void)killedCharacter:(Character*)enemy;

@end
19 changes: 19 additions & 0 deletions Ronin/Hero.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,23 @@ -(instancetype)init {
return self;
}

-(void)update {
self.location = GLKVector3Add(self.location, self.movementInterval);

if (GLKVector3Length(GLKVector3Subtract(self.destination, self.location)) < 0.01) {
self.movementInterval = GLKVector3Make(0.0f, 0.0f, 0.0f);
}
}

-(void)killedCharacter:(Character*)enemy {
float scale = 40.0;
GLKVector3 dir = GLKVector3Subtract(enemy.location, self.location);
dir = GLKVector3DivideScalar(dir, scale);
dir.z = 0.0;
self.movementInterval = dir;
PRINT_VEC3(self.movementInterval);

self.destination = enemy.location;
}

@end

0 comments on commit e6fb4fd

Please sign in to comment.