Skip to content

Commit

Permalink
back
Browse files Browse the repository at this point in the history
  • Loading branch information
akargapolov committed Dec 21, 2023
1 parent 8694719 commit a43cd3d
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 331 deletions.
40 changes: 1 addition & 39 deletions lecture2/src/battle.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,11 @@ var Battle = function () {
this.solder = new Solder('solder');
this.enemy = new Solder('enemy');

//this.showBattleStartAnimation = showBattleStartAnimation;

this.showVictoryAnimation = showVictoryAnimation;


setTimeout(this.start.bind(this), 3000);
};



Battle.prototype.start = function () {
showBattleStartAnimation();

this.running = true;
this.running = true;

console.log("Started!");

Expand All @@ -29,12 +20,6 @@ Battle.prototype.start = function () {
Battle.prototype.run = function () {
if (!this.solder.isAlive() || !this.enemy.isAlive()) {
this.stop();
if (this.solder.isAlive()) {
console.log("Победа!");
this.showVictoryAnimation();
} else {
console.log("Поражение!");
}
return;
}

Expand All @@ -47,8 +32,6 @@ Battle.prototype.run = function () {
this.enemy.attack(this.solder);
delete this.nextEnemyAttack;
}


};

Battle.prototype.stop = function () {
Expand All @@ -59,25 +42,4 @@ Battle.prototype.stop = function () {
clearInterval(this.interval);
};

var showBattleStartAnimation = function() {
var startAnimation = new cc.Node();
var animation = sp.SkeletonAnimation.create(resources.battle_start, resources.battle_atlas);
animation.setAnimation(0, "animation", false);
animation.setCompleteListener(function() {
startAnimation.removeFromParent();
});
startAnimation.addChild(animation);
startAnimation.setPosition(cc.director.getWinSize().width / 2, cc.director.getWinSize().height / 2);
cc.director.getRunningScene().addChild(startAnimation, 10);
}
function showVictoryAnimation() {
var victoryAnim = sp.SkeletonAnimation.create(resources.battle_victory, resources.battle_atlas);
victoryAnim.setPosition(cc.winSize.width / 2, cc.winSize.height / 2);
victoryAnim.setAnimation(0, "animation", false);
victoryAnim.setCompleteListener(function() {
victoryAnim.removeFromParent();
})
cc.director.getRunningScene().addChild(victoryAnim);
}

Battle.ENEMY_INTERVAL = [2000, 3000];
21 changes: 2 additions & 19 deletions lecture2/src/battlescene.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ var BattleScene = cc.Scene.extend({
this.enemyView.setPosition(this.width / 2 + this.width / 6, this.height / 2);
this.addChild(this.enemyView);

this.isAttackButtonDisabled = false;
this.addAttackButton();

cc.audioEngine.playMusic(resources.battle_music, true);
Expand Down Expand Up @@ -47,28 +46,12 @@ var BattleScene = cc.Scene.extend({
this.attackButton.setTitleFontName(resources.marvin_round.name);

this.attackButton.addClickEventListener(function () {
if (this.isAttackButtonDisabled || !this.battle.running) {
console.log("wait start or button is disabled");
if (!this.battle.running) {
console.log("wait start");
return;
}

this.battle.solder.attack(this.battle.enemy);
this.disableAttackButton();
}.bind(this));
},

disableAttackButton: function () {
this.isAttackButtonDisabled = true;
this.attackButton.setEnabled(false);
this.attackButton.setBright(false);

setTimeout(this.enableAttackButton.bind(this), 1000);
},

enableAttackButton: function () {
this.isAttackButtonDisabled = false;
this.attackButton.setEnabled(true);
this.attackButton.setBright(true);
}

});
77 changes: 0 additions & 77 deletions lecture2/src/mainscene.js

This file was deleted.

6 changes: 0 additions & 6 deletions lecture2/src/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,10 @@
var resources = {
background: 'res/background.jpg',

battle_start: 'res/battle_jsons/battle_versus.json',
battle_victory: 'res/battle_jsons/battle_victory.json',

battle_plist: 'res/battle.plist',
battle_atlas: 'res/battle.atlas',
battle_png: 'res/battle.png',

progressbar_background: 'res_source/progress_bar/progress_background.png',
progressbar: 'res_source/progress_bar/progressbar.png',

battle_enemy_effect: 'res/sounds/battle_enemy.mp3',
battle_solder_effect: 'res/sounds/battle_hit.mp3',
battle_music: 'res/sounds/battle_music.mp3',
Expand Down
2 changes: 1 addition & 1 deletion lecture2/src/solder.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Solder.prototype.takeDamage = function (damage) {
console.log(this.type + ' takeDamage, hp - ' + this.hp);

if (this.isAlive()) {
this.onTakeDamageAnimation(damage);
this.onTakeDamageAnimation();
} else {
this.onDieAnimation();
}
Expand Down
98 changes: 11 additions & 87 deletions lecture2/src/solderview.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,11 @@
* Created by andrey on 08.05.2022.
*/


var SolderView = cc.Node.extend({
ctor: function (solder) {
this._super();

this.solder = solder;
this.showVictoryAnimation = showVictoryAnimation;

this.damageLabel = new cc.LabelTTF("", "Arial", 20);
this.damageLabel.setPosition(cc.p(0, 40));
this.addChild(this.damageLabel);

this.animation = sp.SkeletonAnimation.create(resources[solder.type + solder.code + '_json'], resources.battle_atlas);
this.animation.setAnimation(0, "idle", true);
Expand All @@ -25,105 +19,35 @@ var SolderView = cc.Node.extend({
solder.onTakeDamageAnimation = this.onTakeDamage.bind(this);
solder.onAttackAnimation = this.onAttack.bind(this);
solder.onDieAnimation = this.onDie.bind(this);


this.maxHp = solder.hp;
this.hp = solder.hp;


this.progressBarContainer = new cc.Node();
this.addChild(this.progressBarContainer);


this.progressBarBg = new ccui.Scale9Sprite(resources.progressbar_background);
this.progressBarBg.width = 100;
this.progressBarBg.height = 20;

this.progressBarContainer.addChild(this.progressBarBg);

this.progressBar = new ccui.Scale9Sprite(resources.progressbar);
this.progressBar.width = 100;
this.progressBar.height = 20;

this.progressBarContainer.addChild(this.progressBar);

this.updateProgressBar();
},


onDie: function () {
onDie: function() {
this.animation.runAction(new cc.Sequence(
new cc.FadeOut(0.3),
new cc.ToggleVisibility()
));
this.damageLabel.visible = false;

this.progressBarContainer.removeFromParent();
},

onAttack: function () {
onAttack: function() {
this.animation.setAnimation(0, "attack", false);
this.animation.setCompleteListener(function () {
this.animation.setCompleteListener(function() {
this.animation.setAnimation(0, "idle", true);
}.bind(this));

cc.audioEngine.playEffect(resources['battle_' + this.solder.type + '_effect'], false);
},

onTakeDamage: function (damage) {
this.hp -= damage;
this.updateProgressBar();

var progressDelta = 0.3;
var newProgress = this.hp / this.maxHp;
var progressDiff = this.progressBar.width - (newProgress * this.progressBarBg.width);
var reduceAmount = progressDiff * progressDelta;
this.progressBar.width -= reduceAmount;

onTakeDamage: function () {
this.animation.runAction(new cc.Sequence(
new cc.FadeTo(0.3, 140),
new cc.FadeTo(0.3, 255)
));

var damageAnimation = sp.SkeletonAnimation.create(resources.damage_json, resources.battle_atlas);
damageAnimation.setAnimation(0, "animation", false);
damageAnimation.setCompleteListener(function () {
damageAnimation.removeFromParent();
});
this.addChild(damageAnimation);



this.damageLabel.setString("-" + damage);
this.damageLabel.setPositionX(75);
this.damageLabel.visible = true;

this.damageLabel.runAction(cc.sequence(
cc.fadeIn(0.2),
cc.delayTime(1.0),
cc.fadeOut(0.2),
cc.callFunc(function () {
this.damageLabel.visible = false;
}, this)

));

},


updateProgressBar: function () {
var progress = this.hp / this.maxHp;

this.progressBarContainer.x = 50;
this.progressBarContainer.y = -80;

this.progressBar.x = -this.progressBar.width / 2;
this.progressBar.y = 0;

this.progressBarBg.x = -this.progressBarBg.width / 2;
this.progressBarBg.y = 0;

this.progressBarBg.zIndex = -1;
var damage = sp.SkeletonAnimation.create(resources.damage_json, resources.battle_atlas);
damage.setAnimation(0, "animation", false);
damage.setCompleteListener(function() {
damage.removeFromParent();
})
this.addChild(damage);
}

});
});
Loading

0 comments on commit a43cd3d

Please sign in to comment.