Example #1
0
 // 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);
     }
 }
Example #2
0
 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();
 }
Example #3
0
    // Update is called once per frame
    void Update()
    {
        float ACCEL = 20f;
        float MAXV  = 5f;
        float ROTV  = 4 * Mathf.PI;

        if (!statusMap.has(State.DEAD))
        {
            Vector2 input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
            if (input.x != 0f)
            {
                hasMovedHorizontally = true;
            }
            if (input.y != 0f)
            {
                hasMovedVertically = true;
            }
            Vector2 desiredV = MAXV * input;
            rb.AddForce(ACCEL * (desiredV - rb.velocity));
            if (rb.velocity.sqrMagnitude > MAXV * MAXV)
            {
                rb.velocity = MAXV * rb.velocity.normalized;
            }
            else if (rb.velocity.sqrMagnitude < 1f)
            {
            }
            echolocate();
            // look at mouse
            Vector2 mouse = Scene.get().getWorldMousePos();
            turnToward(mouse, ROTV * Time.deltaTime);
            if (Input.GetMouseButton(0))
            {
                attack(mouse);
            }
        }
        else
        {
            HUD.takeDamage();
        }

        actionMap.update(Time.deltaTime);
        statusMap.update(Time.deltaTime);

        safe = (Scene.getTile(transform.position) == Tile.SAFE);
        if (safe)
        {
            health = maxHealth;
            HUD.setHealth(health);
            // respawn at center of tile
            respawnPoint = Scene.get().map.mapToGame(Scene.get().map.gameToMap(transform.position));
        }
        /* DIALOG / HELP */

        if (statusMap.has(State.DEAD))
        {
            HUD.setText("", 0f);
        }
        else if (!hasMovedHorizontally && !hasMovedVertically)
        {
            HUD.setText("Move with WASD", 0.1f);
        }
        else if (shotsFired < 2)
        {
            if (!HUD.hasText())
            {
                HUD.setText("Shoot with the mouse", 0.1f);
            }
        }
        else if (!dangerMessaged || !safetyMessaged)
        {
            if (!dangerMessaged && !HUD.hasText() && !safe)
            {
                HUD.setText("Watch out for moving shapes!", 3f);
                dangerMessaged = true;
            }
            if (!safetyMessaged && !HUD.hasText() && safe)
            {
                HUD.setText("These blue areas are safe.", 3f);
                safetyMessaged = true;
            }
        }
    }
Example #4
0
    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
    }