private void OnTriggerEnter2D(Collider2D collision) { // столкновение с астероидом if (collision.tag == "Asteroid") { AsteroidView asteroid = collision.GetComponent <AsteroidView>(); HpEvent?.Invoke(asteroid.GetAsteroidDamage()); asteroid.KillAsteroid(); } }
private void OnTriggerEnter2D(Collider2D collision) { if (collision.tag == "Respawn") { DestroyAsteroid(); } if (collision.tag == "Bullet") { if (HpEvent != null) { BulletView bullet = collision.gameObject.GetComponent <BulletView>(); HpEvent?.Invoke(bullet.GetDamage()); ScoreEvent?.Invoke(1); bullet.DestroyBullet(); } } }
public void Regenerate(float elapsedSec) { if (hp >= GlobalVars.MotherHP) { return; } hpAccumulated += elapsedSec * HPregenPerSec; if (hpAccumulated > hpRegainAmount) { int hpToAdd = (int)hpAccumulated; hp += hpToAdd; hpAccumulated = hpAccumulated - (float)Math.Floor(hpAccumulated); var eventInstance1 = new HpEvent { ItemId = this.mobItem.Id, HpChange = -hpToAdd, }; var eventData1 = new EventData((byte)EventCode.HpEvent, eventInstance1); var message1 = new ItemEventMessage(target.Avatar, eventData1, MmoActorOperationHandler.GetDefaultSP()); mobItem.EventChannel.Publish(message1); } }
public void UpdatePosition(float elapsedSeconds, SendParameters sp) { if (mobItem.Disposed) { return; } Vector oldPosition = mobItem.Position; this.timeSinceVelUpdate -= elapsedSeconds; if (mobItem.wasBursted) { mobItem.secSinceBursted -= elapsedSeconds; if (mobItem.secSinceBursted <= 0) { mobItem.wasBursted = false; } } if (state == BotState.Patroling) { float distToTargetSq = (currTargetPos - oldPosition).Len2; if (distToTargetSq < 800 || distToTargetSq > GlobalVars.MaxDistFromMotherSq) { ChooseNewTargetPosAndSetVel(); // GlobalVars.log.InfoFormat(mobItem.Id + "chose new velo"); } if (mobItem.Velocity.Len2 > GlobalVars.maxShipVelSq + 1) { mobItem.Velocity = mobItem.Velocity * GlobalVars.burstDampening; SetVelToTarget(); } } if (state == BotState.Chasing) { Vector diff = target.Avatar.Position - mobItem.Position; float distToTarg2 = diff.Len2; if (type == BotType.FastSwarm) { currTargetPos = target.Avatar.Position; SetVelToTarget(); // if bot is colliding with it's targetted player do damage if (distToTarg2 < GlobalVars.swarmShipRadius2) { target.Hitpoints = target.Hitpoints - 1; var eventInstance1 = new HpEvent { ItemId = target.Avatar.Id, HpChange = 1, }; var eventData1 = new EventData((byte)EventCode.HpEvent, eventInstance1); var message1 = new ItemEventMessage(target.Avatar, eventData1, sp); target.Avatar.EventChannel.Publish(message1); } } if (type == BotType.Swarm) { if (distToTarg2 > GlobalVars.BotChasingStopDist2) { currTargetPos = target.Avatar.Position; SetVelToTarget(); } else { mobItem.Velocity = Vector.Zero; } if (timeTillReload > 0) { timeTillReload -= elapsedSeconds; } if (timeTillReload <= 0) // bullet is ready { // check if target is within shoot range if (distToTarg2 < GlobalVars.BotShotSight2) { timeTillReload = GlobalVars.BotReloadTime; mobItem.Rotation = diff / (float)Math.Sqrt(distToTarg2); BotManager.MobFireBullet(mobItem); } } } } //mobItem.Move(oldPosition + mobItem.Velocity * elapsedSeconds); mobItem.SetPos(oldPosition + mobItem.Velocity * elapsedSeconds); mobItem.UpdateRegionSimple(); if (GlobalVars.TrueTenPercent) { // GlobalVars.log.InfoFormat("mob Pos " + oldPosition.ToString()); } // send event var eventInstance = new ItemMoved { ItemId = mobItem.Id, OldPosition = oldPosition, Position = mobItem.Position, Rotation = mobItem.Rotation, // not changing rotation OldRotation = mobItem.Rotation }; var eventData = new EventData((byte)EventCode.ItemMoved, eventInstance); var message = new ItemEventMessage(mobItem, eventData, sp); mobItem.EventChannel.Publish(message); }
//; checks the ship item for collision with a potential projectile item, actor can be passed as // null if this ship is an npc. returns whether or not to delete the projectile public static bool CheckItemCollisionAgainstProjectile(Item ship, Item projectile, ref int hitPoints, SendParameters sp, MmoActorOperationHandler actor, float shipRadiusSq, float shipRadius) { // if its a bullet and it's not my own bullet if ((byte)projectile.Type == (byte)ItemType.Bullet && (actor == null || !actor.ContainsItem(projectile.Id))) { //Vector bulletPos = projectile.GetRewindedPos(Peer.RoundTripTime / 2); float distanceToBullet2 = (projectile.Position - ship.Position).Len2; // GlobalVars.log.InfoFormat("checkin coll " + projectile.Id.ToString() + "dist: " + distanceToBullet2.ToString()); if (distanceToBullet2 < shipRadiusSq) { GlobalVars.log.InfoFormat(ship.Id + " COLLISION with: " + (ItemType)projectile.Type); //if (removeList == null) // removeList = new List<Tuple<Item, Region>>(); //removeList.Add(new Tuple<Item, Region>(projectile, reg)); // set bullet to be removed // sp.Unreliable = false; var eventInstance = new HpEvent { ItemId = ship.Id, HpChange = GlobalVars.bulletDamage }; var eventData = new EventData((byte)EventCode.HpEvent, eventInstance); var message = new ItemEventMessage(ship, eventData, sp); ship.EventChannel.Publish(message); hitPoints -= GlobalVars.bulletDamage; if (hitPoints < 0) { // send itemdestroyed message for player's ship item } ((Bullet)projectile).SetDead(); // projectile.SetPos(new Vector(-60000, -60000)); // projectile.UpdateRegionSimple(); return(true); } } else if ((byte)projectile.Type == (byte)ItemType.Bomb) { if (actor != null && actor.isSaberOut) { Vector saberstartPt = new Vector(); Vector saberendPt = new Vector(); saberstartPt = ship.Position + ship.Rotation * GlobalVars.saberStart; saberendPt = ship.Position + ship.Rotation * GlobalVars.saberLength; Vector offset = new Vector(); if (MathHelper.Intersects(saberstartPt, saberendPt, projectile.Position, shipRadiusSq, ref offset, shipRadius, null, true)) { GlobalVars.log.InfoFormat("saber collide with a bomb"); projectile.SetPos(projectile.Position + offset * 2.1f); // move over instantly to uncollided position // set bomb velocity using 'collision impact' with saber projectile.Velocity = projectile.Velocity + (offset / offset.Len) * ship.GetCumulativeDotProducts(8) * 90f; } } // if this is not my bomb, check if i collide with it if ((actor != null && !actor.ContainsItem(projectile.Id)) || actor == null) { Vector difference = (projectile.Position - ship.Position); float distanceToBomb2 = difference.Len2; if (distanceToBomb2 < (shipRadiusSq + GlobalVars.bombBallRadius2)) { GlobalVars.log.InfoFormat("player + " + ship.Id.Substring(0, 3) + " collision with: " + projectile.Id.ToString()); //if (removeList == null) // removeList = new List<Tuple<Item, Region>>(); //removeList.Add(new Tuple<Item, Region>(projectile, reg)); // set bomb to be removed float t = (GlobalVars.bombRadius2 - distanceToBomb2) / GlobalVars.bombRadius2; int damageToPlayer = (int)Math.Floor(t * GlobalVars.bombDmg); hitPoints -= damageToPlayer; // sp.Unreliable = false; var eventInstance = new HpEvent { ItemId = ship.Id, HpChange = damageToPlayer, }; var eventData = new EventData((byte)EventCode.HpEvent, eventInstance); var message = new ItemEventMessage(ship, eventData, sp); ship.EventChannel.Publish(message); float force = GlobalVars.bombForce * t; difference = difference / (float)Math.Sqrt(distanceToBomb2); ship.wasBursted = true; ship.secSinceBursted = GlobalVars.timeForBurstToLast; ship.Velocity = ship.Velocity + difference * force; return(true); } } } return(false); }