public override void OnTick() { base.OnTick(); if (Velocity.Length() > 0) { PlayerLocation oldPosition = (PlayerLocation)KnownPosition.Clone(); bool onGroundBefore = IsOnGround(KnownPosition); KnownPosition.X += (float)Velocity.X; KnownPosition.Y += (float)Velocity.Y; KnownPosition.Z += (float)Velocity.Z; bool onGround = IsOnGround(KnownPosition); if (!onGroundBefore && onGround) { KnownPosition.Y = (float)Math.Floor(oldPosition.Y); Velocity = Vector3.Zero; } else { Velocity *= (float)(1.0f - Drag); if (!onGround) { Velocity -= new Vector3(0, (float)Gravity, 0); } } LastUpdatedTime = DateTime.UtcNow; } else if (Velocity != Vector3.Zero) { Velocity = Vector3.Zero; LastUpdatedTime = DateTime.UtcNow; } }
public void BroadcastMove(bool forceMove = false) { //if (NoAi || forceMove) { //McpeMoveEntity moveEntity = McpeMoveEntity.CreateObject(); //moveEntity.runtimeEntityId = EntityId; //moveEntity.position = LastSentPosition; //moveEntity.flags = (short) (IsOnGround? 1: 0); //moveEntity.Encode(); //Level.RelayBroadcast(moveEntity); if (LastSentPosition != null) { McpeMoveEntityDelta move = McpeMoveEntityDelta.CreateObject(); move.runtimeEntityId = EntityId; move.prevSentPosition = LastSentPosition; move.currentPosition = (PlayerLocation)KnownPosition.Clone(); move.isOnGround = IsWalker && IsOnGround; if (move.SetFlags()) { Level.RelayBroadcast(move); } } LastSentPosition = (PlayerLocation)KnownPosition.Clone(); // Used for delta } }
/// <summary> /// For debugging of flight-path and rotation. /// </summary> private void BroadcastMoveAndMotion() { if (new Random().Next(5) == 0) { McpeSetEntityMotion motions = McpeSetEntityMotion.CreateObject(); motions.runtimeEntityId = EntityId; motions.velocity = Velocity; Level.RelayBroadcast(motions); } if (LastSentPosition != null) { McpeMoveEntityDelta move = McpeMoveEntityDelta.CreateObject(); move.runtimeEntityId = EntityId; move.prevSentPosition = LastSentPosition; move.currentPosition = (PlayerLocation)KnownPosition.Clone(); move.isOnGround = IsWalker && IsOnGround; if (move.SetFlags()) { Level.RelayBroadcast(move); } } LastSentPosition = (PlayerLocation)KnownPosition.Clone(); // Used for delta if (Shooter != null && IsCritical) { var particle = new CriticalParticle(Level); particle.Position = KnownPosition.ToVector3(); particle.Spawn(new[] { Shooter }); } }
public PlayerCountHologram(string name, Level level, PlayerLocation playerLocation, string gameName) : base(name, level, playerLocation) { _gameName = gameName; KnownPosition = (PlayerLocation)KnownPosition.Clone(); KnownPosition.Y += 2.8f; }
private void DoTeleport() { if (Shooter is Player shooter) { if (KnownPosition.Y > 0) { shooter.Teleport((PlayerLocation)KnownPosition.Clone()); shooter.HealthManager.TakeHit(this, CalculateDamage(shooter), DamageCause.Magic); } } }
public void BroadcastMove(bool forceMove = false) { if (NoAi || forceMove) { McpeMoveEntity moveEntity = McpeMoveEntity.CreateObject(); moveEntity.runtimeEntityId = EntityId; moveEntity.position = (PlayerLocation)KnownPosition.Clone(); moveEntity.Encode(); Level.RelayBroadcast(moveEntity); } }
public void SendPlayerPositionAndLook() { PlayerLocation loc = (PlayerLocation)KnownPosition.Clone(); PlayerPositionAndLookPacket packet = new PlayerPositionAndLookPacket() { Flags = 0, TeleportId = 0, X = loc.X, Y = loc.Y, Z = loc.Z, Yaw = loc.Yaw, Pitch = loc.Pitch }; Connection.SendPacket(packet); }
public override void SpawnToPlayers(Player[] players) { McpeAddItemEntity mcpeAddItemEntity = McpeAddItemEntity.CreateObject(); mcpeAddItemEntity.entityIdSelf = EntityId; mcpeAddItemEntity.runtimeEntityId = EntityId; mcpeAddItemEntity.item = GetItemStack(); mcpeAddItemEntity.x = KnownPosition.X; mcpeAddItemEntity.y = KnownPosition.Y; mcpeAddItemEntity.z = KnownPosition.Z; mcpeAddItemEntity.speedX = Velocity.X; mcpeAddItemEntity.speedY = Velocity.Y; mcpeAddItemEntity.speedZ = Velocity.Z; mcpeAddItemEntity.metadata = GetMetadata(); LastSentPosition = (PlayerLocation)KnownPosition.Clone(); Level.RelayBroadcast(players, mcpeAddItemEntity); BroadcastSetEntityData(); }
public override void OnTick() { base.OnTick(); if (Velocity.Distance > 0) { PlayerLocation oldPosition = (PlayerLocation)KnownPosition.Clone(); var onGroundBefore = IsOnGround(KnownPosition); KnownPosition.X += (float)Velocity.X; KnownPosition.Y += (float)Velocity.Y; KnownPosition.Z += (float)Velocity.Z; var onGround = IsOnGround(KnownPosition); if (!onGroundBefore && onGround) { KnownPosition.Y = (float)Math.Floor(oldPosition.Y); Velocity = Vector3.Zero; } else { Velocity *= (1.0 - Drag); Velocity -= new Vector3(0, Gravity, 0); } } McpeMoveEntity moveEntity = McpeMoveEntity.CreateObject(); moveEntity.entities = new EntityLocations { { EntityId, KnownPosition } }; Level.RelayBroadcast(moveEntity); McpeSetEntityMotion motions = McpeSetEntityMotion.CreateObject(); motions.entities = new EntityMotions { { EntityId, Velocity } }; Level.RelayBroadcast(motions); }
public override void OnTick() { base.OnTick(); if (KnownPosition.Y <= 0 || (Velocity.Distance <= 0 && DespawnOnImpact) || (Velocity.Distance <= 0 && !DespawnOnImpact && Ttl <= 0)) { DespawnEntity(); return; } Ttl--; if (KnownPosition.Y <= 0 || Velocity.Distance <= 0) { return; } Entity entityCollided = CheckEntityCollide(KnownPosition.ToVector3(), Velocity); bool collided = false; if (entityCollided != null) { double speed = Math.Sqrt(Velocity.X * Velocity.X + Velocity.Y * Velocity.Y + Velocity.Z * Velocity.Z); double damage = Math.Ceiling(speed * Damage); if (IsCritical) { damage += Level.Random.Next((int)(damage / 2 + 2)); McpeAnimate animate = McpeAnimate.CreateObject(); animate.entityId = entityCollided.EntityId; animate.actionId = 4; Level.RelayBroadcast(animate); } Player player = entityCollided as Player; if (player != null) { damage = player.CalculatePlayerDamage(player, damage); } entityCollided.HealthManager.TakeHit(this, (int)damage, DamageCause.Projectile); entityCollided.HealthManager.LastDamageSource = Shooter; collided = true; } else { var velocity2 = Velocity; velocity2 *= (1.0d - Drag); velocity2 -= new Vector3(0, Gravity, 0); double distance = velocity2.Distance; velocity2 = velocity2.Normalize() / 2; for (int i = 0; i < Math.Ceiling(distance) * 2; i++) { PlayerLocation nextPos = (PlayerLocation)KnownPosition.Clone(); nextPos.X += (float)velocity2.X * i; nextPos.Y += (float)velocity2.Y * i; nextPos.Z += (float)velocity2.Z * i; BlockCoordinates coord = new BlockCoordinates(nextPos); Block block = Level.GetBlock(coord); collided = block.Id != 0 && (block.GetBoundingBox()).Contains(nextPos.ToVector3()); if (collided) { SetIntersectLocation(block.GetBoundingBox(), KnownPosition); break; } } } if (collided) { Velocity = Vector3.Zero; } else { KnownPosition.X += (float)Velocity.X; KnownPosition.Y += (float)Velocity.Y; KnownPosition.Z += (float)Velocity.Z; Velocity *= (1.0 - Drag); Velocity -= new Vector3(0, Gravity, 0); KnownPosition.Yaw = (float)Velocity.GetYaw(); KnownPosition.Pitch = (float)Velocity.GetPitch(); } // For debugging of flight-path if (BroadcastMovement) { BroadcastMoveAndMotion(); } }
public override void OnTick() { base.OnTick(); if (KnownPosition.Y <= 0 || (Velocity.Distance <= 0 && DespawnOnImpact) || (Velocity.Distance <= 0 && !DespawnOnImpact && Ttl <= 0)) { DespawnEntity(); return; } Ttl--; if (KnownPosition.Y <= 0 || Velocity.Distance <= 0) { return; } Entity entityCollided = CheckEntityCollide(KnownPosition.ToVector3(), Velocity); bool collided = false; if (entityCollided != null) { entityCollided.HealthManager.TakeHit(this, 2, DamageCause.Projectile); collided = true; } else { //collided = CheckBlockCollide(KnownPosition); if (!collided) { var velocity2 = Velocity; velocity2 *= (1.0d - Drag); velocity2 -= new Vector3(0, Gravity, 0); double distance = velocity2.Distance; velocity2 = velocity2.Normalize() / 2; for (int i = 0; i < Math.Ceiling(distance) * 2; i++) { PlayerLocation nextPos = (PlayerLocation)KnownPosition.Clone(); nextPos.X += (float)velocity2.X * i; nextPos.Y += (float)velocity2.Y * i; nextPos.Z += (float)velocity2.Z * i; BlockCoordinates coord = new BlockCoordinates(nextPos); Block block = Level.GetBlock(coord); collided = block.Id != 0 && (block.GetBoundingBox()).Contains(nextPos.ToVector3()); if (collided) { var substBlock = new Block(57) { Coordinates = block.Coordinates }; //Level.SetBlock(substBlock); //KnownPosition = nextPos; SetIntersectLocation(block.GetBoundingBox(), KnownPosition); break; } } } } if (collided) { Velocity = Vector3.Zero; } else { KnownPosition.X += (float)Velocity.X; KnownPosition.Y += (float)Velocity.Y; KnownPosition.Z += (float)Velocity.Z; Velocity *= (1.0 - Drag); Velocity -= new Vector3(0, Gravity, 0); //var k = Math.Sqrt((Velocity.X*Velocity.X) + (Velocity.Z*Velocity.Z)); //KnownPosition.Yaw = (float) Vector3.RadiansToDegrees(Math.Atan2(Velocity.X, Velocity.Z)); //KnownPosition.Pitch = (float) Vector3.RadiansToDegrees(Math.Atan2(Velocity.Y, k)); KnownPosition.Yaw = (float)Velocity.GetYaw(); KnownPosition.Pitch = (float)Velocity.GetPitch(); } // For debugging of flight-path if (BroadcastMovement) { BroadcastMoveAndMotion(); } }
public override void OnTick() { base.OnTick(); if (Ttl <= 0 || KnownPosition.Y <= 0 || Collided) { DespawnEntity(); } Ttl--; Entity entityCollided = CheckEntityCollide(KnownPosition.ToVector3(), Velocity); bool collided = false; if (entityCollided != null) { entityCollided.HealthManager.TakeHit(Shooter, 2, DamageCause.Projectile); collided = true; } else { //collided = CheckBlockCollide(KnownPosition); if (!collided) { var velocity2 = Velocity; velocity2 *= (1.0d - Drag); velocity2 -= new Vector3(0, Gravity, 0); double distance = velocity2.Distance; velocity2 = velocity2.Normalize() / 2; for (int i = 0; i < Math.Ceiling(distance) * 2; i++) { PlayerLocation nextPos = (PlayerLocation)KnownPosition.Clone(); nextPos.X += (float)velocity2.X * i; nextPos.Y += (float)velocity2.Y * i; nextPos.Z += (float)velocity2.Z * i; Vector3 coord = new Vector3(nextPos.ToVector3()); Block block = Level.GetBlock(coord); collided = block.Id != 0 && (block.GetBoundingBox()).Contains(nextPos.ToVector3()); if (collided) { var substBlock = new Block(57) { Coordinates = block.Coordinates }; Level.SetBlock(substBlock); KnownPosition = nextPos; SetIntersectLocation(block.GetBoundingBox(), KnownPosition); break; } } } } if (collided) { Collided = collided; Velocity = Vector3.Zero; } else { KnownPosition.X += (float)Velocity.X; KnownPosition.Y += (float)Velocity.Y; KnownPosition.Z += (float)Velocity.Z; Velocity *= (1.0 - Drag); Velocity -= new Vector3(0, Gravity, 0); var k = Math.Sqrt((Velocity.X * Velocity.X) + (Velocity.Z * Velocity.Z)); KnownPosition.Yaw = (float)(Math.Atan2(Velocity.X, Velocity.Z) * 180f / Math.PI); KnownPosition.Pitch = (float)(Math.Atan2(Velocity.Y, k) * 180f / Math.PI); } BroadcastMoveAndMotion(); }