private void attack(Vector2 mouse) { if (actionMap.ready(ATTACK)) { gunAudio.PlayOneShot(shootSound, 0.5f); ++shotsFired; actionMap.use(ATTACK, null); GameObject shot = Instantiate(bulletObj); shot.transform.position = gun.position; shot.transform.rotation = gun.rotation; int WALL_MASK = 1 << 10; float shotRange = 15f; RaycastHit2D hit = Physics2D.Raycast(gun.position, gun.right, shotRange, WALL_MASK); if (hit.collider != null) { shotRange = hit.distance; } shot.transform.localScale = new Vector3(shotRange, 1f, 1f); /* * GameObject expl = Instantiate(explosionObj); * expl.transform.position = mouse; * float ERAD = 0.8f; * foreach (Enemy e in Scene.getEnemies()) { * if ((mouse - (Vector2) e.transform.position).sqrMagnitude < ERAD * ERAD) { * e.damage(1f); * } * } */ } }
// Update is called once per frame void Update() { actionMap.update(Time.deltaTime); if (actionMap.ready(0)) { actionMap.use(0, null); //Scene.echo(transform.position, 4f, 2f, 32); } }
public void FixedUpdate() { //var rb = GetComponent<Rigidbody2D>(); //rb.velocity = transform.up * MAXV; if (target != null) { var delta = target.transform.position - transform.position; var dist = delta.magnitude; if (!hasWayPoint && dist > (2 + DIST_PER_WAYPOINT)) { var randomRotation = Quaternion.AngleAxis(Random.Range(-35f, 35f), Vector3.forward); wayPoint = transform.position + DIST_PER_WAYPOINT * (randomRotation * delta.normalized); hasWayPoint = true; } if (hasWayPoint) { seek(wayPoint); // Check if the alien is as close to the destination as the waypoint is. if ((target.transform.position - wayPoint).magnitude + 0.1f >= dist) { hasWayPoint = false; } } if (!hasWayPoint && dist < 2) { if (actionMap.ready(ATTACK)) { actionMap.use(ATTACK, null); target.GetComponent <Planet>().damage(1); } flee(target); } else { seek(target); } } actionMap.update(Time.fixedDeltaTime); base.FixedUpdate(); }
public void FixedUpdate() { nearbyUnits.Update(); nearbyBuildings.Update(); Steering steering = GetComponent <Steering>(); float maxSpeed = steering.getMaxSpeed(); // move to destination (queue if necessary) if (group != null) { if (!hasDest || (path.goal - group.getDest()).sqrMagnitude > group.radius * group.radius) { moveTo(group.getDest(), group.radius); } } if (hasDest && canMove()) { path.followPath(steering, seekBehaviour, arrivalBehaviour, brakeBehaviour); } else { steering.updateWeight(seekBehaviour, 0f); steering.updateWeight(arrivalBehaviour, 0f); steering.updateWeight(brakeBehaviour, 2f); } statusMap.update(Time.fixedDeltaTime); actionMap.update(Time.fixedDeltaTime); float maxdd = attackRange * attackRange; if (canAttack()) { foreach (Neighbour <Steering> otherUnitNeighbour in nearbyUnits) { Unit otherUnit = otherUnitNeighbour.obj.GetComponent <Unit>(); if (otherUnitNeighbour.dd > maxdd) { break; } if (otherUnit.owner != owner) { actionMap.use(ATTACK, otherUnit); // look at Vector2 offset = otherUnit.transform.position - transform.position; targetDir = Mathf.Rad2Deg * Mathf.Atan2(offset.y, offset.x); break; } } } if (canAttack()) { foreach (Neighbour <Building> building in nearbyBuildings) { if (building.dd > maxdd) { break; } Building other = building.obj; if (other.owner != owner) { actionMap.use(ATTACK, other); // look at Vector2 offset = other.transform.position - transform.position; targetDir = Mathf.Rad2Deg * Mathf.Atan2(offset.y, offset.x); break; } } } if (canTrade()) { bool isIdle = false; if (hasDest && path.arrived) { //decide what to do next Vector2 dropoff = owner.getBase().getDock(); if (tradeDest != null && path.goal == tradeDest.getDock()) { // pick up resource = tradeDest.resource; carrying = tradeDest.collect(capacity); followPath(owner.getReturnRoute(tradeDest)); resourceObject.SetActive(true); float sz = 0.25f + 0.75f * carrying / capacity; resourceObject.transform.localScale = new Vector3(sz, sz, sz); } else if (path.goal == dropoff) { // drop off owner.collect(resource, carrying); carrying = 0f; resourceObject.SetActive(false); float maxProfit = -1f; foreach (Building building in owner.tradeWith) { Path tradeRoute = owner.getTradeRoute(building); float expectedProfit = Mathf.Max(building.expectedProfit(tradeRoute.length / maxSpeed) - capacity * owner.getTraders(building).Count, 0f); expectedProfit = maxSpeed * expectedProfit / (2 * tradeRoute.length); if (expectedProfit > maxProfit) { maxProfit = expectedProfit; setTradeDest(building); followPath(tradeRoute); } } } else if (carrying > 0f) { moveTo(dropoff, radius); } else { isIdle = true; } } if (isIdle || !hasDest) { // stopped in middle of nowhere float maxProfit = -1f; foreach (Building building in owner.tradeWith) { Path tradeRoute = Pathing.findPath(transform.position, building.getDock(), radius); float expectedProfit = Mathf.Max(building.expectedProfit(tradeRoute.length / maxSpeed) - capacity * owner.getTraders(building).Count, 0f); float tripDuration = tradeRoute.length / maxSpeed + owner.getReturnRoute(building).length / maxSpeed; expectedProfit = expectedProfit / tripDuration; if (expectedProfit > maxProfit) { maxProfit = expectedProfit; setTradeDest(building); followPath(tradeRoute); } } } } // repairs }