Skip to content

Instantly share code, notes, and snippets.

@kexoth
Created May 4, 2013 04:53
Show Gist options
  • Save kexoth/5516246 to your computer and use it in GitHub Desktop.
Save kexoth/5516246 to your computer and use it in GitHub Desktop.
var audioEngine = cc.AudioEngine.getInstance();
var MainLayer = cc.LayerColor.extend({
_enemies: [],
_projectiles: [],
_enemiesDestroyed: 0,
_timer: 0,
_pauseButton: null,
_gamePoints: null,
ctor:function(){
this._super();
cc.associateWithNative(this, cc.LayerColor);
// this.init(cc.c4b(0, 125, 0, 255), winSize.width, winSize.height);
},
onEnter:function(){
this._super();
if( 'touches' in sys.capabilities ) {
this.setTouchEnabled(true);
}
if( 'mouse' in sys.capabilities ) {
this.setMouseEnabled(true);
}
var background = cc.Sprite.create(s_background);
background.scaleX = winSize.width / background.getContentSize().width * 2;
background.scaleY = winSize.height / background.getContentSize().height * 2;
background.setContentSize(cc.size(winSize.width * 2, winSize.height * 2));
background.setPosition(cc.p(background.getPosition().x + winSize.width, 0));
this.addChild(background);
cc.log("Background Box Initial | X:" + background.getBoundingBox().origin.x + " Y:" + background.getBoundingBox().origin.y + " WIDTH:" + background.getBoundingBox().size.width + " HEIGHT:" + background.getBoundingBox().size.height);
this.addPauseButton();
this._gamePoints = cc.LabelTTF.create('0000', 'Chalkduster', 24);
this._gamePoints.setColor(cc.c3b(32, 32, 128));
this._gamePoints.setContentSize(cc.size(100, 30));
this._gamePoints.setPosition(cc.p(winSize.width - 100, winSize.height - 30));
this._gamePoints.setHorizontalAlignment(cc.TEXT_ALIGNMENT_RIGHT);
this.addChild(this._gamePoints);
var actionMove = cc.MoveTo.create(30, cc.p(0, 0));
var actionMoveDone = cc.CallFunc.create(function (node) {
// cc.log("Background Box Before Reposition | X:" + background.getBoundingBox().origin.x + " Y:" + background.getBoundingBox().origin.y + " WIDTH:" + background.getBoundingBox().size.width + " HEIGHT:" + background.getBoundingBox().size.height);
background.setPosition(cc.p(background.getPosition().x + winSize.width, 0));
// cc.log("Background Box After Reposition | X:" + background.getBoundingBox().origin.x + " Y:" + background.getBoundingBox().origin.y + " WIDTH:" + background.getBoundingBox().size.width + " HEIGHT:" + background.getBoundingBox().size.height);
}, this);
background.runAction(cc.RepeatForever.create(cc.Sequence.create(actionMove, actionMoveDone)));//(cc.Sequence.create(actionMove, actionMoveDone));
var player = cc.Sprite.create(s_player);
player.setPosition(player.getContentSize().width/2, winSize.height/2);
this.addChild(player);
cc.log("Player Box | X:" + player.getBoundingBox().origin.x + " Y:" + player.getBoundingBox().origin.y + " WIDTH:" + player.getBoundingBox().size.width + " HEIGHT:" + player.getBoundingBox().size.height);
this.schedule(this.gameLogic, 1.0);
this.scheduleUpdate();
// audioEngine.playMusic(s_bgMusic, true);
audioEngine.playEffect(s_shootEffect);
audioEngine.setEffectsVolume(0.2);
// cc.log("FX Volume: " + audioEngine.getEffectsVolume());
},
onExit:function () {
this._super();
while (this._enemies.length > 0) {
cc.ArrayRemoveObjectAtIndex(this._enemies, this._enemies.length - 1);
}
while (this._projectiles.length > 0) {
cc.ArrayRemoveObjectAtIndex(this._projectiles, this._projectiles.length - 1);
}
this._timer = 0;
this._enemiesDestroyed = 0;
this._gamePoints.setString('0');
},
addEnemy:function (level) {
var rangeMin = 1;
var rangeMax = 4;
// cc.log("Monster with index:" + monsterType);
// var s_monster = g_ressources[]; //eval("s_monster" + randomMonsterIndex);
/*
var monsterType = Math.floor(Math.random() * (rangeMax - 1 + rangeMin)) + rangeMin;
var monsterName = eval("s_monster" + monsterType);
var enemy = cc.Sprite.create(monsterName);
enemy.type = monsterType;
*/
var enemy = Enemy.create(level);
// switch(randomMonsterIndex)
// {
// case 1:
// enemy = cc.Sprite.create(s_monster1);
// break;
// case 2:
// enemy = cc.Sprite.create(s_monster2);
// break;
// case 3:
// enemy = cc.Sprite.create(s_monster3);
// break;
// case 4:
// enemy = cc.Sprite.create(s_monster4);
// break;
// default:
// enemy = cc.Sprite.create(s_monster1);
// }
var minY = enemy.getContentSize().height / 2;
var maxY = winSize.height - enemy.getContentSize().height / 2 ;
var rangeY = maxY - minY;
var actualY = (Math.random() * rangeY) + minY;
enemy.setPosition(winSize.width + enemy.getContentSize().width / 2, actualY);
// cc.log("Start Enemy Position x:" + enemy.getPosition().x + " y:" + enemy.getPosition().y);
this.addChild(enemy);
var minDuration = enemy.getEnemySpeed();
var rangeDuration = 3;
var actualDuration = (Math.random() * rangeDuration) + minDuration;
// cc.log("Enemy Move Duration: " + actualDuration + "| Enemy Speed:" + enemy.getEnemySpeed());
var actionMove = cc.MoveTo.create(actualDuration, cc.p(-enemy.getContentSize().width/2, actualY));
var actionMoveDone = cc.CallFunc.create(function (node) {
cc.ArrayRemoveObject(this._enemies, node);
node.removeFromParent();
var scene = GameOver.create(false, this._enemiesDestroyed);
cc.Director.getInstance().replaceScene(scene);
// cc.log("Enemy Reached End \n Enemy Position x:" + node.getPosition().x + " y:" + node.getPosition().y);
}, this);
enemy.runAction(cc.Sequence.create(actionMove, actionMoveDone));
enemy.setTag(1);
this._enemies.push(enemy);
},
gameLogic:function (dt) {
var level = Math.floor(this._timer / 10);
cc.log("timer: " + this._timer + "level: " + level);
for(var i = 0; i <= (level / 2); i++) {
this.addEnemy(level);
}
this._timer++;
},
locationTapped: function (location) {
var pauseButtonRect = this._pauseButton.getBoundingBox();
if (cc.rectContainsPoint(pauseButtonRect, location)){
if (cc.Director.getInstance().isPaused()){
this.resumeGame();
}
else {
this.pauseGame();
}
}
else {
if (cc.Director.getInstance().isPaused())
return;
var projectile = Projectile.create(Projectile.TypeEnum.SHURIKEN);
projectile.setPosition(20, winSize.height/2);
var offset = cc.pSub(location, projectile.getPosition());
if (offset.x <= 0) return;
this.addChild(projectile);
audioEngine.setEffectsVolume(0.2);
audioEngine.playEffect(s_shootEffect);
var realX = winSize.width + (projectile.getContentSize().width / 2);
var ratio = offset.y / offset.x;
var realY = (ratio * realX) + projectile.getPosition().y;
var realDest = cc.p(realX, realY);
var offset = cc.pSub(realDest, projectile.getPosition());
var length = cc.pLength(offset);
var velocity = 480.0;
var realMoveDuration = length / velocity;
projectile.runAction(cc.RepeatForever.create(cc.RotateBy.create(0.5, 360)));
projectile.runAction(cc.Sequence.create(
cc.MoveTo.create(realMoveDuration, realDest),
cc.CallFunc.create(function (node) {
cc.ArrayRemoveObject(this._projectiles, node);
node.removeFromParent();
/*
if (this._enemiesDestroyed >= 2) {
var scene = GameOver.scene(true);
cc.Director.getInstance().replaceScene(scene);
}
*/
}, this)
));
projectile.setTag(2);
this._projectiles.push(projectile);
}
},
onMouseUp:function (event) {
var location = event.getLocation();
this.locationTapped(location);
},
onTouchesEnded:function (touches, event) {
if (touches.length <= 0) return;
var touch = touches[0];
var location = touch.getLocation();
this.locationTapped(location);
},
update:function (dt) {
for (var i = 0; i < this._projectiles.length; i++){
var projectile = this._projectiles[i];
var projectileRect = projectile.collideRect();
for (var j = 0; j < this._enemies.length; j++ ){
var enemy = this._enemies[j];
var enemyRect = enemy.collideRect();
if (cc.rectIntersectsRect(projectileRect, enemyRect)) {
cc.ArrayRemoveObject(this._projectiles, projectile);
projectile.removeFromParent();
var burstRect = cc.rectIntersection(projectileRect, enemyRect);
this.effect(burstRect.origin, s_burst, s_burstPlist, 8, "burst");
if (enemy.decreaseHP()) {
this.effect(enemy.getPosition(), s_explosion, s_explosionPlist, 12, "explosion");
cc.ArrayRemoveObject(this._enemies, enemy);
enemy.removeFromParent();
this._enemiesDestroyed++;
}
}
}
}
this._gamePoints.setString("live:" + this._enemies.length + "kills:" + this._enemiesDestroyed);
},
addPauseButton:function () {
this.resumeGame();
},
pauseGame:function () {
if (this._pauseButton){
this._pauseButton.removeFromParentAndCleanup();
}
this._pauseButton = cc.Sprite.create(s_play);
this._pauseButton.setPosition(cc.p(50,winSize.height - 50));
this.addChild(this._pauseButton);
cc.Director.getInstance().pause();
for (var i = 0; i < this._enemies.length; i++) {
this._enemies[i].setVisible(false);
}
for (var i = 0; i < this._projectiles.length; i++) {
this._projectiles[i].setVisible(false);
}
},
resumeGame:function () {
if (this._pauseButton){
this._pauseButton.removeFromParentAndCleanup();
}
this._pauseButton = cc.Sprite.create(s_pause);
this._pauseButton.setPosition(cc.p(50,winSize.height - 50));
this.addChild(this._pauseButton);
cc.Director.getInstance().resume();
for (var i = 0; i < this._enemies.length; i++) {
this._enemies[i].setVisible(true);
}
for (var i = 0; i < this._projectiles.length; i++) {
this._projectiles[i].setVisible(true);
}
},
effect: function (location, img_name, plist_name, frames_count, img_prefix) {
var spriteFrameCache = cc.SpriteFrameCache.getInstance();
spriteFrameCache.addSpriteFrames(plist_name, img_name);
var spriteBatchNode = cc.SpriteBatchNode.create(img_name, 8);
this.addChild(spriteBatchNode);
var animationFrames = [];
for (var i = 0; i < frames_count; i++) {
var spriteFrame = spriteFrameCache.getSpriteFrame(img_prefix + i + ".png");
var animationFrame = new cc.AnimationFrame();
animationFrame.initWithSpriteFrame(spriteFrame,0.2,null);
animationFrames.push(animationFrame);
}
var animation = cc.Animation.createWithAnimationFrames(animationFrames, 0.2, 1);
var animate = cc.Animate.create(animation);
var callback = cc.CallFunc.create(function (node) {
node.removeFromParent();
}, this);
var sequence = cc.Sequence.create(animate, callback);
var sprite = cc.Sprite.createWithSpriteFrameName(img_prefix + "0.png");
sprite.setPosition(location);
this.addChild(sprite);
sprite.runAction(sequence);
}
});
MainLayer.create = function () {
var sg = new MainLayer();
if (sg && sg.init(cc.c4b(255,255,255,255))) {
return sg;
}
return null;
};
MainLayer.scene = function () {
var scene = cc.Scene.create();
var layer = MainLayer.create();
scene.addChild(layer);
return scene;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment