Ejemplo n.º 1
0
    public void SpawnWolf(Vector2 spawnPoint)
    {
        GameObject newWolf = (GameObject) Instantiate(wolfPrefab);

        Vector3 wolfPos = spawnPoint.ToWorldCoords();
        wolfPos.y += 0.5f;
        newWolf.transform.position = wolfPos;
        newWolf.transform.parent = this.transform;
        Brain wolfBrain = newWolf.GetComponent<Brain>();
        Genome newGenome = new Genome(bestGenome, baseMutationRate, this);
        aliveWolves.Add(newGenome);
        wolfBrain.memory.SetValue(new MemoryEntry("Genome", newGenome));
        wolfBrain.memory.SetValue(new MemoryEntry("StartPoint", spawnPoint));
        wolfBrain.Init(boxes, allObjects);
    }
Ejemplo n.º 2
0
    void Update()
    {
        if(curCooldown <= 0 && (Input.GetMouseButtonDown(0) || Input.GetButtonDown("Beacon"))) {
            foreach(SensableObject obj in allObjects.GetObjectsInRadius(transform.position, beaconRange)) {
                obj.obj.SendMessage("ImplantMemory",
                    new MemoryEntry("LastBeacon", new BeaconInfo(Time.time, legs.getPosition())),
                    SendMessageOptions.DontRequireReceiver);
            }
            GameObject beacon = (GameObject)Instantiate(beaconGFX, transform.position, Quaternion.identity);
            beacon.transform.parent = transform;
            beacon.SendMessage("ExpandRing", beaconRange);
            StartCoroutine(ResetBeacon());
        }

        Vector2 direction = new Vector2 (Input.GetAxis ("Horizontal"), Input.GetAxis ("Vertical"));
        Vector2 desiredVelocity = direction * speed;

        Vector3 startPosition = transform.position;

        velocity = Vector2.Lerp (velocity, desiredVelocity, Time.deltaTime * 5);

        float transformDistance = Time.deltaTime * velocity.magnitude;
        Vector3 lookDirection = lookAt.position - transform.position;
        lookDirection.y = 0;
        transform.rotation = Quaternion.LookRotation (lookDirection);
        RaycastHit hit;
        if (Physics.SphereCast (startPosition, 0.5f, velocity.ToWorldCoords (), out hit, transformDistance + 0.1f, collidesWith)) {
            Vector3 newWorldVelocity = Vector3.Reflect (velocity.ToWorldCoords (), hit.normal);
            velocity = new Vector2 (newWorldVelocity.x, newWorldVelocity.z);
        }

        legs.translate (velocity * Time.deltaTime);
    }
Ejemplo n.º 3
0
    public void Update()
    {
        if (steeringBehaviours.Count > 0)
        {
            acceleration = new Vector2(0,0);
            float sum = 0;
            /* The foreach statement in C# is internally identical to using a for loop over the iterator returned by 'GetEnumerator',
             * only with clearer syntax. Don't be afraid of them!
             * */
            foreach (SteeringBehaviour behaviour in steeringBehaviours)
            {
                //I have no idea why ClampMagnitude is static...
                Vector2 steering_force = Vector2.ClampMagnitude(behaviour.getDesiredVelocity() - velocity,maxForce);
                sum += behaviour.getWeight ();
                acceleration += behaviour.getWeight()*(steering_force/mass);
                //Debug.DrawRay(myTrans.position, behaviour.getDesiredVelocity().ToWorldCoords(), ColourUtility.GetSpectrum(sum));
            }
            if (sum == 0)
                return;
            acceleration /= sum;
            velocity = Vector2.ClampMagnitude(velocity + (acceleration * Time.deltaTime), maxSpeed);
            //Debug.Log (velocity);

            RaycastHit hit;
            if(Physics.SphereCast(myTrans.position, 0.5f, velocity.ToWorldCoords(), out hit, (velocity.magnitude * Time.deltaTime) + 0.1f, collidesWith)) {
                Vector3 newWorldVelocity = Vector3.Reflect(velocity.ToWorldCoords(), hit.normal);
                velocity = new Vector2(newWorldVelocity.x, newWorldVelocity.z);
            }

            Vector3 worldVelocity = new Vector3(velocity.x,0,velocity.y) * Time.deltaTime;
            myTrans.position += worldVelocity;
            if(velocity.sqrMagnitude > 0) {
                myTrans.rotation = Quaternion.LookRotation(new Vector3(velocity.x,0,velocity.y));
            }
            Debug.DrawRay(myTrans.position, new Vector3(velocity.x,0,velocity.y), Color.yellow);
            //movement.Translate (velocity * Time.deltaTime);
        }
    }