Esempio n. 1
0
    IEnumerator Start()
    {
        // Set up the decision tree
        Decision isEnemy            = new DecisionIsEnemy(this);
        Decision isAirplane         = new DecisionIsAirplane(this);
        Decision isDistanceLessThan = new DecisionIsDistanceLessThan(this, 30);
        // To do : Create an isVehicle object
        DecisionIsVehicle isVehicle   = new DecisionIsVehicle(this);
        Action            fireMissile = new ActionFireMissile(this);
        // To do : Create a laser-firing-action object.
        Action fireLaser = new ActionFireLaser(this);
        // To do : Create a cannon-firing-action object.
        Action fireCannon = new ActionFireCannon(this);

        Decision decisionTree = isEnemy;

        isEnemy.trueNode  = isDistanceLessThan;
        isEnemy.falseNode = null;

        isDistanceLessThan.trueNode  = isAirplane;
        isDistanceLessThan.falseNode = null;

        isAirplane.trueNode = fireMissile;

        // To do : Connect the falseNode of isAirplane to isVehicle
        isAirplane.falseNode = isVehicle;

        // To do : Set the child nodes with proper actions
        isVehicle.trueNode  = fireCannon;
        isVehicle.falseNode = fireLaser;

        yield return(null);

        while (true)       // main loop
        {
            DoRadarScan(); // Scan a new unit or update the scanned unit

            // Get an action from the decision tree
            Action action = decisionTree.MakeDecision() as Action;
            if (action != null)
            {
                if (action.Perform())                    // action performed
                {
                    yield return(new WaitForSeconds(3)); // wait for 3 sec

                    scannedUnit = null;
                }
            }

            if (scannedUnit != null && scannedUnit.distance < 0)
            {
                actionDisplay = "No action";
                yield return(new WaitForSeconds(3)); // wait for 3 sec

                scannedUnit = null;
            }

            yield return(null);
        }
    }
Esempio n. 2
0
    void DoRadarScan()
    {
        if (scannedUnit == null)
        {
            actionDisplay = "";
            scannedUnit   = new ScannedUnit();
        }

        scannedUnit.Update(); // Update its distance
    }