Example #1
0
	// Use this for initialization
	void Start () {
		state = FarmerState.Idle;
        CameraLookPos = transform.position;
        toolbar = FindObjectOfType<Toolbar>();
        rend = GetComponentInChildren<SpriteRenderer>();
        graphics = transform.GetChild(0);
	}
Example #2
0
	// Update is called once per frame
	void Update () {

        if (!paused.GetComponent<Pause>().isPaused) {
    		if (Input.GetMouseButtonUp(0)
                    && !toolbar.BlockOtherClicks
                    && !toolbar.IgnoreMouseUp
                    && toolbar.CanAffordAction(toolbar.ToolMode, actions, currentAction)) {
                Vector2 point = Map.Inst.Bound(Camera.main.ScreenToWorldPoint(Input.mousePosition));
                if (toolbar.ToolMode == FarmerActionType.Move) {
                    ClearActions();
                }
                EnqueueAction(toolbar.ToolMode, point);
            }

            if (state == FarmerState.Idle) {
                DequeueAction();
            } else if (state == FarmerState.Moving) {
                float duration = Time.time - moveStartTime;
                float progress = duration / Mathf.Max(moveDuration, .0001f);
                Vector2 vertical = Vector2.up * (1f - Mathf.Pow(Mathf.Sin (Mathf.PI * progress * 3f * moveDuration), 4)) * stepHeight;
                transform.position = Vector2.Lerp (startPos, target, duration / moveDuration);
                graphics.transform.localPosition = vertical;
                CameraLookPos = transform.position;
                if (allowInstantAction && currentAction.type != FarmerActionType.Move) {
                    Debug.Log("Creating prefab");
                    toolbar.CreatePrefabForAction(currentAction.type, new Vector3(currentAction.target.x, currentAction.target.y, 0f));
                    state = FarmerState.Idle;
                    SetMarkers();
                } else if (duration > moveDuration) {
                    if (currentAction.type != FarmerActionType.Move) {
                        toolbar.CreatePrefabForAction(currentAction.type,
                                transform.position);
                    }
                    state = FarmerState.Idle;
                    SetMarkers();
                }
            }
        }
	}
Example #3
0
 public void ClearActions () {
     actions.Clear();
     SetMarkers();
     state = FarmerState.Idle;
 }
Example #4
0
    void DequeueAction () {
        if (actions.Count == 0) {
            currentAction = null;
            SetMarkers();
            return;
        }
        currentAction = actions.First.Value;
        actions.RemoveFirst();

        if (!toolbar.CanAffordAction(currentAction.type)) {
            toolbar.UnsetTools();
            DequeueAction();
            return;
        }

        SetMarkers();
        target = currentAction.target;
        startPos = transform.position;
        Debug.DrawLine(startPos, target, Color.blue, 0.1f);
        state = FarmerState.Moving;
        moveStartTime = Time.time;

        if (allowInstantAction && currentAction.type != FarmerActionType.Move) {
            target = transform.position;
        }

        float moveDistance = Vector2.Distance(transform.position, target);
        moveDuration = Mathf.Max(moveDistance / moveSpeed, 0.01f);
        rend.flipX = (target.x > transform.position.x);
    }