OSDN Git Service

無敵のキャラが表示されないバグ修正
[h58pcdgame/GameScriptCoreLibrary.git] / www / corelib / classes / EnemyCharacterClass.js
index 0b68ff4..d0639e8 100644 (file)
@@ -7,7 +7,10 @@ var EnemyCharacterClass = function(stage, args){
        this.chasing = true;
        //メインキャラクターを感知する範囲(原点距離がこれ以内の場合に追跡をする。)
        this.chasingRange = 200;
-       //メインキャラクターに与えるダメージの設定
+       this.fightingRange = 32;
+       this.chasingMode = 0;
+       this.lastCollidedTick = 0;
+       //他のキャラクターに与えるダメージの設定
        this.touchDamage = 10;
        this.damagePerTickBase = 60;
        this.damagePerTickCount = this.damagePerTickBase;
@@ -22,22 +25,35 @@ var EnemyCharacterClass = function(stage, args){
        //
        this.max_HP = 20;
        this.HP = this.max_HP;
+       this.isWeaponed = false;
 }.extend(OperatedCharacterClass, {
        className: "EnemyCharacterClass",
        tick : function()
        {
                var main = this.ownerStage.userControlledCharacter;
-               var distX = main.origin.x - this.origin.x;
+               var distX = main.origin.x - this.origin.x - ((this.chasingMode == 0) ? this.fightingRange : -this.fightingRange);
                if(this.chasing && Math.abs(distX) <= this.chasingRange && Math.abs(this.origin.y - main.origin.y) <= this.chasingRange){
+                       //追跡処理
                        if(distX > 0){
                                this.goRight();
+                               if(this.chasingMode == 0){
+                                       this.chasingMode = 1;
+                               }
                        } else{
                                this.goLeft();
+                               if(this.chasingMode == 1){
+                                       this.chasingMode = 0;
+                               }
+                       }
+                       //攻撃処理
+                       if(this.isWeaponed){
+                               this.fire();
                        }
                }
+               //メインキャラへのダメージ
                if(this.damaging){
                        if(this.damagePerTickCount == this.damagePerTickBase){
-                               this.ownerStage.manager.userManager.damage(this.touchDamage);
+                               main.damage(this.touchDamage);
                                this.damagePerTickCount--;
                        } else{
                                this.damagePerTickCount--;
@@ -46,13 +62,11 @@ var EnemyCharacterClass = function(stage, args){
                                }
                        }
                }
+               //自分自身のダメージ
                if(this.hurting){
                        if(this.hurtPerTickCount == this.hurtPerTickBase){
-                               this.HP -= this.hurt;
+                               this.damage(this.hurt);
                                this.hurtPerTickCount--;
-                               if(this.HP <= 0){
-                                       this.ownerStage.removeStageObject(this);
-                               }
                        } else{
                                this.hurtPerTickCount--;
                                if(this.hurtPerTickCount <= 0){
@@ -60,7 +74,17 @@ var EnemyCharacterClass = function(stage, args){
                                }
                        }
                }
-               
+               if(this.lastCollidedTick + 10 < this.ownerStage.manager.tickCount){
+                       if(this.hurting){
+                               //自分自身のダメージ無効
+                               this.hurting = false;
+                       }
+                       if(this.damaging){
+                               //キャラクターへのダメージ無効
+                               this.damaging = false;
+                       }
+               }
+               //落下後の死亡処理
                if(this.origin.y > 1000)
                {
                        this.ownerStage.removeStageObject(this);
@@ -68,70 +92,63 @@ var EnemyCharacterClass = function(stage, args){
                
                EnemyCharacterClass.base.prototype.tick.apply(this);
        },
-       draw: function(x, y){
-               EnemyCharacterClass.base.prototype.draw.apply(this, arguments);
-               this.ownerStage.mainContext.save();
-               this.ownerStage.mainContext.fillStyle = "rgba(255,255,255,0.5)";
-               this.ownerStage.mainContext.strokeStyle = "rgba(0, 0, 0, 1)";
-               this.ownerStage.mainContext.font = "normal 12px sans-serif";
-               drawText(this.ownerStage.mainContext, this.HP, x, y - 20);
-               this.ownerStage.mainContext.restore();
+       damage: function(d){
+               //自分自身へのダメージ
+               this.HP -= d;
+               if(this.HP <= 0){
+                       this.ownerStage.removeStageObject(this);
+               }
        },
-       judgeCollideRange: function(x1, y1, x2, y2, obj){
-               //objは範囲の対象となるオブジェクトのインスタンス。省略可能
-               // 1 - - +
-               // |     |
-               // |     |
-               // + - - 2
-               var retv = OperatedCharacterClass.prototype.judgeCollideRange.apply(this, arguments);
-               if(obj == this.ownerStage.userControlledCharacter){
-                       if(retv){
-                               var cd = this.judgeCollideRange_getDirection(x1, y1, x2, y2);
-                               if(cd & CollideTop){
+       objectAttacked : function(obj, obj_x, obj_y, direction)
+       {
+               if(obj instanceof MainCharacterClass)
+               {
+                       //メインキャラクターに衝突された
+                       //ダメージの開始・終了処理
+                       if(direction & CollideTop){
+                               if(!this.hurting){
+                                       //自分自身のダメージ有効
                                        this.hurting = true;
-                                       var tickCount = this.ownerStage.manager.tickCount;
-                                       this.hurtLastTick = tickCount;
-                               } else{
-                                       this.hurting = false;
                                        this.hurtPerTickCount = this.hurtPerTickBase;
                                }
                        } else{
-                               if(this.hurting){
-                                       this.hurting = false;
-                                       this.hurtPerTickCount = this.hurtPerTickBase;
+                               if(direction & (CollideLeft | CollideRight)){
+                                       if(!this.damaging){
+                                               //メインキャラクタへのダメージ有効
+                                               this.damaging = true;
+                                               this.damagePerTickCount = this.damagePerTickBase;
+                                       }
                                }
                        }
+                       this.lastCollidedTick = this.ownerStage.manager.tickCount;
                }
-               return retv;
        },
-       isCollided : function(obj, x, y)
-       {
-               //MovableStageObjectが(x, y)のとき相手のobjと衝突するか判定
-               var retv = OperatedCharacterClass.prototype.isCollided.apply(this, arguments);
-               if(obj == this.ownerStage.userControlledCharacter){
-                       var tickCount = this.ownerStage.manager.tickCount;
-                       if(retv){
-                               if((this.collideFlag & 8) != 0){
-                                       //上から衝突された場合は自分がダメージを受ける
-                                       //this.hurting = true;
-                                       //this.hurtLastTick = tickCount;
-                               } else{
-                                       //それ以外の場合はメインキャラにダメージを与える
-                                       this.damaging = true;
-                                       this.damageLastTick = tickCount;
-                               }
-                       } else{
-                               if(this.damaging && tickCount > (this.damageLastTick + 5)){
-                                       this.damaging = false;
-                                       this.damagePerTickCount = this.damagePerTickBase;
-                               }
-                               //if(this.hurting && tickCount > (this.hurtLastTick + 5)){
-                               //      this.hurting = false;
-                               //      this.hurtPerTickCount = this.hurtPerTickBase;
-                               //}
+       draw: function(x, y){
+               EnemyCharacterClass.base.prototype.draw.apply(this, arguments);
+               if(this.hurt > 0)
+               {
+                       this.ownerStage.mainContext.save();
+                       var backColor = "gray";
+                       var thickness = 4;
+                       var barWidth = this.max_HP;
+                       if(barWidth < 16) barWidth = 16;
+                       var barPerHP = this.HP / this.max_HP;
+                       var color = "green";
+                       
+                       if(barPerHP < 0.2)
+                       {
+                               var backColor = "red";
+                       }else if(barPerHP < 0.4)
+                       {
+                               var backColor = "yellow";
                        }
+                       
+                       this.ownerStage.mainContext.fillStyle = backColor;
+                       this.ownerStage.mainContext.fillRect(x + (this.size.x - barWidth) / 2, y - thickness, barWidth, thickness);
+                       this.ownerStage.mainContext.fillStyle = color;
+                       this.ownerStage.mainContext.fillRect(x + (this.size.x - barWidth) / 2, y - thickness, barWidth * barPerHP, thickness);
+                       
+                       this.ownerStage.mainContext.restore();
                }
-               return retv
        },
-       
 });