void Update() { if (h > 0) { h -= Time.deltaTime; } e.Move(Input.GetAxis("Horizontal") * e.movementSpeed); if (Input.GetAxis("Switch Item") < 0 && h <= 0) { e.SetSelectedItem(e.GetSelectedItem() - 1); h = 0.1f; } //cursorPos += new Vector2(Input.GetAxis("Cursor Horizontal") * cursorSpeed * Time.deltaTime, Input.GetAxis("Cursor Vertical") * cursorSpeed * Time.deltaTime); cursorPos = new Vector2(Mathf.Clamp(cursorPos.x + (Input.GetAxis("Cursor Horizontal") * cursorSpeed * Time.deltaTime), cursorBoundsMin.x, cursorBoundsMax.x), Mathf.Clamp(cursorPos.y + (Input.GetAxis("Cursor Vertical") * cursorSpeed * Time.deltaTime), cursorBoundsMin.y, cursorBoundsMax.y)); if (Input.GetAxis("Switch Item") > 0 && h <= 0) { e.SetSelectedItem(e.GetSelectedItem() + 1); h = 0.1f; } if (Input.GetAxis("Jump") > 0) { e.Jump(e.jumpForce * Input.GetAxis("Jump")); } if (Input.GetAxis("Fire1") > 0 || Input.GetMouseButton(0)) { e.UseItem(e.GetSelectedItem(), (Vector2)Camera.main.transform.position + cursorPos); } if (Input.GetAxis("Fire1") > 0) { e.MineBlock(e.selectedBlock); } Vector3 pz = Camera.main.transform.position + (Vector3)cursorPos; pz.z = 0; e.placeBlockPosition = pz; GameObject.Find("BPD").transform.position = (Vector3)e.placeBlockPosition + new Vector3(0, 0, 100); if (Input.GetButtonDown("Drop")) { e.DropItem(e.GetSelectedItem(), false); } if (e.GetSelectedItem() > e.storedItems.Count - 1) { e.SetSelectedItem(e.storedItems.Count - 1); return; } if (Input.GetAxis("Place") > 0) { if (e.startBlockPlace(Resources.Load <GameObject>("Prefabs/Block-" + e.storedItems[e.GetSelectedItem()].id), (int)Mathf.Round(pz.x), (int)Mathf.Round(pz.y))) { e.ConsumeItem(e.GetSelectedItem()); } } }
private void OnTriggerEnter2D(Collider2D other) { Rigidbody2D rb = other.GetComponent <Rigidbody2D>(); if (rb.velocity.y < -1f) { Entity m_entity = other.GetComponent <Entity>(); float direction = 1f; if (m_entity) { m_entity.Jump(); if (!m_entity.FacingRight) { direction = -1f; } } else { if (rb.velocity.x < 0f) { direction = -1f; } } rb.angularVelocity = 0f; rb.velocity = new Vector2(m_horizontalSpeed * direction, m_jumpForce); m_animator.SetTrigger("Jumped"); AkSoundEngine.PostEvent("ObjectBounce", gameObject); } }
// Update is called once per frame void Update() { if (Input.GetKey(KeyCode.D)) { if (!isRightDir) { entity.Flip(); isRightDir = true; } entity.MoveRight(speed); if (!entity.isJumping) { animatorController.Play("walk_right"); } else { animatorController.Play("jump_right"); } } else if (Input.GetKey(KeyCode.A)) { if (isRightDir) { entity.Flip(); isRightDir = false; } entity.MoveLeft(speed); if (!entity.isJumping) { animatorController.Play("walk_right"); } else { animatorController.Play("jump_right"); } } else { if (!entity.isJumping) { animatorController.Play("idle_right"); } } if (Input.GetKeyDown(KeyCode.W)) { entity.Jump(jumpForce); animatorController.Play("jump_right"); } }
public static bool FollowPath(Transform t, Entity e, Path p, float speed) { Node tNode = grid.NodeFromWorldPoint(t.position); if (p.Count > 0) { if (e.jumpable) { if (p[0].position.y > tNode.position.y) { e.Jump(); } } else { return(FollowFlyingPath(t, e, p, speed)); } if (tNode.position == p[0].position && p.Count > 1) { p.RemoveAt(0); } if (p.Count > 1) { Vector3 direction = p[0].position - tNode.position; float newX = Mathf.Lerp(t.rigidbody.velocity.x, direction.x * speed, Time.deltaTime * 50); Vector2 oldV = t.rigidbody.velocity; oldV.x = newX; t.rigidbody.velocity = oldV; return(false); } else if (p.Count == 1) { float slowerX = Mathf.Lerp(t.rigidbody.velocity.x, 0, Time.fixedDeltaTime * 2); t.rigidbody.velocity = new Vector2(slowerX, t.rigidbody.velocity.y); return(t.rigidbody.velocity.x == 0); } } return(true); }
// Update is called once per frame void FixedUpdate() { if (follow) { if (transform.position.x < target.position.x - (target.localScale.x * 1.5)) { e.Move(e.movementSpeed); } if (transform.position.x > target.position.x + (target.localScale.x * 1.5)) { e.Move(-e.movementSpeed); } if (target.position.y - (target.localScale.y * 1.5) > transform.position.y) { e.Jump(e.jumpForce); } } int i3 = 0; if (e.GetStat("thirst") / e.GetMaxStat("thirst") > .25f) { return; } int i = 0; foreach (Item item in e.storedItems) { if (item.id == 1) { break; } i++; } e.SetSelectedItem(i); e.UseItem(e.GetSelectedItem()); }
public override bool OnFrameImpl() { if (warp_state != null) { warp_state.OnFrame(); if (warp_state.IsDone) { warp_state = null; } else { return(true); } } List <Entity> entities = g.eve.QueryEntities(); if (entities == null || entities.Count <= 0) { // we're probably jumping right now - just chill return(true); } if (g.me.InSpace && g.me.SolarSystemID == solarsystem_id) { SetDone("Success"); return(false); } entities = g.eve.QueryEntities("ID = {0}".Format(entity_id)); if (entities.Count == 0) { SetDone("stargate not found"); return(false); } if (entities.Count > 1) { SetDone("stargate id ambiguous"); return(false); } Entity stargate = entities[0]; if (stargate.Distance > 150000.0) { warp_state = new WarpState(entity_id); warp_state.OnFrame(); return(true); } else if (stargate.Distance < 2500.0) { if (!jumped) { stargate.Jump(); jumped = true; } return(true); } else if (!approached) { stargate.Approach(); approached = true; } return(true); }
// returns True if they arrive close enough to the end of path public static bool Seek(Transform t, Entity e, Path p) { Node tNode = grid.NodeFromWorldPoint(t.position); // If the path exists, and the next node has a higher y value, jump; if (p.Count > 0) { if (e.jumpable) { if (p[0].position.y > tNode.position.y) { e.Jump(); } } else { if (p[0].position.y > tNode.position.y) { Vector2 newV = t.rigidbody.velocity; newV.y = e.speed; t.rigidbody.velocity = newV; } else if (p[0].position.y < tNode.position.y) { Vector2 newV = t.rigidbody.velocity; newV.y = -e.speed; t.rigidbody.velocity = newV; } } } else { t.rigidbody.velocity = new Vector2(0, t.rigidbody.velocity.y); return(true); } // If we are within 3 nodes distance, we are close enough to the target; if (p.Count <= 3) { float slowerX = Mathf.Lerp(t.rigidbody.velocity.x, 0, Time.fixedDeltaTime * 10); t.rigidbody.velocity = new Vector2(slowerX, t.rigidbody.velocity.y); return(t.rigidbody.velocity.x == 0); } if (tNode.position == p[0].position) { p.RemoveAt(0); p.RemoveAt(p.Count - 1); } Vector3 direction = p[0].position - tNode.position; float newX = Mathf.Lerp(t.rigidbody.velocity.x, direction.x * e.speed, Time.deltaTime * 50); Vector2 oldV = t.rigidbody.velocity; oldV.x = newX; t.rigidbody.velocity = oldV; return(false); }
public override void Jump() { _entity.Jump(); }
void Awake() { _ent = GetComponent <Entity>(); var any = new State <IAAction>("any"); var idle = new State <IAAction>("idle"); var planStep = new State <IAAction>("planStep"); var failStep = new State <IAAction>("failStep"); var pickup = new State <IAAction>("pickup"); var create = new State <IAAction>("create"); var upgrade = new State <IAAction>("upgrade"); var attack = new State <IAAction>("attack"); var super_attack = new State <IAAction>("super_attack"); var success = new State <IAAction>("success"); failStep.OnEnter += a => { _ent.Stop(); Debug.Log("Plan failed"); }; pickup.OnEnter += a => { Debug.Log("pickup.OnEnter"); _ent.GoTo(_target.transform.position); _ent.OnHitItem += PerformPickUp; Debug.Log("pickup.OnEnter finish"); }; pickup.OnExit += a => { Debug.Log("pickup.OnExit"); action_started = false; _ent.OnHitItem -= PerformPickUp; Debug.Log("pickup.OnExit finish"); }; create.OnEnter += a => { print("entro en el create.onEnter"); _ent.GoTo(_target.transform.position); _ent.OnHitItem += PerformCreate; print("salgo en el create.onEnter"); }; create.OnExit += a => { action_started = false; _ent.OnHitItem -= PerformCreate; }; attack.OnEnter += a => { _ent.GoTo(_target.transform.position); _ent.OnHitItem += PerformAttack; }; attack.OnExit += a => { action_started = false; _ent.OnHitItem -= PerformAttack; }; super_attack.OnEnter += a => { _ent.GoTo(_target.transform.position); _ent.OnHitItem += PerformSuperAttack; }; super_attack.OnExit += a => { action_started = false; _ent.OnHitItem -= PerformSuperAttack; }; upgrade.OnEnter += a => { _ent.GoTo(_target.transform.position); _ent.OnHitItem += PerformUpgrade; }; upgrade.OnExit += a => { action_started = false; _ent.OnHitItem -= PerformUpgrade; }; planStep.OnEnter += a => { if (shouldRePlan) { var planner = this.GetComponent <Planner>(); _plan = planner.RecalculatePlan(GetCurState()); shouldRePlan = false; } Debug.Log("Plan next step"); var step = _plan.FirstOrDefault(); if (step != null) { Debug.Log("Next step:" + step.Item1 + "," + step.Item2); _plan = _plan.Skip(1); var oldTarget = _target; _target = step.Item2; if (!_fsm.Feed(step.Item1)) { _target = oldTarget; //Revert if feed failed. } } else { Debug.Log("TipitoAction.Success"); _fsm.Feed(IAAction.Success); } }; success.OnEnter += a => { Debug.Log("Success"); }; success.OnUpdate += () => { _ent.Jump(); }; StateConfigurer.Create(any) .SetTransition(IAAction.NextStep, planStep) .SetTransition(IAAction.FailedStep, idle) .Done(); StateConfigurer.Create(planStep) .SetTransition(IAAction.PickUp, pickup) .SetTransition(IAAction.Create, create) .SetTransition(IAAction.Attack, attack) .SetTransition(IAAction.SuperAttack, super_attack) .SetTransition(IAAction.Upgrade, upgrade) .SetTransition(IAAction.Success, success) .Done(); print("new fsm!"); _fsm = new EventFSM <IAAction>(idle, any); }
// returns True if they arrive close enough to the end of path public static bool Seek(Transform t, Entity e, Path p) { Node tNode = grid.NodeFromWorldPoint(t.position); // If the path exists, and the next node has a higher y value, jump; if (p.Count > 0) { if(e.jumpable) { if (p[0].position.y > tNode.position.y) { e.Jump(); } } else { if (p[0].position.y > tNode.position.y) { Vector2 newV = t.rigidbody.velocity; newV.y = e.speed; t.rigidbody.velocity = newV; } else if (p[0].position.y < tNode.position.y) { Vector2 newV = t.rigidbody.velocity; newV.y = -e.speed; t.rigidbody.velocity = newV; } } } else { t.rigidbody.velocity = new Vector2(0, t.rigidbody.velocity.y); return true; } // If we are within 3 nodes distance, we are close enough to the target; if (p.Count <= 3) { float slowerX = Mathf.Lerp(t.rigidbody.velocity.x, 0, Time.fixedDeltaTime * 10); t.rigidbody.velocity = new Vector2(slowerX, t.rigidbody.velocity.y); return t.rigidbody.velocity.x == 0; } if (tNode.position == p[0].position) { p.RemoveAt(0); p.RemoveAt(p.Count-1); } Vector3 direction = p[0].position - tNode.position; float newX = Mathf.Lerp(t.rigidbody.velocity.x, direction.x * e.speed, Time.deltaTime * 50); Vector2 oldV = t.rigidbody.velocity; oldV.x = newX; t.rigidbody.velocity = oldV; return false; }
public static bool FollowPath(Transform t, Entity e, Path p, float speed) { Node tNode = grid.NodeFromWorldPoint(t.position); if (p.Count > 0) { if (e.jumpable) { if (p[0].position.y > tNode.position.y) { e.Jump(); } } else { return FollowFlyingPath(t, e, p, speed); } if (tNode.position == p[0].position && p.Count > 1) { p.RemoveAt(0); } if (p.Count > 1) { Vector3 direction = p[0].position - tNode.position; float newX = Mathf.Lerp(t.rigidbody.velocity.x, direction.x * speed, Time.deltaTime * 50); Vector2 oldV = t.rigidbody.velocity; oldV.x = newX; t.rigidbody.velocity = oldV; return false; } else if (p.Count == 1) { float slowerX = Mathf.Lerp(t.rigidbody.velocity.x, 0, Time.fixedDeltaTime * 2); t.rigidbody.velocity = new Vector2(slowerX, t.rigidbody.velocity.y); return t.rigidbody.velocity.x == 0; } } return true; }