// Use this for initialization void Start() { _mCompatibleClimbDir = _mLedgeDirection == LedgeAttachment.CLIMB_LEFT ? CharacterFacing.FACE_LEFT : CharacterFacing.FACE_RIGHT; _mSecondsToClimbUp = _mSecondsToClimb * 0.7f; _mSecondsToClimbOver = _mSecondsToClimb * 0.3f; _mClimbStartPos = transform.position; }
// Update is called once per frame void Update() { if (_mPlayerCTRL == null) { return; } if (_mIsClimbing) { ClimbLedge(); return; } if (Input.GetKey(KeyCode.W)) { _mPlayerCTRL.SetFacingDir(CharacterFacing.FACE_UP); } else if (Input.GetKey(KeyCode.S)) { _mPlayerCTRL.SetFacingDir(CharacterFacing.FACE_DOWN); } else if (Input.GetKey(KeyCode.A)) { _mPlayerCTRL.SetFacingDir(CharacterFacing.FACE_LEFT); } else if (Input.GetKey(KeyCode.D)) { _mPlayerCTRL.SetFacingDir(CharacterFacing.FACE_RIGHT); } /*else if (Input.GetKey(KeyCode.K)) * { * Debug.Log("Key K"); * Scenes.instance.LoadScene(Scenes.Scene.GAME_OVER); * }*/ else { _mPlayerCTRL.SetFacingDir(CharacterFacing.FACE_CENTRE); } if (Input.GetKey(KeyCode.Space)) { CharacterFacing playerDir = _mPlayerCTRL.GetFacingDir(); if (playerDir != CharacterFacing.FACE_CENTRE && playerDir != _mCompatibleClimbDir) { ReleasePlayer(); } else { Debug.Log("Climbing"); _mClimbTimeProgess = 0; _mIsClimbing = true; } } }
//FixedUpdate is for handling physics/control protected virtual void FixedUpdate() { float dt = Time.fixedDeltaTime; float horizontal = controlInput.GetAxisRaw("Horizontal"); float vertical = controlInput.GetAxisRaw("Vertical"); float m = rigBod.mass; Vector2 v = rigBod.velocity; if (Mathf.Abs(v.x) > facingDeadZone) { if (v.x > 0) { facingDirection = CharacterFacing.Right; } else if (v.x < 0) { facingDirection = CharacterFacing.Left; } } modelTransform.rotation = Quaternion.Euler(0f, 90 * (int)facingDirection, 0f); float horizontalSpeed = (running) ? runSpeed : walkSpeed; Vector2 movementForce = Vector2.right * (horizontalSpeed - v.magnitude); movementForce.x *= Util.Sign(horizontal); rigBod.AddForce(movementForce); //If on the ground if (grounded || jumpsRemaining > 0) { if (controlInput.GetButtonDown("Vertical") && vertical > 0f && jumpsRemaining > 0) { Jump(jumpHeight * Mathf.Pow(jumpDampening, jumpCount - jumpsRemaining), dt); --jumpsRemaining; } } //Debug.Log ("" + horizontal + " " + v + " " + movementForce); }
// Update is called once per frame void FixedUpdate() { CharacterFacing facing = _mPlayerController.GetFacingDir(); if (_mIsFollowingPlayer) { _mTargetPos = _mPlayerController.GetComponent <Transform>().position; if (facing == CharacterFacing.FACE_LEFT) { _mTargetPos.x -= _mLoftRightOffset; } else if (facing == CharacterFacing.FACE_RIGHT) { _mTargetPos.x += _mLoftRightOffset; } if (facing == CharacterFacing.FACE_UP) { _mTargetPos.y += _mLoftRightOffset; } else if (facing == CharacterFacing.FACE_DOWN) { _mTargetPos.y -= _mLoftRightOffset; } } else { } _mTargetPos.z = this.transform.position.z; Vector3 difference = _mTargetPos - transform.position; float differenceMag = difference.magnitude; transform.position += (difference * differenceMag) * Time.deltaTime; }
public void ResetLevel(LevelResetCause resetCause) { if (GameWon) { return; } if (resetCause == LevelResetCause.Start) { LevelNumber = 1; } else if (resetCause == LevelResetCause.Victory) { ++LevelNumber; if (LevelNumber == 5) { GameWon = true; return; } } LoadTilesFromFile(); Vector2d startPositionToSet = new Vector2d(256, 256); for (int tileX = 0; tileX < Constants.LEVEL_WIDTH; ++tileX) { for (int tileY = 0; tileY < Constants.LEVEL_HEIGHT; ++tileY) { if (Tiles[tileX, tileY] == Constants.TILE_ID_FLAG_WHITE) { startPositionToSet = new Vector2d( tileX * Constants.TILE_SIZE + Constants.TILE_SIZE * 0.5 - Constants.SPRITE_SUIT_SIZE / 2, (tileY + 0.05) * Constants.TILE_SIZE); goto Found; } } } Found: { LavaSpeedPerLevel = new int[4] { 8, 20, 20, 30 }; LavaBombs = new List <LavaBombEntity>(); Angle = 0.0; McPosition = startPositionToSet; McVelocity = new Vector2d(0, 0); McGrounded = false; McRunning = false; LavaHeight = 0; facing = CharacterFacing.Left; //Tiles = new int[Constants.LEVEL_WIDTH, Constants.LEVEL_HEIGHT]; //Tiles[0, 0] = 1; //Tiles[1, 0] = 1; //Tiles[2, 0] = 1; //Tiles[9, 6] = 1; SpriteAnimationPosition = 0; LavaAnimationLoopValue = 0; SpitterLoopValue = 0; FlameSpitterLoopValue = 0; FlamesLoopValue = 0; EditorMode = false; } }
/// <summary> /// Process collisions - X first then Y. /// </summary> /// <param name="elapsedTime"></param> public void ProcessPlayerMovement(KeyboardState prevKeyState, KeyboardState keyState, double elapsedTime) { if (GameWon) { return; } if (EditorMode) { if (keyState.IsKeyDown(Key.F1) && !prevKeyState.IsKeyDown(Key.F1)) { SaveTilesToFile(); } if (keyState.IsKeyDown(Key.F2) && !prevKeyState.IsKeyDown(Key.F2)) { LoadTilesFromFile(); } } if (keyState.IsKeyDown(Key.F12) && !prevKeyState.IsKeyDown(Key.F12)) { EditorMode = !EditorMode; } Vector2d newPosition = McPosition; bool moveLeft = keyState.IsKeyDown(Key.Left) || keyState.IsKeyDown(Key.A); bool moveRight = keyState.IsKeyDown(Key.Right) || keyState.IsKeyDown(Key.D); if (moveLeft && !moveRight) { McRunning = true; newPosition.X -= Constants.CHARACTER_MOVE_SPEED * elapsedTime; facing = CharacterFacing.Left; } else if (moveRight && !moveLeft) { McRunning = true; newPosition.X += Constants.CHARACTER_MOVE_SPEED * elapsedTime; facing = CharacterFacing.Right; } else { McRunning = false; } if ((keyState.IsKeyDown(Key.Space) || keyState.IsKeyDown(Key.W)) && !(prevKeyState.IsKeyDown(Key.Space) || keyState.IsKeyDown(Key.W))) { if (McGrounded) { McGrounded = false; McVelocity.Y += Constants.JUMP_SPEED; } } CollisionOutcome outcome = IsIntersectionWithLevel(newPosition.X, newPosition.Y); switch (outcome) { case CollisionOutcome.None: McPosition = newPosition; break; case CollisionOutcome.Victory: ResetLevel(LevelResetCause.Victory); return; default: break; } newPosition = McPosition; // collisions - process Y McVelocity.Y -= Constants.GRAVITY * elapsedTime; newPosition.Y += McVelocity.Y * elapsedTime; outcome = IsIntersectionWithLevel(newPosition.X, newPosition.Y); switch (outcome) { case CollisionOutcome.None: McPosition = newPosition; McGrounded = false; break; case CollisionOutcome.Victory: ResetLevel(LevelResetCause.Victory); return; default: McVelocity = Vector2d.Zero; McGrounded = true; break; } if (IsLavaCollision(McPosition.Y)) { ResetLevel(LevelResetCause.Death); return; } }
public string GetSpriteTag(CharacterColour colour, CharacterFacing facing) { return($"{colour.ToString().ToLower()}_{facing.ToString().ToLower()}"); }
public void Update(Vector2 position, CharacterFacing facing) { switch (facing) { case CharacterFacing.Left: rectangle.X = (int)(position.X - rectangle.Width / 2 - offset.X); rectangle.Y = (int)(position.Y - rectangle.Height / 2 + offset.Y); break; case CharacterFacing.Right: rectangle.X = (int)(position.X - rectangle.Width / 2 + offset.X); rectangle.Y = (int)(position.Y - rectangle.Height / 2 + offset.Y); break; } }
void FixedUpdate() { if (_mIsPossessed) { return; } if (gameObject.GetComponent <UniquePlayerCTRL>() == null) { // Allow clones to kill themselves if (Input.GetKeyDown(KeyCode.K)) { gameObject.GetComponent <BloodDeath>().Bleed(); GameObject.Find("DataPacket").GetComponent <PickupUSB>().DropItem(); GameObject tempRef = gameObject; int indexOfThisEntity = GameManager.Instance().GetListOfEntities().IndexOf(tempRef.GetComponent <PlayerCTRL>()); GameManager.Instance().GetListOfEntities().Remove(tempRef.GetComponent <PlayerCTRL>()); Destroy(tempRef, 0.0f); PlayerCameraController camera = FindObjectOfType <PlayerCameraController>(); PlayerCTRL lastControlled = GameManager.Instance().GetListOfEntities()[indexOfThisEntity - 1]; lastControlled.SetUserControlEnabled(true); camera.SetTargetPlayerObject(lastControlled); return; } } // artificial gravity stronger than regular gravity _mRigidBody.AddForce(Vector3.down * 20.0f * _mRigidBody.mass); if (_mIsAtTerminal) { return; } if (!_mIsControlledByUser) { return; } if (Input.GetKeyDown(KeyCode.E)) { List <Lift> lifts = FindObjectsOfType <Lift>().OfType <Lift>().ToList(); List <Switch> switches = FindObjectsOfType <Switch>().OfType <Switch>().ToList(); List <Fusebox> fuseboxes = FindObjectsOfType <Fusebox>().OfType <Fusebox>().ToList(); List <Terminal> terminals = FindObjectsOfType <Terminal>().OfType <Terminal>().ToList(); foreach (Lift lift in lifts) { lift.Travel(this, transform); } foreach (Switch button in switches) { button.Press(transform); } foreach (Fusebox fuse in fuseboxes) { fuse.Press(transform); } foreach (Terminal terminal in terminals) { terminal.Press(); } } float leftRight = CalcLeftRightMovement(); if (leftRight != 0) { _mFacingDirection = leftRight > 0 ? CharacterFacing.FACE_RIGHT : CharacterFacing.FACE_LEFT; _mRigidBody.MovePosition(this.transform.position + (new Vector3(leftRight * _mMoveForce, 0.0f, 0.0f) * Time.deltaTime)); AnimatorChangeState(1); if (_mFacingDirection == CharacterFacing.FACE_LEFT) { SpriteRenderer spriteRender = GetComponent <SpriteRenderer>(); spriteRender.flipX = true; } else { SpriteRender.flipX = false; } } else { AnimatorChangeState(0); //Idle } }
public void SetFacingDir(CharacterFacing dir) { _mFacingDirection = dir; }