Skip to content

Commit

Permalink
Initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
akisute committed Aug 29, 2009
0 parents commit 14b9efd
Show file tree
Hide file tree
Showing 319 changed files with 65,914 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# osx noise files
.DS_Store
profile

# xcode noise files
build/*
*.pbxuser
*.mode1v3
*.perspectivev3
26 changes: 26 additions & 0 deletions Classes/GameOverScene.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// GameOverScene.h
// MyShooting
//
// Created by Masashi Ono on 09/08/26.
// Copyright (c) 2009, Masashi Ono
// All rights reserved.
//

#import <UIKit/UIKit.h>
#import "cocos2d.h"


@interface GameOverScene : Scene {
}

@end

#pragma mark -

@interface GameOverSceneLayer : Layer {
}

- (void)backToTitle:(id)sender;

@end
53 changes: 53 additions & 0 deletions Classes/GameOverScene.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// GameOverScene.m
// MyShooting
//
// Created by Masashi Ono on 09/08/26.
// Copyright (c) 2009, Masashi Ono
// All rights reserved.
//

#import "GameOverScene.h"
#import "MainScene.h"


@implementation GameOverScene

- (id)init
{
if(self = [super init])
{
[self addChild:[GameOverSceneLayer node] z:1];
}
return self;
}

@end

#pragma mark -

@implementation GameOverSceneLayer

- (id)init
{
if(self = [super init])
{
[MenuItemFont setFontSize:20];
[MenuItemFont setFontName:@"Helvetica"];
MenuItem *gameOver = [MenuItemFont itemFromString:@"Game Over"
target:self
selector:@selector(backToTitle:)];
Menu *menu = [Menu menuWithItems:gameOver, nil];
[menu alignItemsVerticallyWithPadding:24];
[self addChild:menu];
}
return self;
}

- (void)backToTitle:(id)sender
{
MainScene *scene = [MainScene node];
[[Director sharedDirector] replaceScene:scene];
}

@end
33 changes: 33 additions & 0 deletions Classes/GameScene.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// GameScene.h
// MyShooting
//
// Created by Masashi Ono on 09/08/24.
// Copyright (c) 2009, Masashi Ono
// All rights reserved.
//

#import <UIKit/UIKit.h>
#import "cocos2d.h"


@class MSTGObject;

@interface GameScene : Scene {

}

@end

#pragma mark -

@interface GameSceneLayer : Layer {
int spawnFrames;
int spawnInterval;
}

- (void)mainRoop:(ccTime)dt;

@property (nonatomic, assign) MSTGObject *player;

@end
162 changes: 162 additions & 0 deletions Classes/GameScene.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
//
// GameScene.m
// MyShooting
//
// Created by Masashi Ono on 09/08/24.
// Copyright (c) 2009, Masashi Ono
// All rights reserved.
//

#import "GameScene.h"
#import "MainScene.h"
#import "GameOverScene.h"
#import "MSTGActionFactory.h"
#import "MSTGObjectFactory.h"
#import "MSTGObject.h"


@implementation GameScene

- (id)init
{
if(self = [super init])
{
// Add background image and layer
CGSize size = [[Director sharedDirector] winSize];
Sprite *backgroundImage = [Sprite spriteWithFile:@"background.png"];
backgroundImage.position = ccp(size.width/2, size.height/2);
[self addChild:backgroundImage z:0];
[self addChild:[GameSceneLayer node] z:1];
}
return self;
}

@end

#pragma mark -

@implementation GameSceneLayer

- (id)init
{
if(self = [super init])
{
// Enable touch and accelerometer delegates
self.isTouchEnabled = YES;
self.isAccelerometerEnabled = YES;
// Schedule main roop
[self schedule:@selector(mainRoop:)];

spawnFrames = 0;
spawnInterval = 50;

CGSize size = [[Director sharedDirector] winSize];

// Place a ship of player with tag
MSTGObject *ship = [MSTGObjectFactory ship];
ship.position = ccp(size.width/2, 32);
self.player = ship;
}
return self;
}

#pragma mark Main roop

- (void)mainRoop:(ccTime)dt
{
CGSize size = [[Director sharedDirector] winSize];

// If player is dead, game over
if (!self.player)
{
[self unschedule:@selector(mainRoop:)];
GameOverScene *scene = [GameOverScene node];
TransitionScene *transition = [FadeTransition transitionWithDuration:2.0
scene:scene
withColor:ccRED];
[[Director sharedDirector] replaceScene:transition];
return;
}

// Create new enemy per some frames
if (spawnFrames++ > spawnInterval)
{
float positionX = (signed)(arc4random() % (int)(size.width - 20)) + 10;
MSTGObject *cannon = [MSTGObjectFactory cannon];
cannon.position = ccp(positionX, size.height - 10);
Action *action = [MSTGActionFactory moveByFrom:cannon.position
degree:-90
speed:15];
[cannon runAction:action];
[self addChild:cannon];
spawnFrames = 0;
}

// Fire some bullets
// MSTGObject *bullet = [MSTGObjectFactory bullet];
// bullet.position = ccp(size.width/2, size.height - 10);
// float delta = (signed)(arc4random() % 51) - 25;
// Action *action = [MSTGActionFactory moveByFrom:bullet.position
// degree:-90+delta
// speed:150.0];
// [bullet runAction:action];
// [self addChild:bullet];

}

#pragma mark Properties

- (MSTGObject *)player
{
// TODO getChildByTag is relativly slow, use alternative way
return (MSTGObject *)[self getChildByTag:99999];
}
- (void)setPlayer:(MSTGObject *)aPlayer
{
[self addChild:aPlayer z:0 tag:99999];
}

#pragma mark Event handling
// Using StandardTouchDelegate methods.
// There is also TargetedTouchDelegate which can handle specific single touches easily.

- (BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// TEMPORALLY: Back to main scene
MainScene *scene = [MainScene node];
[[Director sharedDirector] replaceScene:scene];
return kEventHandled;
}

- (BOOL)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
return kEventIgnored;
}

- (BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
return kEventIgnored;
}

- (BOOL)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
return kEventIgnored;
}

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
// Controll player with acceleration
MSTGObject *ship = (MSTGObject *)[self getChildByTag:99999];
Action *action = [MSTGActionFactory moveByFrom:ship.position acceleration:acceleration];
if (action)
{
[ship runAction:action];
}
else
{
// TODO: just stop movement actions, not graphical actions
[ship stopAllActions];
}
}

@end
26 changes: 26 additions & 0 deletions Classes/MSTGActionFactory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// MSTGActionFactory.h
// MyShooting
//
// Created by Masashi Ono on 09/08/24.
// Copyright (c) 2009, Masashi Ono
// All rights reserved.
//

#import <UIKit/UIKit.h>
#import "cocos2d.h"


#define ACTION_DURATION 10000.0f

@interface MSTGActionFactory : NSObject {

}

+ (Action *)moveByFrom:(CGPoint)from to:(CGPoint)to inSecond:(ccTime)second;
+ (Action *)moveByFrom:(CGPoint)from to:(CGPoint)to speed:(float)speed;
+ (Action *)moveByFrom:(CGPoint)from to:(CGPoint)to degree:(float)degree speed:(float)speed;
+ (Action *)moveByFrom:(CGPoint)from degree:(float)degree speed:(float)speed;
+ (Action *)moveByFrom:(CGPoint)from acceleration:(UIAcceleration *)acceleration;

@end
77 changes: 77 additions & 0 deletions Classes/MSTGActionFactory.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//
// MSTGActionFactory.m
// MyShooting
//
// Created by Masashi Ono on 09/08/24.
// Copyright (c) 2009, Masashi Ono
// All rights reserved.
//

#import "MSTGActionFactory.h"


@implementation MSTGActionFactory

+ (Action *)moveByFrom:(CGPoint)from to:(CGPoint)to inSecond:(ccTime)second
{
CGPoint targetPosition = ccp((to.x-from.x)*(ACTION_DURATION/second), (to.y-from.y)*(ACTION_DURATION/second));
Action *action = [MoveBy actionWithDuration:ACTION_DURATION
position:targetPosition];
return action;
}

+ (Action *)moveByFrom:(CGPoint)from to:(CGPoint)to speed:(float)speed
{
float dx = speed * (to.x - from.x) * ACTION_DURATION;
float dy = speed * (to.y - from.y) * ACTION_DURATION;
CGPoint targetPosition = ccp(dx, dy);
Action *action = [MoveBy actionWithDuration:ACTION_DURATION
position:targetPosition];
return action;
}

+ (Action *)moveByFrom:(CGPoint)from to:(CGPoint)to degree:(float)degree speed:(float)speed
{
float degreeTo = ccpToAngle(ccp(to.x - from.x, to.y - from.y));
return [MSTGActionFactory moveByFrom:from
degree:degreeTo + degree
speed:speed];
}

+ (Action *)moveByFrom:(CGPoint)from degree:(float)degree speed:(float)speed
{
float dx = speed * cosf(CC_DEGREES_TO_RADIANS(degree)) * ACTION_DURATION;
float dy = speed * sinf(CC_DEGREES_TO_RADIANS(degree)) * ACTION_DURATION;
CGPoint targetPosition = ccp(dx, dy);
Action *action = [MoveBy actionWithDuration:ACTION_DURATION
position:targetPosition];
return action;
}

+ (Action *)moveByFrom:(CGPoint)from acceleration:(UIAcceleration *)acceleration
{
CGPoint targetPosition = ccp(from.x, from.y);
if (acceleration.x > 0.2)
{
targetPosition.x += 100 * ACTION_DURATION;
}
else if(acceleration.x < -0.2)
{
targetPosition.x -= 100 * ACTION_DURATION;
}

if (acceleration.y > 0.15)
{
targetPosition.y += 100 * ACTION_DURATION;
}
else if(acceleration.y < -0.15)
{
targetPosition.y -= 100 * ACTION_DURATION;
}

Action *action = [MoveBy actionWithDuration:ACTION_DURATION
position:targetPosition];
return action;
}

@end
Loading

0 comments on commit 14b9efd

Please sign in to comment.