private void MovementPrediction(Vector2 ClientsidePos) { if (ClientsidePos != Entity.Position) { //If error is too big, Vector2 recieved = LoginManagerClient.GetCharacter().physicalPosition; float diff = recieved.Length() - ClientsidePos.Length(); //Console.WriteLine(diff); if (Math.Abs(diff) > 25) { LoginManagerClient.GetCharacter()._pos = recieved; ClientsidePos = recieved; } if (Entity.Position.Length() < 1250) { //player.Position = new Vector2(MathHelper.Lerp(player.Position.X, ClientsidePos.X, 0.1f), MathHelper.Lerp(player.Position.Y, ClientsidePos.Y, 0.1f)); Entity.Position = ClientsidePos; } else { Entity.Position = ClientsidePos; } } }
public void SetupTiles() { if (Core.Scene == null) { return; } MapList = FileManager.GetMapInformation("Data/" + ConstantValues.MapDataFileName); Map.Map map = null; CharacterPlayer c = LoginManagerClient.GetCharacter(); foreach (var maps in MapList) { if (maps.MapName == c.LastMultiLocation) { map = maps; } } if (map != null) { entity = Scene.CreateEntity(map.MapName); TiledMapRenderer tmr = entity.AddComponent(new TiledMapRenderer(map.TmxMap)); tmr.Material = new Material(BlendState.NonPremultiplied); tmr.SetRenderLayer(1); tmr.SetLayersToRender(new string[] { "Tile", "Collision", "Decoration", "CustomCollision" }); TmxObjectGroup l = map.TmxMap.GetLayer <TmxObjectGroup>("Objects"); foreach (TmxObject obj in l.Objects) { ObjectSceneEntity osc = new ObjectSceneEntity(obj, tmr.TiledMap.TileWidth); osc.SetPosition(osc.Position); Scene.AddEntity(osc); } } }
private void Sendregister(Button obj) { string usr = textFieldu.GetText(); string pass = CryptoManager.ToHash(textFieldp.GetText()); if (string.IsNullOrWhiteSpace(usr) || string.IsNullOrEmpty(usr)) { return; } LoginManagerClient loginManagerClient = new LoginManagerClient(usr, pass); if (ClientNetworkManager.client != null) { MessageManager.SetLoginManagerClient(loginManagerClient); loginManagerClient.SetUniqueID(ClientNetworkManager.client.UniqueIdentifier); if (ClientNetworkManager.client.ServerConnection == null) { ClientNetworkManager.TryToConnect(loginManagerClient); } MessageManager.SendRegisterRequest(); } }
public override void Update() { if (player != null) { player = LoginManagerClient.GetCharacter(); HealthtextComponent.SetText(player.CurrentHealth.ToString()); } base.Update(); }
private void AddItems(Skin skin) { var textButtonStyle = new ImageButtonStyle { Up = new PrimitiveDrawable(Color.DarkGray, 6, 2), Over = new PrimitiveDrawable(Color.DimGray), Down = new PrimitiveDrawable(Color.DimGray), PressedOffsetX = 0, PressedOffsetY = 0 }; int count = 0; foreach (var i in LoginManagerClient.GetCharacter().Equipment) { float f = GetWidth(); if (count % 5 == 0) { Row(); } if (i != null && i.GetSprite() == null) { i.SetSprite(TextureContainer.GetSpriteAtlasByName("Items").GetSprite(i.TextureName)); } if (i != null) { SpriteDrawable spd = new SpriteDrawable(i.GetSprite()); NinePatchDrawable ninePatchDrawable = new NinePatchDrawable(i.GetSprite(), 0, 0, 0, 0) { MinHeight = 48, MinWidth = 48 }; ItemButton imButton = new ItemButton(ninePatchDrawable, count, i); imButton.SetTouchable(Touchable.Enabled); imButton.OnHovered += delegate { OnHovered(imButton, i, skin, stage); }; imButton.OnExited += delegate { OnExit(imButton, stage); }; imButton.OnMoved += delegate { OnMovedAndHovered(imButton); }; imButton.OnClicked += delegate { OnClickedCharacter(imButton); }; Add(imButton).Size(48, 48).Pad(4).Expand(); } else { ItemButton imButton = new ItemButton(textButtonStyle, count, null); imButton.Add(new Label("")).Expand().Left().SetAlign(Nez.UI.Align.BottomLeft); imButton.SetTouchable(Touchable.Disabled); Add(imButton).Size(48, 48).Pad(4).Expand(); } count++; } }
//TODO: Put into their own functions private void CustomMessage(NetIncomingMessage message) { MessageTemplate template; string ms = message.ReadString(); if (ms != null) { template = Newtonsoft.Json.JsonConvert.DeserializeObject <MessageTemplate>(ms); switch (template.MessageType) { //TODO: Put These into functions case MessageType.LoginSuccess: Debug.WriteLine("Login and recieved sucessfully!"); MainScene mainScene = new MainScene(); CharacterPlayer player = Newtonsoft.Json.JsonConvert.DeserializeObject <CharacterPlayer>(template.JsonMessage); MessageManager.GetLoginManagerClient().SetCharacter(player); mainScene.player = PlayerManager.CreatePlayer(player, mainScene); FollowCamera fCamera = new FollowCamera(mainScene.player, FollowCamera.CameraStyle.CameraWindow) { FollowLerp = 0.3f }; mainScene.player.AddComponent(fCamera); Core.StartSceneTransition(new FadeTransition(() => mainScene)); break; case MessageType.LoginFailure: Debug.WriteLine("Failed to login! Try again"); break; case MessageType.Movement: Vector2 vector2 = Newtonsoft.Json.JsonConvert.DeserializeObject <Vector2>(template.JsonMessage); LoginManagerClient.GetCharacter().MoveToPos(vector2); break; case MessageType.GameUpdate: GameUpdateState(template.JsonMessage); break; case MessageType.MapChange: TileSceneComponent tsc = Scene.GetSceneComponent <TileSceneComponent>(); Map.Map map = tsc.MapList.FirstOrDefault(m => m.MapName.Equals(template.JsonMessage)); tsc.ChangeMap(map); break; } } }
private void CheckConnection() { if (ClientNetworkManager.connection == null) { return; } framesPassed++; if (ClientNetworkManager.connection.Status == NetConnectionStatus.Disconnected && framesPassed > 10) { attempts++; if (!ClientNetworkManager.TryToConnect(MessageManager.GetLoginManagerClient())) { Debug.WriteLine("Attemps: " + attempts); } else { attempts = 0; } if (MessageManager.GetLoginManagerClient() != null || LoginManagerClient.GetCharacter() == null) { MessageManager.SendLoginRequest(); } } }
private void GameUpdateState(string jsonMessage) { DataTemplate dataTemplate = Newtonsoft.Json.JsonConvert.DeserializeObject <DataTemplate>(jsonMessage); //Check for changes bool updateInv = false; if (Scene is MainScene && LoginManagerClient.GetCharacter() != null) { updateInv = CheckIfNeedToUpdate(LoginManagerClient.GetCharacter(), dataTemplate.RecieverCharacter); } //the recieved position LoginManagerClient.SetCharacterStatic(dataTemplate.RecieverCharacter); LoginManagerClient.SetRecievedPosition(dataTemplate.RecieverCharacter._pos); LoginManagerClient.GetCharacter().physicalPosition = dataTemplate.RecieverCharacter.physicalPosition; //Change UI from new data if (updateInv) { Stage stage = (Scene as MainScene).UICanvas.Stage; List <InventoryWindow> invlist = stage.FindAllElementsOfType <InventoryWindow>(); List <CharacterWindow> charlist = stage.FindAllElementsOfType <CharacterWindow>(); Vector2 invpos = new Vector2(-1, -1); Vector2 charpos = new Vector2(-1, -1); float invwidth, invheight, charw, charh; invheight = invwidth = charw = charh = -1; if (invlist.Count > 0) { invpos = new Vector2(invlist[0].GetX(), invlist[0].GetY()); invheight = invlist[0].GetHeight(); invwidth = invlist[0].GetWidth(); } if (charlist.Count > 0) { charpos = new Vector2(charlist[0].GetX(), charlist[0].GetY()); charw = charlist[0].GetWidth(); charh = charlist[0].GetHeight(); } UIManager.GenerateInventoryWindow(InputComponent.skin, Scene, invpos, invwidth, invheight); UIManager.GenerateCharacterWindow(InputComponent.skin, Scene, charpos, charw, charh); } //removes the caracters that are not close to the player LoginManagerClient.OtherCharacters.RemoveAll(tempc => !dataTemplate.OthersCharacters.Contains(tempc)); //add other characters in the area to a list if (dataTemplate.OthersCharacters != null && dataTemplate.OthersCharacters.Count > 0) { foreach (CharacterPlayer charac in dataTemplate.OthersCharacters) { int i = LoginManagerClient.OtherCharacters.FindIndex(tempc => tempc._name.Equals(charac._name)); if (i == -1) { LoginManagerClient.OtherCharacters.Add(charac); } else { CharacterPlayer character = dataTemplate.OthersCharacters.Find(tempc => tempc._name.Equals(charac._name)); LoginManagerClient.OtherCharacters[i] = character; //Console.WriteLine(LoginManagerClient.OtherCharacters[i].CurrentHealth.ToString()); } } } }
private void FixedUpdate() { Vector2 ClientsidePos = LoginManagerClient.GetCharacter().physicalPosition; MovementPrediction(ClientsidePos); }
private Keys[] KeyboardFixed() { KeyboardState newState = Input.CurrentKeyboardState; KeyboardState OldKeyboardState = Input.PreviousKeyboardState; List <Keys> keys = new List <Keys>(); float speed = 500; var dir = Vector2.Zero; //might be usable later for abilities and more. if (newState.IsKeyDown(Keys.T) && !OldKeyboardState.IsKeyDown(Keys.T)) { keys.Add(Keys.T); } //Generated inventory if (newState.IsKeyDown(Keys.I) && OldKeyboardState.IsKeyUp(Keys.I)) { keys.Add(Keys.I); } if (newState.IsKeyDown(Keys.C) && OldKeyboardState.IsKeyUp(Keys.C)) { keys.Add(Keys.C); } if (newState.IsKeyDown(Keys.S) && !OldKeyboardState.IsKeyDown(Keys.S)) { IsMoving = true; } else if (newState.IsKeyDown(Keys.S) && OldKeyboardState.IsKeyDown(Keys.S)) { // the player is holding the key down //LoginManagerClient.GetCharacter()._pos.Y += 3*60f * Time.DeltaTime; //LoginManagerClient.GetCharacter()._pos.Y += 1f; keys.Add(Keys.S); dir.Y = 1f; IsMoving = true; } else if (!newState.IsKeyDown(Keys.S) && OldKeyboardState.IsKeyDown(Keys.S)) { // the player was holding the key down, but has just let it go } if (newState.IsKeyDown(Keys.W) && !OldKeyboardState.IsKeyDown(Keys.W)) { IsMoving = true; } else if (newState.IsKeyDown(Keys.W) && OldKeyboardState.IsKeyDown(Keys.W)) { // the player is holding the key down //LoginManagerClient.GetCharacter()._pos.Y -= 3*60f * Time.DeltaTime; //LoginManagerClient.GetCharacter()._pos.Y -= 1f; dir.Y = -1f; keys.Add(Keys.W); IsMoving = true; } else if (!newState.IsKeyDown(Keys.W) && OldKeyboardState.IsKeyDown(Keys.W)) { // the player was holding the key down, but has just let it go } if (newState.IsKeyDown(Keys.A) && !OldKeyboardState.IsKeyDown(Keys.A)) { IsMoving = true; } else if (newState.IsKeyDown(Keys.A) && OldKeyboardState.IsKeyDown(Keys.A)) { // the player is holding the key down //LoginManagerClient.GetCharacter()._pos.X -= 3*60f * Time.DeltaTime; //LoginManagerClient.GetCharacter()._pos.X -= 1f; keys.Add(Keys.A); dir.X = -1f; IsMoving = true; } else if (!newState.IsKeyDown(Keys.A) && OldKeyboardState.IsKeyDown(Keys.A)) { // the player was holding the key down, but has just let it go } if (newState.IsKeyDown(Keys.D) && !OldKeyboardState.IsKeyDown(Keys.D)) { IsMoving = true; } else if (newState.IsKeyDown(Keys.D) && OldKeyboardState.IsKeyDown(Keys.D)) { // the player is holding the key down //LoginManagerClient.GetCharacter()._pos.X += 3*60f * Time.DeltaTime; //LoginManagerClient.GetCharacter()._pos.X += 1f; keys.Add(Keys.D); dir.X = 1f; IsMoving = true; } else if (!newState.IsKeyDown(Keys.D) && OldKeyboardState.IsKeyDown(Keys.D)) { // the player was holding the key down, but has just let it go } var movement = dir * speed * Time.DeltaTime; if (movement != Vector2.Zero) { LoginManagerClient.GetCharacter()._pos += movement; } return(keys.ToArray()); }
public static void SetLoginManagerClient(LoginManagerClient _login) { login = _login; }