private void InitializeAsGamePad(ScreenAccess ParentForm, InputDeviceAccess.TypeOfDevices NewTypeOfDevice) { //Check to see if this type of device is available and try to get it for this user try { //Grab first attached gamepad that isnt already assigned foreach (DirectInput.DeviceInstance CurrDeviceInstance in DirectInput.Manager.GetDevices(DirectInput.DeviceClass.GameControl, DirectInput.EnumDevicesFlags.AttachedOnly)) { if (InputDeviceAccess.IsFreeDeviceGuid(CurrDeviceInstance.InstanceGuid) == true) { this.device = new DirectInput.Device(CurrDeviceInstance.InstanceGuid); //If this device is dead throw an exception if (this.device == null) { throw new Exception("found a gamepad GUID, but when we tried to make a device from it it was null"); } else { break; } } } //throw an exception if there is no device if (this.device == null) { throw new Exception("A gamepad was assigned as an input device, but none were found."); } //Setup device this._TypeOfDevice = NewTypeOfDevice; this.device.SetDataFormat(DirectInput.DeviceDataFormat.Joystick); this.device.SetCooperativeLevel(ParentForm, DirectInput.CooperativeLevelFlags.Background | DirectInput.CooperativeLevelFlags.NonExclusive); this.device.Properties.AxisModeAbsolute = true; this.device.Acquire(); //Determine how many users are suited for this device this._DeviceUserMax = 1; // Get space in the keytable for the number of device users this.KeyTable = new System.Collections.Hashtable[this._DeviceUserMax]; //record guid as used InputDeviceAccess.AssignedGuids.Add(this.device.DeviceInformation.InstanceGuid); //Get the keys just so PressedKeys wont be null this.PollInput(); } catch (Exception err) { throw err; } }
private ControllerIdType InitializeDevice(InputDeviceAccess.TypeOfDevices TypeOfDevice, System.Collections.Hashtable NewKeyTable) { InputDeviceAccess CurrDevice = null; ControllerIdType NewControllerId = new ControllerIdType(); //If the desired device is a keyboard and another keyboard has available users // assign this user to the same keyboard. if (TypeOfDevice == InputDeviceAccess.TypeOfDevices.Keyboard) { foreach (InputDeviceAccess ExistingDevice in this.Devices) { if (ExistingDevice.TypeOfDevice == InputDeviceAccess.TypeOfDevices.Keyboard && ExistingDevice.DeviceUserCount < ExistingDevice.DeviceUserMax) { CurrDevice = ExistingDevice; } } } //create device if (CurrDevice == null) { CurrDevice = new InputDeviceAccess(this.ParentForm, TypeOfDevice); } //controller info: guid & device user id NewControllerId.DeviceGuid = CurrDevice.Guid; NewControllerId.DeviceUserId = CurrDevice.GetNextUserId(); //Save ControllerId and Device this.ControllerIds.Add(NewControllerId); this.Devices.Add(CurrDevice); //Send device new keytable CurrDevice.SetKeyTable(NewKeyTable, NewControllerId.DeviceUserId); //Poll the device to give it some keys to check CurrDevice.PollInput(); return(NewControllerId); }
public InputDeviceAccess GetInputDeviceRef(ControllerIdType ControllerWithGuidOfDeviceToReturn) { InputDeviceAccess DeviceToReturn = null; //Find the device with the guid and returns a reference to it foreach (InputDeviceAccess CurrInputDevice in this.Devices) { if (CurrInputDevice.Guid == ControllerWithGuidOfDeviceToReturn.DeviceGuid) { DeviceToReturn = CurrInputDevice; } } //Throw if the device was not found if (DeviceToReturn == null) { throw new System.Exception("the device manager was instructed to return a reference to a device with the Guid " + ControllerWithGuidOfDeviceToReturn.DeviceGuid.ToString() + " which is not an existing device"); } return(DeviceToReturn); }
private void ChangeStateLocal(BackgroundAccess Foreground) { //Have the device manager give me a ref to the device // that way I can poll and get keys from it directly InputDeviceAccess CurrInputDevice = this.ParentInputDeviceManager.GetInputDeviceRef(this.ControllerId); CurrInputDevice.PollInput(); //Make player fall if he is jumping and moving down if (this.VelocityY < 0 && this._State != PlayerState.Parachute && this._State != PlayerState.ClimbDown && this._State != PlayerState.ClimbUp && this._State != PlayerState.ClimbIdle && this._State != PlayerState.Drown && this._HurtSide == CollisionRectAccess.HitSide.None) { if (this._State == PlayerState.JumpLeftMoving || this._State == PlayerState.JumpRightMoving || this._State == PlayerState.FallMoving) { this._State = PlayerState.FallMoving; } else { this._State = PlayerState.FallDown; } } //Make player fall off laders if they move right or left if ( (this._State == PlayerState.ClimbDown || this._State == PlayerState.ClimbUp || this._State == PlayerState.ClimbIdle) && (CurrInputDevice.GetKey(InputDeviceAccess.GameKeys.Left, this.ControllerId) || CurrInputDevice.GetKey(InputDeviceAccess.GameKeys.Right, this.ControllerId)) ) { this.FaceLeft = CurrInputDevice.GetKey(InputDeviceAccess.GameKeys.Left, this.ControllerId); this._State = PlayerState.FallMoving; } //Synchronous animations // Respawn if the player sank into the goo or died if (this.Y < -2.2f || this._State == PlayerState.Dead) { this.ActionRespawn(); } // Die else if (this._State == PlayerState.Dying || this._Item == ItemType.Death) { this.ActionDie(); } // Drown else if (this.Y <= -1.6815 || this._State == PlayerState.Drown) { this.ActionDrown(); } // Explode else if (this._State == PlayerState.Explode) { this.ActionExplode(); } //Hurt else if (this._HurtSide != CollisionRectAccess.HitSide.None) { //TODO: //if ishurt and standing and they are pressing they key twords the // enemy then do is ramming and turn off hurt // otherwise do hurt this.ActionHurt(Foreground); } // //Hook // else if(this._Item == ItemType.Hook && // ( // this._State == PlayerState.Hook || // CurrInputDevice.GetKey(InputDeviceAccess.GameKeys.Attack, this.ControllerId)==true && // (this._State == PlayerState.FallDown || this._State == PlayerState.FallMoving || // this._State == PlayerState.JumpLeftMoving || this._State == PlayerState.JumpRightMoving || // this._State == PlayerState.JumpLeftUp || this._State == PlayerState.JumpRightUp) // ) // ) // { // this.ActionHook(Foreground); // } //Parachute else if ( this._Item == ItemType.parachute && (CurrInputDevice.GetKey(InputDeviceAccess.GameKeys.Attack, this.ControllerId) == true || this._State == PlayerState.Parachute) && (this._State == PlayerState.FallDown || this._State == PlayerState.FallMoving || this._State == PlayerState.Parachute) ) { if (CurrInputDevice.GetKey(InputDeviceAccess.GameKeys.Right, this.ControllerId)) { this.FaceLeft = false; } else if (CurrInputDevice.GetKey(InputDeviceAccess.GameKeys.Left, this.ControllerId)) { this.FaceLeft = true; } this.ActionParachute(Foreground); } //Landing else if (this.IsOnGround && (this._State == PlayerState.Land || this._State == PlayerState.FallDown || this._State == PlayerState.FallMoving)) { this.ActionLand(); } //Climbing up poll else if (CurrInputDevice.GetKey(InputDeviceAccess.GameKeys.Up, this.ControllerId) && Foreground.CanGrabPoll(this) && (this.IsOnGround || this._State == PlayerState.ClimbUp || this._State == PlayerState.ClimbDown || this._State == PlayerState.ClimbIdle)) { this.ActionClimbUp(Foreground); } //Climbing down poll else if (CurrInputDevice.GetKey(InputDeviceAccess.GameKeys.Down, this.ControllerId) && Foreground.CanGrabPoll(this) && (this.IsOnGround || this._State == PlayerState.ClimbUp || this._State == PlayerState.ClimbDown || this._State == PlayerState.ClimbIdle)) { this.ActionClimbDown(Foreground); } //Idle on poll else if (this._State == PlayerState.ClimbUp || this._State == PlayerState.ClimbDown || this._State == PlayerState.ClimbIdle) { this.ActionClimbIdle(Foreground); } //Falling Moving and Falling Down else if (this.IsOnGround == false && this._State != PlayerState.Parachute && this._State != PlayerState.JumpLeftMoving && this._State != PlayerState.JumpRightMoving && this._State != PlayerState.JumpLeftUp && this._State != PlayerState.JumpRightUp && this._State != PlayerState.SuperJumpMoving && this._State != PlayerState.SuperJumpUp) { if (this._State != PlayerState.FallDown) { this.ActionFallMoving(Foreground); } else { this.ActionFallDown(Foreground); } } //Jumping moving else if ( (this._State == PlayerState.JumpRightMoving || this._State == PlayerState.JumpLeftMoving) || (CurrInputDevice.GetKey(InputDeviceAccess.GameKeys.Up, this.ControllerId) == true && (CurrInputDevice.GetKey(InputDeviceAccess.GameKeys.Left, this.ControllerId) == true || CurrInputDevice.GetKey(InputDeviceAccess.GameKeys.Right, this.ControllerId) == true) && this.IsOnGround == true) ) { //Set a direction for the initial frame if (this._State != PlayerState.JumpRightMoving && this._State != PlayerState.JumpLeftMoving) { this.FaceLeft = CurrInputDevice.GetKey(InputDeviceAccess.GameKeys.Left, this.ControllerId); } this.ActionJumpMoving(Foreground); } //Jumping up else if ( (this._State == PlayerState.JumpRightUp || this._State == PlayerState.JumpLeftUp) || CurrInputDevice.GetKey(InputDeviceAccess.GameKeys.Up, this.ControllerId) == true && this.IsOnGround == true ) { this.ActionJumpUp(Foreground); } //Super Jump moving else if ( this._State == PlayerState.SuperJumpMoving || (CurrInputDevice.GetKey(InputDeviceAccess.GameKeys.Attack, this.ControllerId) == true && this._Item == ItemType.SuperJump && (CurrInputDevice.GetKey(InputDeviceAccess.GameKeys.Left, this.ControllerId) == true || CurrInputDevice.GetKey(InputDeviceAccess.GameKeys.Right, this.ControllerId) == true) && this.IsOnGround == true) ) { //Set a direction for the initial frame if (this._State != PlayerState.JumpRightMoving && this._State != PlayerState.JumpLeftMoving) { this.FaceLeft = CurrInputDevice.GetKey(InputDeviceAccess.GameKeys.Left, this.ControllerId); } this.ActionSuperJumpMoving(Foreground); } //Super Jump up else if ( this._State == PlayerState.SuperJumpUp || (this._Item == ItemType.SuperJump && CurrInputDevice.GetKey(InputDeviceAccess.GameKeys.Attack, this.ControllerId) == true && this.IsOnGround == true) ) { this.ActionSuperJumpUp(Foreground); } //Rolling backwords else if (this._State == PlayerState.RollBack) { this.ActionRollBack(Foreground); } //Rolling Forward else if ( this._State == PlayerState.RollForward || ( (this._State == PlayerState.RunRight || this._State == PlayerState.RunLeft) && CurrInputDevice.GetKey(InputDeviceAccess.GameKeys.Down, this.ControllerId) == true && (CurrInputDevice.GetKey(InputDeviceAccess.GameKeys.Left, this.ControllerId) == true || CurrInputDevice.GetKey(InputDeviceAccess.GameKeys.Right, this.ControllerId) == true) ) ) { this.ActionRollForward(Foreground); } //Use weapon else if ((CurrInputDevice.GetKey(InputDeviceAccess.GameKeys.Attack, this.ControllerId) == true || this._State == PlayerState.Shoot || this._State == PlayerState.Grenade || this._State == PlayerState.Mine || this._State == PlayerState.SlideMine) && this.ItemCount > 0) { if (this._Item == ItemType.Gun) { this.ActionGun(); } else if (this._Item == ItemType.Grenade) { this.ActionGrenade(); } else if (this._Item == ItemType.SlideMine) { this.ActionSlideMine(); } else if (this._Item == ItemType.Mine) { this.ActionMine(); } } //Asynchronous animations //Ducking else if (CurrInputDevice.GetKey(InputDeviceAccess.GameKeys.Down, this.ControllerId) == true) { this.ActionDuck(); } //Running Right else if (CurrInputDevice.GetKey(InputDeviceAccess.GameKeys.Right, this.ControllerId) == true) { this.ActionRunRight(Foreground); } //Running Left else if (CurrInputDevice.GetKey(InputDeviceAccess.GameKeys.Left, this.ControllerId) == true) { this.ActionRunLeft(Foreground); } //Idle animations else if (this._State == PlayerState.Duck) { this.ActionLand(); } else { if (this.FaceLeft == false) { this.ActionStandRight(); } else { this.ActionStandLeft(); } } //Move player if (this.VelocityX != 0.0f) { this.MoveHorizontal(Foreground); } if (this.VelocityY > 0.0f) { this.MoveUp(Foreground); } else if (this.VelocityY < 0.0f) { this.MoveDown(Foreground); } }