private void OnPlayerJoined(CharacterJoinedPacket packet) { Debug.Log($"[Client] Player joined: {packet.UserName}"); var clientCharacter = new ClientCharacter(packet.CharacterPacket.Id, packet.UserName); _clientManager.AddCharadcter(packet.CharacterPacket.Id, clientCharacter); }
protected override void OnHandle(IPacket packet, ClientWorldState worldState) { MoveItemServerPacket incomingPacket = (MoveItemServerPacket)packet; ClientCharacter mobileToUpdate = (ClientCharacter)worldState.GetCharacter(incomingPacket.CharacterID); Item item = (Item)worldState.GetItem(incomingPacket.ItemID); if (mobileToUpdate == null) { worldState.MissingCharacters.Add(incomingPacket.CharacterID); } else if (item != null) { if (incomingPacket.MoveToID.HasValue) //(item.Data.IsWorldItem) { ContainerItem moveToContainer = (ContainerItem)worldState.GetItem(incomingPacket.MoveToID.Value); if (!mobileToUpdate.MoveItem(item, moveToContainer)) { //ToDo: Simulation missmatch, Force server Authority } } else if (incomingPacket.MoveToPosition.HasValue) { if (!mobileToUpdate.MoveItem(item, incomingPacket.MoveToPosition.Value)) { //ToDo: Simulation missmatch, Force server Authority } } } }
private void OnJoinAccept(JoinAcceptPacket packet) { Id = packet.Id; Debug.Log("[Client] OnJoinAccept : " + packet.Id); _lastServerTick = packet.ServerTick; var clientCharacter = new ClientCharacter(packet.Id, packet.UserName); _clientManager.AddCharadcter(packet.Id, clientCharacter); }
public void Update(GameTime gameTime) { if (!this.ConnectedToWorld) { return; } worldServerClient.Process(); if (moveToPacket != null) { worldServerClient.Send(moveToPacket); moveToPacket = null; } if (aimToPacket != null) { var toSendPacket = aimToPacket; aimToPacket = null; worldServerClient.Send(toSendPacket); } if (performAbilityPacket != null) { var toSendPacket = performAbilityPacket; performAbilityPacket = null; worldServerClient.Send(toSendPacket); } lock (serverMessages) { if (serverMessages.Count > 0) { var queue = serverMessages; serverMessages = new Queue <ServerMessage>(queue.Count); while (queue.Count > 0) { ServerMessage message = queue.Dequeue(); message.Duration -= gameTime.ElapsedGameTime.TotalMilliseconds; if (message.Duration > 0D) { serverMessages.Enqueue(message); } } } } if (WorldState.MissingCharacters.Count > 0) { List <int> charIdToCheck = WorldState.MissingCharacters; WorldState.MissingCharacters = new List <int>(10); foreach (int charID in charIdToCheck) { CharacterModel charData = dataContext.GetCharacter(charID); ClientCharacter character = WorldState.CreateCharacter(charData); //, dataContext.GetItem(charData.InventoryID)); WorldState.AddCharacter(character); } } }
private void AddDamageIndicator(ClientCharacter clientCharacter, int damageAmount) { var dmgEfx = new DamageTextEffect(); dmgEfx.Character = clientCharacter; dmgEfx.Text = string.Format("{0}", damageAmount); dmgEfx.Duration = 2000; dmgEfx.Color = Color.Red; DamageTextEffectList.Enqueue(dmgEfx); }
private void Character_AffectedByPower(object sender, AffectedByPowerEventArgs e) { if (sender == null) { return; } CharacterPower power = e.CharacterPower; ClientCharacter character = (ClientCharacter)sender; }
protected override void OnHandle(IPacket packet, ClientWorldState worldState) { PerformAbilityPacket incomingPacket = (PerformAbilityPacket)packet; ClientCharacter mobileToUpdate = (ClientCharacter)worldState.GetCharacter(incomingPacket.CharacterId); if (mobileToUpdate == null) { worldState.MissingCharacters.Add(incomingPacket.CharacterId); return; } worldState.PerformAbility(incomingPacket.AbilityUsed, mobileToUpdate); }
protected override void OnHandle(IPacket packet, ClientWorldState worldState) { MoveToPacket incomingPacket = (MoveToPacket)packet; ClientCharacter mobileToUpdate = (ClientCharacter)worldState.GetCharacter(incomingPacket.MobileId); if (mobileToUpdate == null) { worldState.MissingCharacters.Add(incomingPacket.MobileId); return; } mobileToUpdate.ServerMoveToRecived(new Point(incomingPacket.X, incomingPacket.Y)); }
protected override void OnHandle(IPacket packet, ClientWorldState worldState) { StatsChangedPacket incomingPacket = (StatsChangedPacket)packet; ClientCharacter mobileToUpdate = (ClientCharacter)worldState.GetCharacter(incomingPacket.MobileId); if (mobileToUpdate == null) { worldState.MissingCharacters.Add(incomingPacket.MobileId); return; } mobileToUpdate.ServerStatsRecived(incomingPacket.Health, incomingPacket.Energy); }
private void Character_HealthChanged(Character character, StatChangedEventArgs e) { ClientCharacter clientCharacter = (ClientCharacter)character; if (e.Value <= e.PreviousValue) //Damage { this.AddDamageIndicator(clientCharacter, e.PreviousValue - e.Value); effectManager.AddEffect(new BloodEffect(clientCharacter.Position)); } else //Healing { this.AddHealIndicator(clientCharacter, e.Value - e.PreviousValue); } }
internal void AddChatMessage(int mobileId, string message) { ClientCharacter clientCharacter = (ClientCharacter)this.GetCharacter(mobileId); clientCharacter.SentMessage(); clientCharacter.IsWritingMessage = false; ChatMessage chatMessage = new ChatMessage(); chatMessage.Text = message; chatMessage.Duration = 2000 + message.Length * 50; chatMessage.Owner = clientCharacter; this.ChatMessages.Add(chatMessage); }
internal void ClientUpdate(GameTime gameTime) { Vector2 centerVector2 = new Vector2(960f, 540f); float lerpAmount = (float)(gameTime.ElapsedGameTime.TotalMilliseconds / WorldPump.Interval); if (this.PlayerCharacter.Position != PlayerCharacter.DrawPosition) { Vector2 drawPosition = Vector2.Lerp(PlayerCharacter.DrawPosition.ToVector2(), this.PlayerCharacter.Position.ToVector2(), lerpAmount); PlayerCharacter.DrawPosition = drawPosition.ToPoint(); PlayerCharacter.OldDrawPosition = PlayerCharacter.DrawPosition.ToVector2(); } foreach (ushort clientId in Characters) { if (PlayerCharacter.Id == clientId) { continue; } ClientCharacter client = (ClientCharacter)this.GetCharacter(clientId); //Vector2 realClientPosition = centerVector2 - (this.PlayerCharacter.Position - client.Position).ToVector2(); //new Vector2(this.PlayerCharacter.Position.X - client.Position.X, this.PlayerCharacter.Position.Y - client.Position.Y); if (client.Position != client.DrawPosition) { Vector2 drawPosition = Vector2.Lerp(client.DrawPosition.ToVector2(), client.Position.ToVector2(), lerpAmount); client.DrawPosition = drawPosition.ToPoint(); client.OldDrawPosition = PlayerCharacter.DrawPosition.ToVector2(); //client.OldDrawPosition = Vector2.Lerp(client.OldDrawPosition, realClientPosition, lerpAmount); } } if (this.ChatMessages.Count > 0) { Queue <ChatMessage> expiredMessage = new Queue <ChatMessage>(this.ChatMessages.Count); foreach (ChatMessage message in this.ChatMessages) { message.Duration -= gameTime.ElapsedGameTime.TotalMilliseconds; if (message.Duration <= 0D) { expiredMessage.Enqueue(message); } } while (expiredMessage.Count > 0) { this.ChatMessages.Remove(expiredMessage.Dequeue()); } } }
protected override void OnHandle(IPacket packet, ClientWorldState worldState) { UseItemPacket incomingPacket = (UseItemPacket)packet; ClientCharacter mobileToUpdate = (ClientCharacter)worldState.GetCharacter(incomingPacket.MobileId); Item itemToUse = worldState.GetItem(incomingPacket.ItemId); if (mobileToUpdate == null) { worldState.MissingCharacters.Add(incomingPacket.MobileId); } if (mobileToUpdate != null && itemToUse != null) { if (itemToUse.IsWorldItem) { if (!mobileToUpdate.PickupItem(itemToUse)) { //ToDo: Simulation missmatch, Force server Authority } } else { if (itemToUse.Category == ItemCategory.Consumable) { ConsumableItem itemToConsume = (ConsumableItem)itemToUse; if (!itemToConsume.Use(mobileToUpdate, worldState)) { //ToDo: Simulation missmatch, Force server Authority } } else if (itemToUse.Category == ItemCategory.Armor || itemToUse.Category == ItemCategory.Weapon) { if (!mobileToUpdate.Equip(itemToUse)) { //ToDo: Sim missmatch, force server Auth } } } } }
internal ClientCharacter CreateCharacter(CharacterModel charData) //, ItemData inventoryData) { ClientCharacter character = new ClientCharacter(charData.Id, charData.WorldLocation); character.Stats.Health = charData.Health; character.Stats.Energy = charData.Energy; foreach (var power in charData.Powers) { character.Learn((CharacterPowerIdentity)power); } if (charData.RightHandId.HasValue) { character.RightHand = (WeaponItem)this.GetItem(charData.RightHandId.Value); } if (charData.LeftHandId.HasValue) { character.LeftHand = (WeaponItem)this.GetItem(charData.RightHandId.Value); } if (charData.ArmorId.HasValue) { character.Armor = (ArmorItem)this.GetItem(charData.ArmorId.Value); } BagClientItem inventory = (BagClientItem)this.GetItem(charData.InventoryId); character.Inventory = inventory; //character.InventoryData = inventory.Data; //inventoryData; //dataContext.GetItem(charData.InventoryID); //if (character.InventoryData != null) // character.Inventory = inventory; //(BagClientItem)this.CreateItem(character.InventoryData); //world.AddCharacter(character); //this.AddItem(character.Inventory); return(character); }
internal void DropItem(ClientCharacter playerCharacter, IItem item) { MoveItemPacket packet = new MoveItemPacket(item.Id, playerCharacter.Position); worldServerClient.Send(packet); }
internal void SetChatState(int mobileId, bool state) { ClientCharacter clientCharacter = (ClientCharacter)this.GetCharacter(mobileId); clientCharacter.IsWritingMessage = state; }
private void Character_PerformsPower(object sender, PerformsPowerEventArgs e) { if (sender == null) { return; } CharacterPower power = e.CharacterPower; ClientCharacter character = (ClientCharacter)sender; switch (power.Id) { case CharacterPowerIdentity.DefaultAttack: effectManager.AddEffect(new SwingEffect(this.GetScreenPostion(character.DrawPosition), this.GetScreenPostion(character.AimToPosition), character.IsEquiped(ItemIdentity.Bow))); break; case CharacterPowerIdentity.HardAttack: effectManager.AddEffect(new SwingEffect(this.GetScreenPostion(character.DrawPosition), this.GetScreenPostion(character.AimToPosition), character.IsEquiped(ItemIdentity.Bow))); break; case CharacterPowerIdentity.CriticalAttack: break; case CharacterPowerIdentity.StunAttack: break; case CharacterPowerIdentity.SlowingAttack: break; case CharacterPowerIdentity.DecreaseEnergyCost: break; case CharacterPowerIdentity.IncreaseEnergyCost: break; case CharacterPowerIdentity.DecreaseDuration: break; case CharacterPowerIdentity.IncreaseDuration: break; case CharacterPowerIdentity.Meditation: break; case CharacterPowerIdentity.AbsorbDamage: break; case CharacterPowerIdentity.DeflectDamage: break; case CharacterPowerIdentity.ShortSpeedBurst: break; case CharacterPowerIdentity.IncreaseSpeed: break; case CharacterPowerIdentity.IncreaseMaxHealth: break; case CharacterPowerIdentity.IncreaseMaxEnergy: break; case CharacterPowerIdentity.IncreaseHealthRegen: break; case CharacterPowerIdentity.IncreaseEnergyRegen: break; case CharacterPowerIdentity.Stealth: break; case CharacterPowerIdentity.IncreaseSwordPower: break; case CharacterPowerIdentity.IncreaseBowPower: break; case CharacterPowerIdentity.IncreasePlateArmor: break; case CharacterPowerIdentity.IncreaseLeatherArmor: break; case CharacterPowerIdentity.IncreaseMaxWeight: break; case CharacterPowerIdentity.Interrupt: break; default: break; } }
private void DrawCharacters(SpriteBatch spriteBatch) { Vector2 centerHead = headTexture.Bounds.Center.ToVector2(); Vector2 centerBody = bodyTexture.Bounds.Center.ToVector2(); centerBody.Y = 0f; Texture2D bodyTextureToUse, headTextureToUse; foreach (ushort id in world.Characters) { //if (id == world.PlayerCharacter.Id) // continue; bodyTextureToUse = bodyTexture; headTextureToUse = headTexture; ClientCharacter charToDraw = (ClientCharacter)world.GetCharacter(id); Vector2 charToDrawDirection = charToDraw.AimToPosition.ToVector2() - charToDraw.Position.ToVector2(); Vector2 bodyMovingBobPosition = Vector2.Zero; if (charToDraw.IsMoving) { bodyMovingBobPosition = movementBodyBobEffect.PositioinBob; } Vector2 bodyRotationPosition = charToDrawDirection; if (bodyRotationPosition != Vector2.Zero) { bodyRotationPosition.Normalize(); } bodyRotationPosition.X *= 2f; if (bodyRotationPosition.Y < 0f) { bodyRotationPosition.Y *= 10f; } if (charToDraw.IsDead) { bodyTextureToUse = bodyGhostTexture; headTextureToUse = headGhostTexture; } var distance = Vector2.Distance(world.PlayerCharacter.Position.ToVector2(), charToDraw.Position.ToVector2()); float visibility = charToDraw.Stats.GetVisibility(distance); if (visibility > 0f) { Color visibilityColor = Color.White * visibility; //Draw Body Vector2 clientScreenPostion = this.GetScreenPostion(charToDraw.DrawPosition).ToVector2(); spriteBatch.Draw(bodyTextureToUse, clientScreenPostion + bodyMovingBobPosition + bodyRotationPosition, null, visibilityColor, 0f, centerBody, 1f, SpriteEffects.None, 1f); if (charToDraw.Armor != null && !charToDraw.IsDead) { IArmorClientItem armorItem = (IArmorClientItem)charToDraw.Armor; spriteBatch.Draw(armorItem.Texture, clientScreenPostion + bodyMovingBobPosition + bodyRotationPosition, null, visibilityColor, 0f, new Vector2(armorItem.Texture.Bounds.Center.X, 0f), 1f, SpriteEffects.None, 1f); } //Draw Head spriteBatch.Draw(headTextureToUse, clientScreenPostion, null, visibilityColor, (float)world.VectorToRadian(charToDrawDirection), centerHead, 1f, SpriteEffects.None, 1f); //Draw Armor if (charToDraw.Armor != null && !charToDraw.IsDead) { IArmorClientItem armorItem = (IArmorClientItem)charToDraw.Armor; spriteBatch.Draw(armorItem.HeadTexture, clientScreenPostion, null, visibilityColor, (float)world.VectorToRadian(charToDrawDirection), centerHead, 1f, SpriteEffects.None, 1f); } //Draw Weapons if (charToDraw.RightHand != null && !charToDraw.IsDead) { IClientItem weaponItem = (IClientItem)charToDraw.RightHand; spriteBatch.Draw(weaponItem.Texture, (clientScreenPostion - (bodyMovingBobPosition * .5f)) + bodyRotationPosition + new Vector2(-weaponItem.Texture.Bounds.Width, weaponItem.Texture.Bounds.Center.Y * .5f), null, visibilityColor, -.5f, weaponItem.Texture.Bounds.Center.ToVector2(), 1f, SpriteEffects.None, 1f); } //Draw ChatIcon if (charToDraw.IsWritingMessage) { spriteBatch.Draw(chatIcon, clientScreenPostion + new Vector2(0f, -30f), null, visibilityColor, 0f, Vector2.Zero, 1f, SpriteEffects.None, 1f); } } } //Vector2 start = world.PlayerCharacter.Position.ToVector2(); //Vector2 end = world.PlayerCharacter.AimToPosition.ToVector2(); ////float distance = Vector2.Distance(start, end); //Vector2 direction = end - start; //Vector2 plrbodyMovingBobPosition = Vector2.Zero; //if (world.PlayerCharacter.IsMoving) // plrbodyMovingBobPosition = movementBodyBobEffect.PositioinBob; //Vector2 plrbodyRotationPosition = direction; //plrbodyRotationPosition.Normalize(); //plrbodyRotationPosition.X *= 2f; //if (plrbodyRotationPosition.Y < 0f) //plrbodyRotationPosition.Y *= 10f; //spriteBatch.Draw(bodyTexture, CenterScreenVector2 + plrbodyMovingBobPosition + plrbodyRotationPosition, null, Color.White, 0f, centerBody, 1f, SpriteEffects.None, 1f); //spriteBatch.Draw(headTexture, CenterScreenVector2, headTexture.Bounds, Color.White, (float)world.VectorToRadian(direction), centerHead, 1f, SpriteEffects.None, 1f); }
public bool LoadContent(ClientWorldState world) { if (!this.CharacterSelected) { worldServerClient.Disconnect(); return(false); } //CharacterData playerCharData = dataContext.GetCharacter(playerSelectableCharacter); IEnumerable <ItemModel> items = dataContext.GetItems(playerSelectableCharacter.MapId); if (items != null) { List <Item> hasContainerItem = new List <Item>(); foreach (ItemModel itemData in items) { Item item = world.CreateItem(itemData); if (item.ContainerId.HasValue) { hasContainerItem.Add(item); } world.AddItem(item); } foreach (Item item in hasContainerItem) { ContainerItem containerItem = (ContainerItem)world.GetItem(item.ContainerId.Value); if (containerItem != null) { containerItem.Items.Add(item); } } } //world.PlayerCharacter = new ClientCharacter(playerCharacterId, playerCharData.WorldLocation); ////world.PlayerCharacter.Id = playerCharacterId; //world.PlayerCharacter.Health = playerCharData.Health; //world.PlayerCharacter.Energy = playerCharData.Energy; //world.PlayerCharacter.InventoryData = dataContext.GetItem(playerCharData.InventoryID); //world.PlayerCharacter.Inventory = (BagClientItem)world.CreateItem(world.PlayerCharacter.InventoryData); //world.AddCharacter(world.PlayerCharacter); //world.AddItem(world.PlayerCharacter.Inventory); IEnumerable <CharacterModel> onlineCharacters = dataContext.GetCharacters(playerSelectableCharacter.MapId); if (onlineCharacters != null) { foreach (CharacterModel charData in onlineCharacters) { ClientCharacter character = world.CreateCharacter(charData); //, dataContext.GetItem(charData.InventoryID)); //new ClientCharacter(charData.CharacterDataID, charData.WorldLocation); ////world.PlayerCharacter.Id = playerCharacterId; //character.Health = playerCharData.Health; //character.Energy = playerCharData.Energy; //character.InventoryData = dataContext.GetItem(charData.InventoryID); //if (character.InventoryData != null) // character.Inventory = (BagClientItem)world.CreateItem(character.InventoryData); if (character.Id == playerSelectableCharacter.CharacterId) { world.PlayerCharacter = character; } if (character != null) { world.AddCharacter(character); } //world.AddItem(character.Inventory); } } if (world.PlayerCharacter == null) { worldServerClient.Disconnect(); return(false); } return(true); }
internal void PerformAbility(ClientCharacter playerCharacter, CharacterPowerIdentity abilityId) { performAbilityPacket = new PerformAbilityPacket(playerCharacter.Id, abilityId); //Packets are sent on Update to reduce Spam }