// Update is called once per frame void Update() { if (Input.GetAxisRaw("Horizontal") > 0) { transform.position = new Vector3((float)(transform.position.x + (speed * Time.deltaTime)), transform.position.y); lastDirection = LastDirection.RIGHT; } else if (Input.GetAxisRaw("Horizontal") < 0) { transform.position = new Vector3((float)(transform.position.x - (speed * Time.deltaTime)), transform.position.y); lastDirection = LastDirection.LEFT; } if (Input.GetAxisRaw("Vertical") > 0) { transform.position = new Vector3(transform.position.x, (float)(transform.position.y + (speed * Time.deltaTime))); lastDirection = LastDirection.DOWN; } else if (Input.GetAxisRaw("Vertical") < 0) { transform.position = new Vector3(transform.position.x, (float)(transform.position.y - (speed * Time.deltaTime))); lastDirection = LastDirection.UP; } if (Input.GetButtonDown("Fire1")) { switch (lastDirection) { case LastDirection.UP: transform.position = new Vector3(transform.position.x, transform.position.y - teleportRange); break; case LastDirection.DOWN: transform.position = new Vector3(transform.position.x, transform.position.y + teleportRange); break; case LastDirection.LEFT: transform.position = new Vector3(transform.position.x - teleportRange, transform.position.y); break; case LastDirection.RIGHT: transform.position = new Vector3(transform.position.x + teleportRange, transform.position.y); break; } } }
protected override void SetAnimations() { if (_gameTime.TotalGameTime.TotalSeconds - LastAttackTime > 5) // attack for 1 second { Direction = General.eDirection.Idle; LastAttackTime = _gameTime.TotalGameTime.TotalSeconds; _animationManager.Play(_animations["Idle" + LastDirection.ToString()]); } else if (_gameTime.TotalGameTime.TotalSeconds - LastAttackTime > 4) // attack once at 4 seconds { Direction = General.eDirection.Attack; _animationManager.Play(_animations["Attack" + LastDirection.ToString()]); } else { base.SetAnimations(); } }
//Create recursive method sequence //Has START, which runs Left, Right, Up, Down searches if possible //If bubble found, run Start with it. //Need safeguards to ensure that the same bubble isn't being covered. Store list of positions to avoid? Pass cluster list to check if the object already exists? private List <Bubble> StartFindCluster(Bubble bubble, Vector2 position, LastDirection lastDir = LastDirection.None) { List <Bubble> cluster = new List <Bubble>(); int x = (int)position.x; int y = (int)position.y; //right if (lastDir != LastDirection.Left) { if (bubbleList[x + 1][y].Color == bubble.Color) { cluster.Add(bubbleList[x + 1][y]); cluster.AddRange(StartFindCluster(bubbleList[x + 1][y], new Vector2(x + 1, y))); } } //left if (lastDir != LastDirection.Right) { if (bubbleList[x - 1][y].Color == bubble.Color) { cluster.Add(bubbleList[x - 1][y]); cluster.AddRange(StartFindCluster(bubbleList[x - 1][y], new Vector2(x - 1, y))); } } //up if (lastDir != LastDirection.Down) { if (bubbleList[x][y + 1].Color == bubble.Color) { cluster.Add(bubbleList[x][y + 1]); cluster.AddRange(StartFindCluster(bubbleList[x][y + 1], new Vector2(x, y + 1))); } } //down if (lastDir != LastDirection.Up) { if (bubbleList[x][y - 1].Color == bubble.Color) { cluster.Add(bubbleList[x][y - 1]); cluster.AddRange(StartFindCluster(bubbleList[x][y - 1], new Vector2(x, y - 1))); } } return(cluster); }
void Reset() { GetComponent <Animator>().SetFloat("Death", 0f); GetComponent <Animator>().Rebind(); GetComponent <Animator>().enabled = false; currentNode = GetComponent <GameLoad>().start; nextNode = null; transform.position = new Vector2(currentNode.transform.position.x, currentNode.transform.position.y); lastDirection = LastDirection.None; GetComponent <GameLoad>().InitializeGhosts(level); readyText.enabled = true; if (lives > 0) { Invoke("Ready", 2f); } else { readyText.text = "Game Over!"; } }
// Start is called before the first frame update void Start() { scoreText.text = "0"; ghostPoints = 200; for (int i = 0; i < lives; i++) { Instantiate(lifeIcon, new Vector3(24 + 20 * i, 8, 0), Quaternion.identity); } GetComponent <Animator>().enabled = false; dead = true; GetComponent <GameLoad>().CreateNodes(); PrepareLevel(); currentNode = GetComponent <GameLoad>().start; nextNode = null; transform.position = new Vector2(currentNode.transform.position.x, currentNode.transform.position.y); lastDirection = LastDirection.None; Invoke("Ready", 2f); }
/// <summary> /// Compute the current position based on the time and updates. /// </summary> /// <param name="timestamp">local time in ticks (milliseconds)</param> /// <returns></returns> public virtual Vector3 ComputePosition(long timestamp) { long timeDifference = (timestamp - lastLocTimestamp); if (pathInterpolator != null) { PathLocAndDir locAndDir = pathInterpolator.Interpolate(timestamp); log.DebugFormat("MobNode.ComputePosition: oid {0}, followTerrain {1}, pathInterpolator {2}", oid, followTerrain, locAndDir == null ? "null" : locAndDir.ToString()); if (locAndDir != null) { if (locAndDir.LengthLeft != 0f) { SetOrientation(Vector3.UnitZ.GetRotationTo(LastDirection.ToNormalized())); } LastDirection = locAndDir.Direction; lastDirTimestamp = timestamp; lastLocTimestamp = timestamp; Vector3 loc = locAndDir.Location; // If we don't have full pathing support, use // MoveMobNode so the mob is supported by // collision volumes. if (useMoveMobNodeForPathInterpolator && collider != null && collider.PartCount > 0) { Vector3 diff = loc - Position; diff.y = 0; Vector3 newpos = worldManager.MoveMobNode(this, diff, timeDifference); lastPosition = newpos; return(newpos); } else { loc = worldManager.ResolveLocation(this, loc); if (collider != null) { Vector3 diff = loc - lastPosition; collider.AddDisplacement(diff); } lastPosition = loc; return(loc); } } else { // This interpolator has expired, so get rid of it pathInterpolator = null; LastDirection = Vector3.Zero; } } else { lastLocTimestamp = timestamp; } Vector3 pos = lastPosition + ((timeDifference / 1000.0f) * LastDirection); Vector3 displacement = pos - Position; // Move the mob node - - given pathing, this should // _only_ happen for mobnodes that are other clients' // players. pos = worldManager.MoveMobNode(this, displacement, timeDifference); lastPosition = pos; return(pos); }
void FixedUpdate() { horizInput = Input.GetKey("right") || Input.GetKey("k") ? 1 : Input.GetKey("left") || Input.GetKey("j")? -1 : horizInput; vertInput = Input.GetKey("up") || Input.GetKey("i") ? 1 : Input.GetKey("down") || Input.GetKey("m") ? -1 : vertInput; if (dead) { horizInput = vertInput = 0; } else { if (!siren.isPlaying) { siren.Play(); } if (nextNode == null) { if (lastDirection != LastDirection.Horizontal) { if (vertInput > 0) { nextNode = currentNode.up; } else if (vertInput < 0) { nextNode = currentNode.down; } if (horizInput > 0) { nextNode = currentNode.right == null ? nextNode : currentNode.right; } else if (horizInput < 0) { nextNode = currentNode.left == null ? nextNode : currentNode.left; } if (nextNode == null) { horizInput = vertInput = 0; } } else if (lastDirection != LastDirection.Vertical) { if (horizInput > 0) { nextNode = currentNode.right; } else if (horizInput < 0) { nextNode = currentNode.left; } if (vertInput > 0) { nextNode = currentNode.up == null ? nextNode : currentNode.up; } else if (vertInput < 0) { nextNode = currentNode.down == null ? nextNode : currentNode.down; } if (nextNode == null) { horizInput = vertInput = 0; } } } if (nextNode != null) { if (nextNode == currentNode.left || nextNode == currentNode.right) { vertInput = 0; lastDirection = LastDirection.Horizontal; } if (nextNode == currentNode.up || nextNode == currentNode.down) { horizInput = 0; lastDirection = LastDirection.Vertical; } GetComponent <Animator>().SetFloat("DirX", horizInput); GetComponent <Animator>().SetFloat("DirY", vertInput); transform.position = new Vector2(transform.position.x + horizInput * speed, transform.position.y + vertInput * speed); if (nextNode.transform.position.x == transform.position.x && nextNode.transform.position.y == transform.position.y) { // if (nextNode == currentNode || // nextNode == currentNode.right && nextNode.Past(nextNode.transform.position.x, currentNode.transform.position.x, transform.position.x) || // nextNode == currentNode.left && nextNode.Past(currentNode.transform.position.x, nextNode.transform.position.x, transform.position.x) || // nextNode == currentNode.up && transform.position.y >= nextNode.transform.position.y || // nextNode == currentNode.down && nextNode.transform.position.y >= transform.position.y) { // transform.position = nextNode.transform.position; currentNode = nextNode; nextNode = null; } else if (currentNode.transform.position.x == transform.position.x && currentNode.transform.position.y == transform.position.y) { nextNode = null; } } Vector2 newPos = transform.position; if (transform.position.y > screenTop) { newPos.y = screenBottom; } if (transform.position.y < screenBottom) { newPos.y = screenTop; } if (transform.position.x > screenRight) { newPos.x = screenLeft; } if (transform.position.x < screenLeft) { newPos.x = screenRight; } transform.position = newPos; } }
public override void Update(GameTime gameTime) { var direction = Vector2.Zero; var dodging = false; var attacking = false; var pad = GamePad.GetState(PlayerIndex.One); if (pad.IsConnected) { direction = pad.ThumbSticks.Left; direction.Y = -direction.Y; if (pad.IsButtonDown(Buttons.A) && LastDirection.LengthSquared() > 0) { dodging = true; } if (pad.IsButtonDown(Buttons.X) && LastDirection.LengthSquared() > 0) { attacking = true; } } else { if (Keyboard.GetState().IsKeyDown(Keys.W)) { direction.Y -= 1; } if (Keyboard.GetState().IsKeyDown(Keys.S)) { direction.Y += 1; } if (Keyboard.GetState().IsKeyDown(Keys.A)) { direction.X -= 1; } if (Keyboard.GetState().IsKeyDown(Keys.D)) { direction.X += 1; } if (Keyboard.GetState().IsKeyDown(Keys.Space) && LastDirection.LengthSquared() > 0) { dodging = true; } if (Keyboard.GetState().IsKeyDown(Keys.Enter) && LastDirection.LengthSquared() > 0) { attacking = true; } } if (direction.LengthSquared() > 1) { direction.Normalize(); } // ReSharper disable once ConditionIsAlwaysTrueOrFalse if (Teleport) { if (!_dodged) { if (dodging) { _dodged = true; direction *= DodgeDistance; } } if (!dodging) { _dodged = false; } } // ReSharper disable once RedundantIfElseBlock else { if (dodging && !_dodged) { direction = LastDirection; direction.Normalize(); direction *= DodgeDistance; _dodgeTimeSpan += gameTime.ElapsedGameTime; if (_dodgeTimeSpan > _maxDodgeTimeSpan) { _dodged = true; } } else { if (!dodging) { _dodged = false; _dodgeTimeSpan = TimeSpan.Zero; } LastDirection = direction; } } if (!_attacked) { if (attacking) { _attacked = true; Attack(); } } else { if (!attacking) { _attacked = false; } } if (Math.Abs(direction.X) < TOLERANCE) { if (Math.Abs(direction.Y) > TOLERANCE) { SetAnimation(direction.Y < 0 ? EView.Back : EView.Front); } else { SetAnimation(EView.Idle); } } else { SetAnimation(direction.X > 0 ? EView.Right : EView.Left); } Position += direction * (float)gameTime.ElapsedGameTime.TotalSeconds * MoveSpeed; Position = ClampToMap(Position); base.Update(gameTime); }