private void checkPlayerHit2(PersonEntity person1, PersonEntity person2) { if (person1.type == ConfigConstant.ENTITY_CALL && person1.ownerPlayer.teamIndex == person2.ownerPlayer.teamIndex) { return; } if (person2.type == ConfigConstant.ENTITY_CALL && person1.ownerPlayer.teamIndex == person2.ownerPlayer.teamIndex) { return; } CollisionInfo info = Collision.checkCollision(person1.collisionShape, person2.collisionShape, this.mapData); if (info.isHit) { //相对速度冲量(投影到斥力方向的分力) Vector2D velocityDeltaV2d = person1.velocity.clone().subtract(person2.velocity); Vector2D positionDeltaV2d = info.deltaPos.clone(); double len = velocityDeltaV2d.projectionOn(positionDeltaV2d); if (len > 0) { Vector2D pushForceV2d = positionDeltaV2d.clone(); bool isPlayer = person1.type == ConfigConstant.ENTITY_PLAYER && person2.type == ConfigConstant.ENTITY_PLAYER; bool hurtCollision = isPlayer && person1.ownerPlayer.teamIndex != person2.ownerPlayer.teamIndex; double forcePush = hurtCollision ? ConfigConstant.PLAYER_FORCE_PUSH2 : ConfigConstant.PLAYER_FORCE_PUSH; forcePush *= len; int pushBack = isPlayer ? ConfigConstant.PUSH_FORCE_BACK : 0; pushForceV2d.length = forcePush * person1.scale / person2.scale + pushBack; person2.addForce(pushForceV2d); pushForceV2d.length = forcePush * person2.scale / person1.scale + pushBack; pushForceV2d.multiply(-1); person1.addForce(pushForceV2d); pushForceV2d.clear(); //都是人 并且队伍不同。 碰撞伤害! if (hurtCollision) { //子弹的配置。也确实 写死就好了。 Dictionary <string, object> bulletData = new Dictionary <string, object> { { "resId", "bulletNull" }, { "speed", 1 }, { "lifeTime", 30 }, { "atk", (int)(forcePush * ConfigConstant.PUSH_DAMAGE_RATE) + ConfigConstant.PUSH_DAMAGE }, { "range", new object[] { ConfigConstant.SHIP_RADIUS } }, { "posTarget", 1 }, { "bangRes", "hitCollide" }, { "bangScale", Math.Sqrt(forcePush * 0.02) }, { "buff", new Dictionary <string, object>() { { "spRate", new Dictionary <string, object>() { //'joystickMin': { 'value':60, 'operation':1, 'effect':'buff002' }, #遥感最小值 { "value", 0.4f }, { "operation", 2 }, { "time", 500 }, } }, { "id", "spRate" } } } }; Vector2D pos = positionDeltaV2d.clone().multiply(0.5); BulletEntity bullet = this.createFightEntity(ConfigConstant.ENTITY_BULLET) as BulletEntity; bullet.lockTarget = person1; bullet.owner = person2; bullet.position.copy(pos); bullet.initConfig(bulletData); bullet = this.createFightEntity(ConfigConstant.ENTITY_BULLET) as BulletEntity; bullet.lockTarget = person2; bullet.owner = person1; bullet.position.copy(pos.reverse()); bullet.initConfig(bulletData); } } //近距离排斥力,越近越大 double dist = (person1.collisionShape.radius + person2.collisionShape.radius) - positionDeltaV2d.length; positionDeltaV2d.length = dist * ConfigConstant.PLAYER_FORCE_REPULSIVE; person2.addForce(positionDeltaV2d); positionDeltaV2d.multiply(-1); person1.addForce(positionDeltaV2d); velocityDeltaV2d.clear(); } }
///判断和玩家的碰撞 public void checkPerson() { //TODO:这里检测了两次碰撞。 只用一次其实就够了。 filter传true。 List <FightEntity> personList = this._map.getFightEntitysByRange(this._shape, new List <int> { ConfigConstant.ENTITY_PLAYER, ConfigConstant.ENTITY_CALL }, null, -1); //把敌我双方的所有人取出来,不需要排序。 //personList = this._map.players.ConvertAll<FightEntity>((a)=> { return a; }); for (int i = 0, len = personList.Count; i < len; i++) { PersonEntity person = (PersonEntity)personList[i]; if (!person.alived) { continue; } Vector2D deltaPosition = person.findDelta; double dist = person.findDist; //把玩家向外推,越中心越强 double rate = 1 - Math2.range(dist / (person.shape.radius + this._shape.radius), 1, 0); //玩家受到的推力,玩家越大越难被推动 double value = (dist * rate * 0.4f + this._pushValue) / person.scale; Vector2D forcePersonPush = Vector2D.createVector2(value, deltaPosition.angle); person.addForce(forcePersonPush); if (this._bounceSelfRate > 0) { //相对速度 Vector2D deltaVelocity = person.velocity.clone().subtract(this._velocity); //相对速度(投影到位置向量,为负时向内冲) double velocityValue = deltaVelocity.projectionOn(deltaPosition); if (velocityValue < 0) { //玩家向内冲,弹开,看速度冲量 Vector2D projectionVelocity = deltaVelocity.projectionVector(deltaPosition); Vector2D forcePerson = projectionVelocity.clone().multiply(this._bouncePersonRate / person.scale); Vector2D forceSelf = projectionVelocity.clone().multiply(-this._bounceSelfRate * person.scale); this.addForce(forceSelf); person.addForce(forcePerson); projectionVelocity.clear(); forcePerson.clear(); forceSelf.clear(); } } //碰到了 给人上buff。 if (this._data.ContainsKey("buff")) { Dictionary <string, object> buffs = (Dictionary <string, object>) this._data["buff"]; string id = buffs["id"].ToString(); foreach (string key in buffs.Keys) { if (key == "id") { continue; } new Buff(this._map).initBuff(id, key, person, this, (Dictionary <string, object>)buffs[key]); } } this.dispatchEventWith(EventConstant.HIT); } }