// Update is called once per frame public override Status Update() { playerPosition = player.transform.position; selfPosition = self.transform.position; direction = self.GetComponent <Movement> ().direction; float xDiff = Mathf.Abs(playerPosition.x - selfPosition.x); float yDiff = Mathf.Abs(playerPosition.y - selfPosition.y); if (xDiff < blackboard.GetFloatVar("attackProximityDistanceX")) { if (yDiff <= blackboard.GetFloatVar("attackProximityDistanceY")) { if (self.tag != "Boss" || xDiff >= blackboard.GetFloatVar("attackProximityMinX")) //! other Bosses need { if (direction == Movement.Direction.Left && playerPosition.x < selfPosition.x) { if (onSuccess.id != 0) { owner.root.SendEvent(onSuccess.id); } return(Status.Success); } if (direction == Movement.Direction.Right && playerPosition.x > selfPosition.x) { if (onSuccess.id != 0) { owner.root.SendEvent(onSuccess.id); } return(Status.Success); } } } } return(Status.Failure); }
/** * Rotates a given to local orientation. */ public static Vec3 toVec3(this Dir d) { switch (d) { case Dir.None: return(new Vec3(0.0f, 0.0f, 0.0f)); case Dir.Front: return(new Vec3(0.0f, 0.0f, 1.0f)); case Dir.Back: return(new Vec3(0.0f, 0.0f, -1.0f)); case Dir.Left: return(new Vec3(-1.0f, 0.0f, 0.0f)); case Dir.Right: return(new Vec3(1.0f, 0.0f, 0.0f)); case Dir.Top: return(new Vec3(0.0f, 1.0f, 0.0f)); case Dir.Bottom: return(new Vec3(0.0f, -1.0f, 0.0f)); default: throw new System.Exception("Invalid direction"); } }
public void OnFinishMovement(Dir d) { bool slip = false; if (d == this.facing || this.facing == Dir.None) { /* If on ice (and nothing in front), repeat */ GO obj = getObjectAt(RelPos.Bottom); if (obj != null) { Type blockType = Type.Error; this.issueEvent <RemoteGetType>( (x, y) => x.Get(out blockType), obj); slip = (blockType == Type.IceBlock); } slip = (slip && (getObjectAt(RelPos.Front) == null)); } if (slip) { this.uncheckedMove(d, this.lastDelay); } else { this.anim &= ~Animation.Move; } }
/** * Move the object to a new position. */ private System.Collections.IEnumerator move(Vec3 tgtPosition, Dir d, float moveDelay) { this.isMoving = true; this.issueEvent <MovementDetector>((x, y) => x.OnStartMovement(d, moveDelay)); int steps = (int)(moveDelay / Time.fixedDeltaTime); Vec3 dtMovement = tgtPosition / (float)steps; Vec3 finalPosition = this.transform.localPosition + tgtPosition; for (int i = 0; i < steps; i++) { /* TODO: Tween/Lerp? */ this.transform.localPosition = this.transform.localPosition + dtMovement; yield return(new UnityEngine.WaitForFixedUpdate()); } this.transform.localPosition = finalPosition; /* XXX: Wait some extra time (so the collision updates) to signal that * this entity finished turning. Otherwise, next frame's movement may * break */ yield return(new UnityEngine.WaitForFixedUpdate()); this.isMoving = false; this.issueEvent <MovementDetector>((x, y) => x.OnFinishMovement(d)); }
public PacManPlayer(int id, Position position, Movement.Direction direction, int score, bool alive) : base(id, position) { Score = score; Alive = alive; Direction = direction; }
public void OnCollision(BoxCollider collidedWith, Rectangle boundingBox) { if (collidedWith != null) { var(depthX, depthY) = boundingBox.GetAbsIntersectionDepth(collidedWith.BoundingBox); if (depthX == 0 && depthY == 0) //I know this will be dead on 0 { return; } if (depthX < depthY) { XDirection = Movement.Directions[XDirection].Opposite; } else { YDirection = Movement.Directions[YDirection].Opposite; } } else { if (Movement.IsTouchingBounds(boundingBox.X, boundingBox.Width, Scene.Game.GraphicsDevice.Viewport.Width)) { XDirection = Movement.Directions[XDirection].Opposite; } if (Movement.IsTouchingBounds(boundingBox.Y, boundingBox.Height, Scene.Game.GraphicsDevice.Viewport.Height)) { YDirection = Movement.Directions[YDirection].Opposite; } } }
/** * Try to move within the ledge. * * @param moveDir Base direction of sideways movement * @param moveDelay How long the movement should take */ protected void tryMoveLedge(Dir moveDir, float moveDelay) { switch (moveDir) { case Dir.Front: /* Move up, if there's enough room */ if (this.getObjectAt(RelPos.FrontTop) == null) { Dir d = this.facing | Dir.Top; this.move(d, moveDelay); this.dropFromLedge(); } break; case Dir.Back: /* Simply start to fall */ this.dropFromLedge(); break; case Dir.Right: this.tryMoveLedgeSide(RelPos.Right, this.facing.rotateClockWise() /* innerTurn */, this.facing.rotateCounterClockWise() /* outterTurn */, moveDir, moveDelay); break; case Dir.Left: this.tryMoveLedgeSide(RelPos.Left, this.facing.rotateCounterClockWise() /* innerTurn */, this.facing.rotateClockWise() /* outterTurn */, moveDir, moveDelay); break; } }
static private Dir vec3ToDir(Vec3 pos) { /* XXX: Only check axis X and Z */ int[] axisOrder = { 0, 2 }; Dir[,] perAxis = { { Dir.Left, Dir.Right }, { Dir.Bottom, Dir.Top }, { Dir.Back, Dir.Front }, }; float absDist = 0.0f; Dir d = Dir.None; /* Select the direction of the axis with the greater distance, so the * entity will follow the last position of the targeted object */ for (int i = 0; i < axisOrder.Length; i++) { int axis = axisOrder[i]; if (Math.Abs(pos[axis]) > absDist) { absDist = Math.Abs(pos[axis]); if (pos[axis] < 0) { d = perAxis[axis, 0]; } else if (pos[axis] > 0) { d = perAxis[axis, 1]; } } } return(d); }
/** * Rotate toward direction 'to' (i.e., to 'tgt' degrees), rotating 'dt' * degress. */ private System.Collections.IEnumerator turn(float tgt, float dt, Dir to) { this.isTurning = true; this.issueEvent <TurnDetector>((x, y) => x.OnStartTurning(to)); int steps = (int)(this.TurnDelay / Time.fixedDeltaTime); dt /= (float)steps; Vec3 axis = new Vec3(0, 1, 0); for (int i = 0; i < steps; i++) { this.transform.Rotate(axis, dt * (i / (float)steps) * 2f); yield return(new UnityEngine.WaitForFixedUpdate()); } Vec3 tmp = this.transform.eulerAngles; this.transform.eulerAngles = new Vec3(tmp.x, tgt, tmp.z); /* XXX: Wait some extra time (so the collision updates) to signal that * this entity finished turning. Otherwise, next frame's movement may * break */ yield return(new UnityEngine.WaitForFixedUpdate()); this.isTurning = false; this.issueEvent <TurnDetector>((x, y) => x.OnFinishTurning(to)); }
private void KeyIsDown(object sender, KeyEventArgs e) { switch (e.KeyCode) { case Keys.Left: Direction = Movement.Direction.Left; break; case Keys.Right: Direction = Movement.Direction.Right; break; case Keys.Up: Direction = Movement.Direction.Up; break; case Keys.Down: Direction = Movement.Direction.Down; break; case Keys.Enter: tbMsg.Enabled = true; Direction = Movement.Direction.Stopped; tbMsg.Focus(); break; } }
public override void Update(double elapsedSeconds) { Direction = Movement.GetInputDirections(); TryMove(elapsedSeconds); base.Update(elapsedSeconds); }
void FixedUpdate() { AnimateVulnerable(); if (_player) { return; } if (_wait <= _delayed_start) { ++_wait; return; } if (_starting_position) { _direction = transform.position.x < _pen_mid.transform.position.x ? Movement.Direction.Right : Movement.Direction.Left; } else if (_starting_area) { _direction = transform.position.z < _pen_exit.transform.position.z ? Movement.Direction.Up: Movement.Direction.Down; } Quaternion rot = Quaternion.identity; rot.eulerAngles = new Vector3(270.0f, 0.0f, 0.0f); // while (Movement.CollisionIn(_direction, transform.position)) // Movement.SwitchDirection(Movement.Randomize(), 0, transform.position, ref _direction); rigidbody.velocity = Movement.MoveDirection(_direction) * _speed; rot.eulerAngles = Movement.FaceDirection(_direction); rigidbody.rotation = rot; }
/** * Move the object in a given direction * * @param to The movement direction * @param delay How long the movement shall take */ virtual protected void move(Dir to, float delay) { if ((this.anim & Animation.Move) != 0) { return; } this.uncheckedMove(to, delay); }
override protected void turn(Dir to) { if (!this.isOnLedge() && (this.anim & Animation.Turn) == 0) { Global.Sfx.playPlayerTurning(this.fastGetTr()); } base.turn(to); }
private void tryPushBlock(Dir pushDir) { GO block = null; float delay = 0.0f; bool didPush = false; if (turnToClosestBlock(out block)) { if (pushDir == this.facing) { this.issueEvent <PushController>( (x, y) => x.TryPush(ref delay, ref didPush, pushDir), block); if (didPush) { Global.Sfx.playPushBlock(delay, this.fastGetTr()); this.StartCoroutine(this.doPush(delay)); } else { this.StartCoroutine(this.playCantPushSfx()); } } else if (pushDir == this.facing.toLocal(Dir.Back) && getObjectAt(RelPos.Back) == null) { this.issueEvent <PushController>( (x, y) => x.TryPush(ref delay, ref didPush, pushDir, this.gameObject), block); if (didPush) { Global.Sfx.playPullBlock(delay, this.fastGetTr()); /* Make sure any block bellow becomes cracked */ if (this.isOnLedge()) { this.dropFromLedge(); } if (!getBlockAt(RelPos.BackBottom)) { this.move(pushDir, delay); this.StartCoroutine(this.delayedFall(delay)); this.skipFallSfx = true; } else { this.move(pushDir, delay); } } else { this.StartCoroutine(this.playCantPushSfx()); } } } }
public void SetScore(int x, int y, Movement.Direction d, int score, int distance) { if (!_cells[x][y]._pass || distance >= RADIUS || _cells[x][y]._score >= score) return; _cells[x][y]._score = score; if (d != Movement.Direction.Right) SetScore(x + 1, y, Movement.Direction.Left, score - 1, distance + 1); if (d != Movement.Direction.Left) SetScore(x - 1, y, Movement.Direction.Right, score - 1, distance + 1); if (d != Movement.Direction.Up) SetScore(x, y - 1, Movement.Direction.Down, score - 1, distance + 1); if (d != Movement.Direction.Down) SetScore(x, y + 1, Movement.Direction.Up, score - 1, distance + 1); }
/** * Rotate this direction clock-wise. */ public static Dir rotateClockWise(this Dir d) { int i = (int)d; if ((i << 1) > 0xf) { return((Dir)0x1); } return((Dir)(i << 1)); }
/** * Rotate this direction counter clock-wise. */ public static Dir rotateCounterClockWise(this Dir d) { int i = (int)d; if ((i >> 1) == 0x0) { return((Dir)0x8); } return((Dir)(i >> 1)); }
public void Lunge(BaseAttack.Strength strength = BaseAttack.Strength.Light) { //Move forward if (!active) { active = true; _dir = gameObject.GetComponent <Movement>().direction; lightAttack = (strength == BaseAttack.Strength.Light ? true : false); prevPosition = _collision.transform.position; } }
private void KeyIsUp(object sender, KeyEventArgs e) { switch (e.KeyCode) { case Keys.Left: case Keys.Right: case Keys.Up: case Keys.Down: Direction = Movement.Direction.Stopped; break; } }
/** * Rotate the object to a given orientation * * @param to The new orientation */ virtual protected void turn(Dir to) { if ((this.anim & Animation.Turn) != 0) { return; } else if (this.turner != null) { this.issueEvent <TurnController>( (x, y) => x.Rotate(this.facing, to), this.turner); } }
//TODO this could be more 'elegant' as as loop or smtg public Movement.Direction BestDirection(int x, int y) { Movement.Direction d = Movement.Direction.Up; if (ScoreInDirection(x, y, Movement.Direction.Left) > ScoreInDirection(x, y, Movement.Direction.Up)) d = Movement.Direction.Left; if (ScoreInDirection(x, y, Movement.Direction.Down) > ScoreInDirection(x, y, Movement.Direction.Left)) d = Movement.Direction.Down; if (ScoreInDirection(x, y, Movement.Direction.Right) > ScoreInDirection(x, y, Movement.Direction.Down)) d = Movement.Direction.Right; return d; }
void CheckSpeedDirectionChanges() { if (speed == 0) { direction = Movement.Direction.Forward; } if (ShouldUpdateLoops()) { if (onUpdateMovementState != null) { onUpdateMovementState(); } } }
private bool turnToClosestBlock(out GO obj) { GO tmp = null; Dir objDir = Dir.None; getClosestBlock(out tmp, out objDir); obj = tmp; if (tmp != null && objDir != this.facing) { this.turn(objDir); return(false); } return(true); }
public static void TurnTo(Movement.Direction dir) { switch (dir) { case Movement.Direction.Up: SetAnimation(upwardsAnimation); break; case Movement.Direction.Down: SetAnimation(downwardsAnimation); break; case Movement.Direction.Left: SetAnimation(sideAnimation); SetFlip(false); break; case Movement.Direction.Right: SetAnimation(sideAnimation); SetFlip(true); break; } }
protected void getClosestBlock(out GO obj, out Dir dir) { GO retObj = null; Dir retDir = Dir.None; RelPos[] testPos = { RelPos.Front, RelPos.Right, RelPos.Left, RelPos.Back }; foreach (RelPos p in testPos) { if (checkBlockAt(out retObj, out retDir, p)) { break; } } obj = retObj; dir = retDir; }
override protected void updateState() { base.updateState(); this.updateAnimationState(); if (this.anim != Animation.None) { return; } Dir newDir = this.getInputDirection(); if (newDir != Dir.None) { if (this.isOnLedge()) { if (this.checkActionButton() && this.getBlockAt(RelPos.Bottom) != null) { this.tryPushBlock(newDir); } else { this.tryMoveLedge(newDir, this.MoveDelay); } } else if (this.checkActionButton()) { this.tryPushBlock(newDir); } else if (this.facing != newDir) { this.turn(newDir); } else { this.tryMoveForward(this.MoveDelay); } } else if (!this.isOnLedge() && this.checkActionButton()) { GO obj = null; this.turnToClosestBlock(out obj); } }
public void Move(Dir d, float moveDelay) { if (this.isMoving) { return; } Vec3 tgtPosition = new Vec3(); Dir tmp = d; for (int i = (int)Dir.First; tmp != Dir.None && i < (int)Dir.Max; i <<= 1) { switch (tmp & (Dir)i) { case Dir.Back: tgtPosition.z = -1.0f; break; case Dir.Left: tgtPosition.x = -1.0f; break; case Dir.Front: tgtPosition.z = 1.0f; break; case Dir.Right: tgtPosition.x = 1.0f; break; case Dir.Top: tgtPosition.y = 1.0f; break; case Dir.Bottom: tgtPosition.y = -1.0f; break; } /* switch */ tmp = (Dir)(((int)tmp) & ~i); } /* for */ this.StartCoroutine(this.move(tgtPosition, d, moveDelay)); }
void OnTriggerStay(Collider other) { if (_player) { return; } if (other.gameObject.tag == "Pen Gate") { if (_direction == Movement.Direction.Down) { _direction = Movement.Direction.Left; } } else if ((other.collider.gameObject.tag == "Ghost" || other.collider.gameObject.tag == "Player") && InFrontOf(other)) { Movement.Reverse(ref _direction); } }
void OnTriggerEnter(Collider other) { if (_player) { return; } if (other.gameObject.tag == "Pellet") { return; } if (other.gameObject.tag == "Pen Mid") { _starting_position = false; } else if (other.gameObject.tag == "Pen Exit") { _starting_area = false; if (_direction == Movement.Direction.Down) { _direction = Movement.Direction.Left; } else if (_direction == Movement.Direction.Up) { _direction = Movement.Direction.Right; } } else if (other.gameObject.tag == "Pen Gate") { if (_direction == Movement.Direction.Down) { _direction = Movement.Direction.Left; } } else if ((other.collider.gameObject.tag == "Ghost" || other.collider.gameObject.tag == "Player") && InFrontOf(other)) { Movement.Reverse(ref _direction); } else { Movement.SwitchDirection(Movement.Randomize(), 0, transform.position, ref _direction); } }