DataModelクラスはNSCodingを実装したsingletonクラスです。
NSCodingを実装しているのはゲームの現在の状態を保持しておきたいから、
またsingletonにしているのはDataModelクラスはゲーム内に一つしか必要ないからのようです。
どの辺がsingletonかというと、DataModel.mの以下の部分ですね。
+(DataModel*)getModel { if (!_sharedContext) { _sharedContext = [[self alloc] init]; } return _sharedContext; }この辺の詳細を知りたいならGoogle先生に聞いてくださいまし。
まぁ、Objective-cでsingletonの実装をするなら↑のようになると思ってください。
DataModelクラスを他から呼び出す場合は
DataModel *m = [DataModel getModel];となりますね。
また、ここでは主要なものは配列で保持しています。コードを見てみましょう。
#import "cocos2d.h" @interface DataModel : NSObject { CCLayer *_gameLayer; NSMutableArray *_targets; NSMutableArray *_waypoints; NSMutableArray *_waves; UIPanGestureRecognizer *_gestureRecognizer; } @property (nonatomic, retain) CCLayer *_gameLayer; @property (nonatomic, retain) NSMutableArray * _targets; @property (nonatomic, retain) NSMutableArray * _waypoints; @property (nonatomic, retain) NSMutableArray * _waves; @property (nonatomic, retain) UIPanGestureRecognizer *_gestureRecognizer;; + (DataModel*)getModel; @end
変数の説明ですが
target は敵ですね(TDのcreepのことです)。
waypoint はそのままです。詳しくは前回の投稿を見てください。
waves は Waveクラス(敵の数とか登場する間隔とかを定義しているクラス)を保持する為の配列です。
そのほかに、DataModelクラスはUIPanGestureRecognizer型とCCLayer型の変数を持っています。
まずCCLayerですが、タッチ入力と加速度センサー入力を受け取るクラスです。
このチュートリアルでは
現在のゲームレイヤーへのポインタで、全てのアクションがここで実行されている
と説明されてます。
UIPanGestureRecognizerはタッチジェスチャーを検出するためのクラスですが、
ここでは
480×320に制限されたゲームのスクリーンをスムーズにスクロールするために使う
みたいな事書いてました(かなり意訳)。
まぁ、そのへんは後々に。
次に「敵」のクラスを見ていきましょう。敵を表すクラスはCreepクラスです。
#import "cocos2d.h" #import "DataModel.h" #import "WayPoint.h" @interface Creep : CCSprite { int _curHp; int _moveDuration; int _curWaypoint; } @property (nonatomic, assign) int hp; @property (nonatomic, assign) int moveDuration; @property (nonatomic, assign) int curWaypoint; - (Creep *) initWithCreep:(Creep *) copyFrom; - (WayPoint *)getCurrentWaypoint; - (WayPoint *)getNextWaypoint; @end @interface FastRedCreep : Creep { } +(id)creep; @end @interface StrongGreenCreep : Creep { } +(id)creep; @end
Creepクラスは
・HP(HitPoint)
・敵の速さ
・現在のWayPoint
をそれぞれセットできます。
敵はCreepを継承したものを作成します。
ここでは
・FastRedCreep
移動スピードは早いが弱い(HPが少ない)敵(赤色)
・StrongGreenCreep
移動スピードは遅いが強い敵(緑色)
を用意しています。
それではこの敵をどうやって実装していくか実装部分を見ていきましょう。
@implementation FastRedCreep + (id)creep { FastRedCreep *creep = nil; if ((creep = [[[super alloc] initWithFile:@"Enemy1.png"] autorelease])) { creep.hp = 10; creep.moveDuration = 4; creep.curWaypoint = 0; } return creep; }
まず、staticメソッドを用意して、ここで敵の設定を行います。
この敵を呼び出すときは
[FastRedCreep creep]
とすることで、すぐに画面に反映されます。
また、このクラスはCCSpriteを継承しているので、その機能を色々使えるようになってます。
ちなみにCCSpriteはcocos2dで画像を表示するためのクラスです。
背景とかキャラクターとかはこのクラスで表示させます。
とりあえず第2回はここまで。
次回は敵が今どこにいるのかを取得する箇所からやってきます。
いやー、意外に時間かかるなー
0 件のコメント:
コメントを投稿