protected void findTargetObject(char c) { if (lastChar != c || !lastCharValid) { lastChar = c; lastCharValid = true; targets.Clear(); this.World.FindTargetsFor(c, targets); } double minDist = double.MaxValue; InteractiveObject closer = null; foreach (InteractiveObject o in targets) { double d = o.DistanceTo(this.camera.Origin + this.camera.Direction.Normalized.Scaled(50)); if (o.HasActionFor(c) && d < minDist) { minDist = d; closer = o; } } if (minDist <= 130) { closer.Act(c); } }
// Update is called once per frame void Update() { float horizontalMovement = 0; if (canMove && !cursorManager.isUsingUIObject) { horizontalMovement = Input.GetAxis("Horizontal") * speed * Time.deltaTime; } if (Input.GetMouseButtonDown(0) && !cursorManager.isHoveringUI) { if (chatManager.IsActive()) { chatManager.Next(); } else { nextTarget = cursorManager.GetCurrentTarget(); if (cursorManager.isUsingUIObject) { if (cursorManager.isValidItemUsage) { walkingToTarget = false; } else { cursorManager.isUsingUIObject = false; cursorManager.ResetCursor(); Say(new string[] { "That's not gonna work." }); } } else { if (nextTarget == null) { nextMovePositionX = Camera.main.ScreenToWorldPoint(Input.mousePosition).x; walkingToTarget = true; } else { walkingToTarget = false; } } } } if (Math.Abs(horizontalMovement) > 0) { // clear target and give control back as user is using keyboard walkingToTarget = false; nextTarget = null; } if (walkingToTarget && Math.Abs(nextMovePositionX - transform.position.x) > threshold) { horizontalMovement = speed * Time.deltaTime * (nextMovePositionX < transform.position.x ? -1 : 1); } else if (nextTarget != null) { InteractiveObject actor = nextTarget; float distToTarget = Math.Abs(nextTarget.transform.position.x - transform.position.x); if (((actor.canInspect || actor.canUse || actor.canEnter) && distToTarget > threshold) || (actor.canTalk && distToTarget > 1.5)) { horizontalMovement = speed * Time.deltaTime * (nextTarget.transform.position.x < transform.position.x ? -1 : 1); } else { // we reached the target, time to act upon it nextTarget = null; actor.Act(); if (actor.canInspect || actor.canUse) { SetSprite(faceUp); } } } if (horizontalMovement == 0 && state != State.Whistling && state != State.Idle) { state = State.Idle; idleCounter = 0; } else if (horizontalMovement < 0 && state != State.WalkingLeft) { state = State.WalkingLeft; animCounter = 0; currentSpriteIndex = 0; } else if (horizontalMovement > 0 && state != State.WalkingRight) { state = State.WalkingRight; animCounter = 0; currentSpriteIndex = 0; } if (Math.Abs(horizontalMovement) > 0 && !chatManager.IsActive()) { transform.Translate(horizontalMovement, 0, 0); bool isStopped = false; if (horizontalMovement < 0 && transform.position.x < leftBound.position.x + leftBound.localScale.x) { transform.position = new Vector3(leftBound.position.x + leftBound.localScale.x, transform.position.y, transform.position.z); isStopped = true; } if (horizontalMovement > 0 && transform.localPosition.x > rightBound.localPosition.x) { transform.position = new Vector3(rightBound.position.x, transform.position.y, transform.position.z); isStopped = true; } if (isStopped) { walkingToTarget = false; } } if (chatManager.IsActive()) { } else { // set correct sprite if (state == State.Idle) { SetSprite(idle); idleCounter += Time.deltaTime; if (idleCounter > idleMaxTime) { state = State.Whistling; SetSprite(whistle[0]); currentSpriteIndex = 0; animCounter = 0; } } else { Sprite[] spriteArray; if (state == State.WalkingLeft) { spriteArray = walkLeft; } else if (state == State.WalkingRight) { spriteArray = walkRight; } else { spriteArray = whistle; } if (animCounter > animSpeed || animCounter == 0) { animCounter = 0; currentSpriteIndex = (currentSpriteIndex + 1) % 2; SetSprite(spriteArray[currentSpriteIndex]); } animCounter += Time.deltaTime; } } }