LostRoutes项目日志——敌人精灵Enemy解析

Enemy类在Enemy.js中,类Enemy类继承自PhysicsSprite,以便于可以使用物理引擎中的一些特性。

原版的Enemy.js:

  1 var Enemy = cc.PhysicsSprite.extend({//PhysicsSprite
  2     enemyType: 0,        //敌人类型
  3     initialHitPoints: 0,    //初始的生命值
  4     hitPoints: 0,        //当前的生命值
  5     velocity: null,            //速度
  6     space: null,         //所在物理空间
  7     ctor: function (enemyType, space) {
  8         //精灵帧
  9         var enemyFramName = EnemyName.Enemy_Stone;
 10         //得分值
 11         var hitPointsTemp = 0;
 12         //速度
 13         var velocityTemp = cc.p(0, 0);
 14         switch (enemyType) {
 15         case EnemyTypes.Enemy_Stone:
 16             enemyFramName = EnemyName.Enemy_Stone;
 17             hitPointsTemp = Enemy_initialHitPoints.Enemy_Stone;
 18             velocityTemp = Sprite_Velocity.Enemy_Stone;
 19             break;
 20         case EnemyTypes.Enemy_1:
 21             enemyFramName = EnemyName.Enemy_1;
 22             hitPointsTemp = Enemy_initialHitPoints.Enemy_1;
 23             velocityTemp = Sprite_Velocity.Enemy_1;
 24             break;
 25         case EnemyTypes.Enemy_2:
 26             enemyFramName = EnemyName.Enemy_2;
 27             hitPointsTemp = Enemy_initialHitPoints.Enemy_2;
 28             velocityTemp = Sprite_Velocity.Enemy_2;
 29             break;
 30         case EnemyTypes.Enemy_Planet:
 31             enemyFramName = EnemyName.Enemy_Planet;
 32             hitPointsTemp = Enemy_initialHitPoints.Enemy_Planet;
 33             velocityTemp = Sprite_Velocity.Enemy_Planet;
 34             break;
 35         }
 36 
 37         this._super("#" + enemyFramName);
 38         this.setVisible(false);
 39 
 40         this.initialHitPoints = hitPointsTemp;
 41         this.velocity = velocityTemp;
 42         this.enemyType = enemyType;
 43 
 44         this.space = space;
 45 
 46         var shape;
 47 
 48         if (enemyType == EnemyTypes.Enemy_Stone || enemyType == EnemyTypes.Enemy_Planet) {
 49             this.body = new cp.Body(10, cp.momentForCircle(1, 0, this.getContentSize().width / 2 - 5, cp.v(0, 0)));
 50             shape = new cp.CircleShape(this.body, this.getContentSize().width / 2 - 5, cp.v(0, 0));
 51         } else if (enemyType == EnemyTypes.Enemy_1) {
 52             var verts = [
 53                          -5, -91.5,
 54                          -59, -54.5,
 55                          -106, -0.5,
 56                          -68, 86.5,
 57                          56, 88.5,
 58                          110, -4.5
 59                          ];
 60             this.body = new cp.Body(1, cp.momentForPoly(1, verts, cp.vzero));
 61             shape = new cp.PolyShape(this.body, verts, cp.vzero);
 62         } else if (enemyType == EnemyTypes.Enemy_2) {
 63             var verts = [
 64                          2.5, 64.5,
 65                          73.5, -9.5,
 66                          5.5, -63.5,
 67                          -71.5, -6.5
 68                          ];
 69             this.body = new cp.Body(1, cp.momentForPoly(1, verts, cp.vzero));
 70             shape = new cp.PolyShape(this.body, verts, cp.vzero);
 71         }
 72 
 73         this.space.addBody(this.body);
 74 
 75         shape.setElasticity(0.5);
 76         shape.setFriction(0.5);
 77         shape.setCollisionType(Collision_Type.Enemy);
 78         this.space.addShape(shape);
 79         //this.setBody(this.body);
 80         this.body.data = this;
 81 
 82         this.scheduleUpdate();
 83     },
 84 
 85     update: function (dt) {
 86         //设置陨石和行星旋转.
 87         switch (this.enemyType) {
 88         case EnemyTypes.Enemy_Stone:
 89             this.setRotation(this.getRotation() - 0.5);
 90             break;
 91         case EnemyTypes.Enemy_Planet:
 92             this.setRotation(this.getRotation() + 1);
 93             break;
 94         }
 95         //计算移动位置
 96         var newX = this.body.getPos().x + this.velocity.x * dt;
 97         var newY = this.body.getPos().y + this.velocity.y * dt;
 98 
 99         this.body.setPos(cc.p(newX, newY));
100 
101         //超出屏幕重新生成敌人
102         if (this.body.getPos().y + this.getContentSize().height / 2 < 0) {
103             this.spawn();
104         }
105     },
106     spawn: function () {
107         var yPos = winSize.height + this.getContentSize().height / 2;
108         var xPos = cc.random0To1() * (winSize.width - this.getContentSize().width) + this.getContentSize().width / 2;
109         this.body.setPos(cc.p(xPos, yPos));
110         this.hitPoints = this.initialHitPoints;
111         this.setVisible(true);
112     }
113 });

第44行:初始化敌人所在的物理空间,使用物理空间引入物理引擎,进行碰撞检测。
第49~80行:将敌人对象添加物理引擎支持,使之能够利用物理引擎精确检测碰撞。
(当然不适用物理引擎,也可以检测碰撞,一般情况下只能检测简单的矩形碰撞,不够精准)
第49行:是在敌人类型是陨石和行星情况下创建物理对象,cp.momentForCircle函数是创建圆形物理惯性力矩,其中第一个参数是质量,1是经验值;第二个参数是圆形内径;第三个参数是圆形外径;第四个参数是偏移量。
第50行:为物体添加圆形形状,其中this.getContentSize().width/2是半径,-5是修正值。
第52行:使用verts坐标数组创建物体。
第61行:为物体添加多边形形状,这是针对飞机形状的敌人。
提示:由于底层封装了Chipmunk引擎,Chipmunk要求多边形定点数据必须是按照顺时针,必须是凸多边形。如果遇到凹多边形,则可以把它分割成为几个凸多边形。另外,顶点坐标的原点在图形的中心,OpenGL坐标。
第73行:this.space.addBody(this.body)是将上面定义好的物体对象添加到物理空间中。
第75行:shape.setElasticity(0.5)是为形状设置弹性系数。
第76行:shape.setFriction(0.5)是为形状设置摩擦系数。
第77行:通过shape.setCollisionType(Collision_Type.Enemy)语句为形状设置碰撞检测类型。
第78行:this.space.addShape(shape)语句将形状添加到物理空间中。
第80行:this.body.data=this是把精灵放到物体的data数据成员中,这样在碰撞发生的时候可以通过下面的语句从物体取出精灵对象。

原文地址:https://www.cnblogs.com/moonlightpoet/p/5715280.html