public Commands GetPlayerCommands(int playerIndex, ref WarsContent warsContent, ref Microsoft.DirectX.DirectInput.JoystickState joystickState) { Commands commands = new Commands(); /* * if (true)//joystickState.X != 0) * { * Console.WriteLine("joystickState.X = {0}", joystickState.X); * } * if (true)//joystickState.Y != 0) * { * Console.WriteLine("joystickState.Y = {0}", joystickState.Y); * } */ byte[] buttons = joystickState.GetButtons(); /* * for (int i = 0; i < buttons.Length; i++) * { * if (buttons[i] >= 128) * { * Console.WriteLine("Button {0} pressed!", i); * } * } */ Ship me = warsContent.getPlayersShip(playerIndex); if (me.ShipType == Constants.SHIP_TYPE_MANUALLY_CONTROLLED) { if (Keyboard.GetState().IsKeyDown(Keys.A) || joystickState.X == 0) { commands.left = true; } if (Keyboard.GetState().IsKeyDown(Keys.D) || joystickState.X == 65535) { commands.right = true; } if (Keyboard.GetState().IsKeyDown(Keys.W) || buttons[4] >= 128 || buttons[5] >= 128 || buttons[6] >= 128 || buttons[7] >= 128) { commands.throttle = true; } if (Keyboard.GetState().IsKeyDown(Keys.Space) || Keyboard.GetState().IsKeyDown(Keys.P) || buttons[0] >= 128 || buttons[1] >= 128 || buttons[2] >= 128 || buttons[3] >= 128) { commands.fire = 1; } } else if (me.ShipType == Constants.SHIP_TYPE_COMPUTER_CONTROLLED) { //int left = random.Next(2); //Ship me = warsContent.getPlayersShip(playerIndex); Ship other = warsContent.getPlayersShip(me.FightingPlayer); if (me.Counter <= 0) // find new ship to fight { me.Counter = 3; int newPlayerToFight = FindNearestShip(playerIndex, ref warsContent); if (newPlayerToFight != 0 && (other = warsContent.getPlayersShip(newPlayerToFight)) != null) { me.FightingPlayer = newPlayerToFight; Console.WriteLine("New player to fight: {0}", newPlayerToFight); } //else //{ // other = warsContent.getPlayersShip(1); //} } if (other == null) { other = warsContent.getPlayersShip(1); } //Ship other = warsContent.getPlayersShip(me.FightingPlayer); //Vector2 toOtherVec; toOtherVec = new Vector2(); toOtherVec = other.Pos - me.Pos; Vector2 totSpeedVec = other.Speed - me.Speed; float toOtherAngle = GetVectorAngle(toOtherVec); Console.WriteLine("toOtherAngle: {0}", MathHelper.ToDegrees(toOtherAngle).ToString()); float fromOtherAngle = MathHelper.WrapAngle(toOtherAngle - MathHelper.Pi); float speedAngle = GetVectorAngle(totSpeedVec); float diffAngle = MathHelper.WrapAngle(speedAngle - fromOtherAngle); if (diffAngle != 0) { float speedLength = totSpeedVec.Length(); if (speedLength != 0) { double sinDiffAngle = Math.Sin(diffAngle); if (sinDiffAngle != 0) { double val = (double)(speedLength / Constants.REDSHOT_SPEED) * sinDiffAngle; compensationAngle = -(float)Math.Asin(val); } } } toOtherAngle += compensationAngle; //Console.WriteLine("compensationAngle: {0}", MathHelper.ToDegrees(compensationAngle)); //Console.WriteLine("compensationAngle: {0}", MathHelper.ToDegrees(compensationAngle)); //Console.WriteLine("GetVectorAngle: {0}, {1}", MathHelper.ToDegrees(toOtherAngle), MathHelper.ToDegrees(me.RotationAngle)); if (me.RotationAngle - toOtherAngle > Constants.SMALLEST_ANGLE_DIFF_ACCEPT) { if (me.RotationAngle - toOtherAngle < MathHelper.Pi) // Handle wrap-around ("normal" case == < Pi ==> right) { commands.right = true; } else { commands.left = true; } } else if (toOtherAngle - me.RotationAngle > Constants.SMALLEST_ANGLE_DIFF_ACCEPT) { if (toOtherAngle - me.RotationAngle < MathHelper.Pi) // Handle wrap-around ("normal" case == < Pi ==> left) { commands.left = true; } else { commands.right = true; } } if (toOtherVec.Length() > 400 && (me.Speed.Length() <= other.Speed.Length() || Math.Abs(GetVectorAngle(me.Speed) - GetVectorAngle(toOtherVec)) > MathHelper.PiOver4)) { commands.throttle = true; } if (random.Next(10) == 5) { commands.fire = 1; } } return(commands); }
} // onPaint().fim // [--- void verificarJoystick() { if (joystick == null) { return; } // Variáveis para guardar o estado das teclas int seta_esquerda = 0; int seta_direita = 0; int seta_cima = 0; int seta_abaixo = 0; // <b> DirectInput.JoystickState state = joystick.CurrentJoystickState; byte[] btn = state.GetButtons(); if (state.X < -40) { seta_esquerda = 1; } if (state.X > 1) { seta_direita = 1; } if (state.Y < -40) { seta_cima = 1; } if (state.Y > 0) { seta_abaixo = 1; } if (state.Z < -40) { seta_cima = 1; } if (state.Z > 1) { seta_abaixo = 1; } // </b> // Atualiza posicionamento do 'jogador' if (seta_abaixo == 1) { ylin += 5; } if (seta_cima == 1) { ylin -= 5; } if (seta_esquerda == 1) { xcol -= 5; } if (seta_direita == 1) { xcol += 5; } // Muda 'jogador' conforme seta pressionada if (seta_esquerda == 1) { jogador = "<(-:"; } if (seta_direita == 1) { jogador = ":-)>"; } // Aplique um reset se botão 1 for pressionando if (btn[1] > 0) { xcol = 320; ylin = 240; } // endif // Pressione os dois botões [0] e [1] do joystick para sair if ((btn[1] > 0) && (btn[0] > 1)) { terminar = true; } // Processa a tecla Escape if (terminar) { device.Dispose(); joystick.Dispose(); this.Close(); Application.Exit(); } // endif } // Atualizarjoystick()