public void HandlePacket(Packet packet) { if (packet is RequestWorldPacket) { handler.SendPacket(new WorldPacket(legend.world)); } else if (packet is MoveAndFacePacket) { MoveAndFacePacket movePacket = (MoveAndFacePacket)packet; Position newPos = new Position(movePacket.x, movePacket.y); player.facing = (FACING)movePacket.facing; legend.world.MoveEntity(player, newPos, legend.config); if (player.pos != newPos) { handler.SendPacket(new PlayerPositionPacket(player.pos.x, player.pos.y)); } } else if (packet is MovePacket) { MovePacket movePacket = (MovePacket)packet; Position newPos = new Position(movePacket.x, movePacket.y); legend.world.MoveEntity(player, newPos, legend.config); if (player.pos != newPos) { handler.SendPacket(new PlayerPositionPacket(player.pos.x, player.pos.y)); } } else if (packet is SendMessagePacket) { SendMessagePacket messagePacket = (SendMessagePacket)packet; var msg = new ChatMessage(this.username, messagePacket.message, this.userId); legend.world.SendToNearby(msg, player.pos, legend.config); } else if (packet is EntityInteractPacket) { EntityInteractPacket interactPacket = (EntityInteractPacket)packet; TryInteract(interactPacket.interactType, interactPacket.guid); } else if (packet is GUIOptionPacket) { GUIOptionPacket optionPacket = (GUIOptionPacket)packet; if (dialogueOpen != null && dialogueOpen.HasOption(optionPacket.guid)) { Option option = dialogueOpen.GetOption(optionPacket.guid); //TODO: ENFORCE REQUIREMENTS if (option.IsDisplayed(player.flags)) { if (option is DialogueOption) { DialogueOption dialogueOption = (DialogueOption)option; OpenDialogue(dialogueOption.dialogueKey); } else if (option is EndDialogueOption) { handler.SendPacket(new CloseDialoguePacket(Guid.NewGuid())); } } else { //SMH Dirty cheater. handler.SendPacket(new CloseDialoguePacket(Guid.NewGuid())); } } } }
public override void _Process(float delta) { var camera = (Camera2D)GetParent().GetNode("Camera2D"); var tileMap = (TileMap)GetParent().GetNode("Tiles"); var bumpMap = (TileMap)GetParent().GetNode("BumpMap"); //bool doMove = focused && !moving; bool doMove = (focus == "GameFocus" && !moving); if (pos != prevPos) { var newPos = tileMap.MapToWorld(pos); SetPosition(newPos); camera.SetPosition(newPos); moving = false; } var target = new Vector2(pos); if (Input.IsActionPressed("ui_right") && doMove) { target.x += 1; facing = 3; moving = true; } if (Input.IsActionPressed("ui_left") && doMove) { target.x -= 1; facing = 0; moving = true; } if (Input.IsActionPressed("ui_down") && doMove) { target.y += 1; facing = 2; moving = true; } if (Input.IsActionPressed("ui_up") && doMove) { target.y -= 1; facing = 1; moving = true; } if (Input.IsActionJustPressed("debug_mode")) { debugMode = !debugMode; tileMap.SetVisible(!debugMode); bumpMap.SetVisible(debugMode); var debugNode = (Control)GetParent().GetParent().GetParent().GetNode("GUI").GetNode("Debug"); debugNode.SetVisible(debugMode); } if (Input.IsActionJustPressed("ui_interact")) { var interactTarget = new Vector2(pos); switch (facing) { case 0: interactTarget.x -= 1; break; case 1: interactTarget.y -= 1; break; case 2: interactTarget.y += 1; break; case 3: interactTarget.x += 1; break; } Entity interactEntity = (Entity)GetParent().GetParent().Call("getCollidedEntity", tileMap.MapToWorld(interactTarget)); if (interactEntity != null) { var interactEntityPacket = new EntityInteractPacket(0, Guid.Parse(interactEntity.uuid)); sendPacket(interactEntityPacket); } else { GD.Print("NO ENTITY AT ", interactTarget); } } if (Input.IsActionJustPressed("ui_open_inventory")) { Window window = (Window)GetNode("../../../GUI/Window"); InventoryPanel inventoryPanel = window.OpenInventoryPanel(); inventoryPanel.SetInventory(inventory); } if (Input.IsActionJustPressed("ui_close")) { Window window = (Window)GetNode("../../../GUI/Window"); window.ClosePanels(); } //GD.Print(new object[] {pos, " vs. ", target}); if (!pos.Equals(target) && moving) { var prevPos = tileMap.MapToWorld(pos); var newPos = tileMap.MapToWorld(target); if (bumpMap.GetCell((int)target.x, (int)target.y) != 0 && !(bool)GetParent().GetParent().Call("collidesWithEntity", newPos)) { var deltaPos = newPos - prevPos; moveDelta = deltaPos / moveTime; timeMoving = 0.0f; //GD.Print("Delta ", deltaPos); pos = target; var movePacket = new MoveAndFacePacket((int)pos.x, (int)pos.y, (int)facing); sendPacket(movePacket); } else { if (facing != prevFacing) { var movePacket = new MoveAndFacePacket((int)pos.x, (int)pos.y, (int)facing); sendPacket(movePacket); } } } var result = tileMap.MapToWorld(pos); //camera.SetPosition(result); var playerSprite = (AnimatedSprite)GetNode("PlayerSprite"); string animation = ""; switch (facing) { case 0: animation = "left"; break; case 1: animation = "up"; break; case 2: animation = "down"; break; case 3: animation = "right"; break; } if (moving) { animation += "_walk"; playerSprite.Play(); if ((timeMoving + delta) >= moveTime) { delta = moveTime - timeMoving; moving = false; } timeMoving += delta; MoveAndCollide(moveDelta * delta); camera.SetPosition(Position); } else { SetPosition(result); camera.SetPosition(result); } if (animation != prevAnim) { //GD.Print("Setting anim to " + animation); playerSprite.SetAnimation(animation); } prevPos = pos; prevAnim = animation; camera.SetPosition(Position); prevFacing = facing; }