Exemplo n.º 1
0
    void UIAction(int what)
    {
        ComSat.Trace(this, "UIAction");
        if (what < 0 || what >= constructionPrefabs.Length)
        {
            return;
        }

        var mine = constructionPrefabs[what].GetComponent <Mine>();

        if (mine != null)
        {
            var sourceHere = Utility.GetThingAt <ResourceSource>(entity.position);
            if (sourceHere == null || sourceHere.hasMine || sourceHere.resource != mine.resource)
            {
                return;
            }

            buildPosition = sourceHere.GetComponent <Entity>().position;
            movable.Move(buildPosition);
            buildIndex = what;
            return;
        }

        ComSat.SpawnEntity(constructionPrefabs[what].gameObject, entity.team, entity.position, entity.rotation);
        ComSat.DestroyEntity(entity, DestroyReason.Tranformed);
    }
Exemplo n.º 2
0
 void Move(DVector2 location)
 {
     ComSat.Trace(this, "Move");
     moving      = true;
     destination = location;
     target      = null;
 }
Exemplo n.º 3
0
 public void Move(DVector2 location)
 {
     ComSat.Trace(this, "Move");
     moving      = true;
     destination = location;
     SendMessage("MoveDestinationChanged", SendMessageOptions.DontRequireReceiver);
 }
Exemplo n.º 4
0
 void Awake()
 {
     ComSat.Trace(this, "Awake");
     entity = gameObject.GetComponent <Entity>();
     entity.AddUpdateAction(TickUpdate);
     entity.AddInstantiateAction(OnInstantiate);
 }
Exemplo n.º 5
0
    void TickUpdate()
    {
        ComSat.Trace(this, "TickUpdate");
        if (ComSat.EntityExists(target))
        {
            destination = target.position;

            var radius = target.collisionRadius + entity.collisionRadius;
            if ((target.position - entity.position).sqrMagnitude < radius * radius)
            {
                var components = target.GetComponents(typeof(ISabotagable));

                foreach (var c in components)
                {
                    (c as ISabotagable).Sabotage();
                }

                ComSat.DestroyEntity(entity, DestroyReason.HitTarget);
            }
        }
        if (moving)
        {
            if ((destination - entity.position).sqrMagnitude < sqrPositioningAccuracy)
            {
                // Close enough.
                moving = false;
                motor.Stop();
            }
            else
            {
                motor.MoveTowards(destination);
            }
        }
    }
Exemplo n.º 6
0
    void TickUpdate()
    {
        ComSat.Trace(this, "TickUpdate");

        if (fireCycle != FireCycle.READY)
        {
            fireDelayTime -= ComSat.tickRate;
            if (fireDelayTime <= 0)
            {
                if (fireCycle == FireCycle.FIREDLEFT)
                {
                    // Fire right.
                    fireCycle = FireCycle.FIREDRIGHT;
                    FireOneBarrel(-1);
                    // This is enough time for the left barrel to recycle.
                    fireDelayTime = barrelRecycleTime - barrelDelay;
                }
                else if (fireCycle == FireCycle.FIREDRIGHT)
                {
                    // cycle complete.
                    fireCycle = FireCycle.READY;
                }
            }
        }
    }
Exemplo n.º 7
0
 void Awake()
 {
     ComSat.Trace(this, "Awake");
     moving = false;
     motor  = GetComponent <Vehicle>();
     entity = GetComponent <Entity>();
     entity.AddUpdateAction(TickUpdate);
 }
Exemplo n.º 8
0
 void Start()
 {
     ComSat.Trace(this, "Start");
     if (baseMesh && team != 0)
     {
         baseMesh.renderer.material.color = Utility.TeamColour(team);
     }
 }
Exemplo n.º 9
0
 void Awake()
 {
     ComSat.Trace(this, "Awake");
     entity     = GetComponent <Entity>();
     movable    = GetComponent <SimpleMovable>();
     buildIndex = -1;
     entity.AddUpdateAction(TickUpdate);
 }
Exemplo n.º 10
0
 void Move(DVector2 location)
 {
     ComSat.Trace(this, "Move");
     mode        = Mode.MOVE;
     target      = null;
     targets     = null;
     destination = location;
 }
Exemplo n.º 11
0
 void TickUpdate()
 {
     ComSat.Trace(this, "TickUpdate");
     age += ComSat.tickRate;
     if (age >= lifetime)
     {
         ComSat.DestroyEntity(entity, DestroyReason.OldAge);
     }
 }
Exemplo n.º 12
0
 void Attack(Entity[] targets)
 {
     ComSat.Trace(this, "Attack");
     mode           = Mode.ATTACK;
     target         = null;
     this.targets   = targets;
     movingToTarget = false;
     vehicle.Stop();
 }
Exemplo n.º 13
0
    void Awake()
    {
        ComSat.Trace(this, "Awake");
        entity = GetComponent <Entity>();
        entity.AddUpdateAction(TickUpdate);
        combatVehicle = GetComponent <CombatVehicle>();

        fireCycle = FireCycle.READY;
    }
Exemplo n.º 14
0
 public void TickUpdate()
 {
     ComSat.Trace(this, "TickUpdate");
     foreach (var a in updateActions)
     {
         a();
     }
     position += velocity * ComSat.tickRate;
 }
Exemplo n.º 15
0
    void Awake()
    {
        ComSat.Trace(this, "Awake");
        entity = GetComponent <Entity>();
        entity.AddUpdateAction(TickUpdate);
        combatVehicle = GetComponent <CombatVehicle>();

        missilesLoaded = maxMissiles;
    }
Exemplo n.º 16
0
    void Awake()
    {
        ComSat.Trace(this, "Awake");
        entity = GetComponent <Entity>();
        entity.AddUpdateAction(TickUpdate);
        vehicle = GetComponent <Vehicle>();

        mode = Mode.IDLE;
    }
Exemplo n.º 17
0
    void TickUpdate()
    {
        ComSat.Trace(this, "TickUpdate");

                if(!powerSink.poweredOn) {
                        target = null;
                        audio.Stop();
                        return;
                }

                if(!ComSat.EntityExists(target)) {
                        // Magic. Destroyed GameObjects compare against null.
                        // Explicitly set to null to avoid keeping it around.
                        target = null;
                        audio.Stop();

                        // Search for victims.
                        target = ComSat.FindEntityWithinRadius(entity.position, attackRange, entity.team);
                        if(target != null) {
                                audio.Play();
                        }
                } else {
                        var dp = target.position - entity.position;

                        DReal targetTurretAngle;

                        var projectileProjectile = projectilePrefab.GetComponent<Projectile>();
                        if(projectileProjectile != null && powerSink.Powered()) {
                                var aimSpot = Utility.PredictShot(entity.position, projectileProjectile.initialSpeed,
                                                                  target.position, target.velocity);
                                targetTurretAngle = DReal.Mod(DVector2.ToAngle(aimSpot - entity.position) - entity.rotation, DReal.TwoPI);
                        } else {
                                targetTurretAngle = DReal.Mod(DVector2.ToAngle(dp) - entity.rotation, DReal.TwoPI);
                        }

                        // Turn turret to point at target.
                        TurnTurret(targetTurretAngle);
                        // Fire when pointing the gun at the target.
                        if(targetTurretAngle == turretRotation) {
                                Fire();
                        }

                        // Stop shooting when out of range.
                        if(dp.sqrMagnitude >= attackRange * attackRange) {
                                audio.Stop();
                                target = null;
                        }
                }

                if(fireDelay > 0) {
                        fireDelay -= ComSat.tickRate;
                }
    }
Exemplo n.º 18
0
 void Awake() {
         ComSat.Trace(this, "Awake");
         entity = GetComponent<Entity>();
         entity.AddUpdateAction(TickUpdate);
         entity.AddDestroyAction(DestroyAction);
         powerSink = GetComponent<PowerSink>();
         buildQueue = new Queue<BuildCommandData>();
         resourceMan = FindObjectOfType<ResourceManager>();
         buildMan = FindObjectOfType<BuildManager>();
         playerInterface = FindObjectOfType<PlayerInterface>();
         ResetBuildTime();
 }
Exemplo n.º 19
0
    void Attack(Entity[] targets)
    {
        ComSat.Trace(this, "Attack");
        var validTargets = targets.Where(t => ComSat.EntityExists(t) && t.GetComponent(typeof(ISabotagable)) != null).OrderBy(t => (t.position - entity.position).sqrMagnitude);

        if (!validTargets.Any())
        {
            return;
        }
        target = validTargets.First();
        moving = true;
    }
Exemplo n.º 20
0
        void TickUpdate() {
                ComSat.Trace(this, "TickUpdate");
                if(sabotageTime > 0) {
                        sabotageTime -= ComSat.tickRate;
                }
                if (buildQueue.Any()) {
                        var buildMe = buildQueue.Peek();
                        var prefab = prefabs[buildMe.what];

                        if(delay > 0) {
                                var advance = ComSat.tickRate;
                                if(sabotageTime > 0) {
                                        advance /= sabotageTimeMultiplier;
                                }
                                if(!powerSink.Powered()) {
                                        advance /= 2;
                                }

                                var completion = advance / prefab.buildTime;
                                var totalRemaining = prefab.buildCost - usedResources;
                                partialMetalUnit += DReal.Min(completion * prefab.buildCost.Metal, totalRemaining.Metal);
                                partialSmokeUnit += DReal.Min(completion * prefab.buildCost.MagicSmoke, totalRemaining.MagicSmoke);
                                var rs = new ResourceSet { Metal = (int)partialMetalUnit, MagicSmoke = (int)partialSmokeUnit };
                                if (resourceMan.TakeResources(entity.team, rs)) {
                                        usedResources += rs;
                                        partialMetalUnit %= 1;
                                        partialSmokeUnit %= 1;
                                        delay -= advance;
                                } else {
                                        partialMetalUnit -= completion * prefab.buildCost.Metal;
                                        partialSmokeUnit -= completion * prefab.buildCost.MagicSmoke;
                                }
                        }
                        if(delay <= 0) {
                                if (!resourceMan.TakeResources(entity.team, prefab.buildCost - usedResources)) return;

                                // Timer expired and we're building something.
                                print("Build new " + prefab);
                                var prefabSize = (DReal)prefab.collisionRadiusNumerator / prefab.collisionRadiusDenominator;
                                var wiggle = ((ComSat.RandomValue() % 5) / 5) * ((ComSat.RandomValue() % 2 == 0) ? 1 : -1);
                                var position = prefab.buildAtPoint
                                        ? buildMe.position
                                        : (entity.position + DVector2.FromAngle(entity.rotation + wiggle) * (entity.collisionRadius + prefabSize + 2 + wiggle));
                                ComSat.SpawnEntity(entity, prefab.gameObject, position, 0);
                                if(buildMe.buildCollider != null) {
                                        buildMan.RemovePendingBuild(buildMe.buildCollider);
                                }
                                if (!buildMe.repeat) buildQueue.Dequeue();
                                ResetBuildTime();
                        }
                }
        }
Exemplo n.º 21
0
 public void Damage(int damage)
 {
     ComSat.Trace(this, "Damage");
     if (maxHealth == 0)
     {
         return;
     }
     health -= damage;
     if (health <= 0)
     {
         ComSat.DestroyEntity(this, DestroyReason.Damaged);
     }
 }
Exemplo n.º 22
0
    void Awake()
    {
        ComSat.Trace(this, "Awake");
                entity = GetComponent<Entity>();
                entity.AddUpdateAction(TickUpdate);
                powerSink = GetComponent<PowerSink>();

                target = null;
                turretRotation = 0;

                fireDelay = 0;

                barrelRecycleTime = (DReal)barrelRecycleTimeNumerator / barrelRecycleTimeDenominator;
    }
Exemplo n.º 23
0
 void UIAction(int what) {
         ComSat.Trace(this, "UIAction");
         if(what == clearQueue) {
                 foreach(var data in buildQueue) {
                         if(data.buildCollider != null) {
                                 buildMan.RemovePendingBuild(data.buildCollider);
                         }
                 }
                 buildQueue.Clear();
                 delay = 0;
                 resourceMan.AddResource(entity.team, ResourceType.Metal, usedResources.Metal);
                 resourceMan.AddResource(entity.team, ResourceType.MagicSmoke, usedResources.MagicSmoke);
                 ResetBuildTime();
         }
 }
Exemplo n.º 24
0
 void TickUpdate()
 {
     ComSat.Trace(this, "TickUpdate");
     if (moving)
     {
         if ((destination - entity.position).sqrMagnitude < sqrPositioningAccuracy)
         {
             // Close enough.
             moving = false;
             motor.Stop();
         }
         else
         {
             motor.MoveTowards(destination);
         }
     }
 }
Exemplo n.º 25
0
    void TickUpdate()
    {
        ComSat.Trace(this, "TickUpdate");
        if (ComSat.EntityExists(target))
        {
            var dir         = target.position - entity.position;     // also vector to dest.
            var targetAngle = DVector2.ToAngle(dir);
            var baseAngle   = Utility.CalculateNewAngle(entity.rotation, targetAngle, DReal.Radians(turnSpeed));
            entity.rotation = baseAngle;
        }
        entity.velocity = DVector2.FromAngle(entity.rotation) * speed;
        DVector2 newPosition = entity.position + entity.velocity * ComSat.tickRate;

        // FIXME: this should do something to account for hitting fast-moving projectiles.
        DVector2 hitPosition;
        Entity   hit = ComSat.LineCast(entity.position, newPosition, out hitPosition, entity.team);

        if (hit != null && (!hit.hitOnlyIfTargetted || hit == target))
        {
            hit.Damage((int)ComputeDamage());

            var position = new Vector3((float)hitPosition.y, 0, (float)hitPosition.x);
            var rotation = Quaternion.AngleAxis((float)entity.rotation, Vector3.up);
            if (impactPrefab != null && ComSat.RateLimit())
            {
                ObjectPool.Instantiate(impactPrefab, position, rotation);
            }

            //if(trail) {
            //        trail.transform.parent = null;
            //        trail.autodestruct = true;
            //        trail = null;
            //}

            if (!penetrates || speed < minPenetrationSpeed)
            {
                ComSat.DestroyEntity(entity, DestroyReason.HitTarget);
                return;
            }
            else
            {
                speed -= penetrationSpeedReduction;
            }
        }
    }
Exemplo n.º 26
0
    // This could be smarter. If dest is too close & perpendicular, then the tank
    // can end up circling around.
    public void MoveTowards(DVector2 dest)
    {
        ComSat.Trace(this, "MoveTowards");
        var dir         = dest - entity.position; // also vector to dest.
        var targetAngle = DVector2.ToAngle(dir);
        var baseAngle   = Utility.CalculateNewAngle(entity.rotation, targetAngle, DReal.Radians(turnSpeed));

        entity.rotation = baseAngle;

        // Move along current heading. Ramp speed up as the angle gets closer.
        // Augh.
        // [-pi,pi] => [0,2pi]
        if (targetAngle < 0)
        {
            targetAngle += DReal.TwoPI;
        }
        // Get targetAngle within +/- pi of baseAngle.
        if (targetAngle < baseAngle - DReal.PI)
        {
            targetAngle += DReal.TwoPI;
        }
        else if (targetAngle > baseAngle + DReal.PI)
        {
            targetAngle -= DReal.TwoPI;
        }
        var diff = DReal.Abs(baseAngle - targetAngle);

        if (canMoveWithoutTurning || diff < maxMoveAngle)
        {
            var distance = dir.magnitude;
            //print("Distance: " + distance + "  speed is: " + tickSpeed);
            var speed = minSpeed + (maxSpeed - minSpeed) * (1 - (diff / DReal.PI));
            if (distance < speed)
            {
                speed = DReal.Max(minSpeed, distance);
            }
            entity.velocity = canMoveWithoutTurning ? dir.normalized * speed : DVector2.FromAngle(baseAngle) * speed;
        }
        else
        {
            Stop();
        }
    }
Exemplo n.º 27
0
 void TickUpdate()
 {
     ComSat.Trace(this, "TickUpdate");
     if (ComSat.EntityExists(target))
     {
         moving      = true;
         destination = target.position;
         if ((destination - entity.position).sqrMagnitude < detonateRange * detonateRange)
         {
             Detonate();
             return;
         }
     }
     else if (targets != null && targets.Any())
     {
         PickNewTarget();
     }
     if (moving)
     {
         if ((ComSat.RandomValue() % 500) == 0)
         {
             Detonate();
             return;
         }
         if ((destination - entity.position).sqrMagnitude < sqrPositioningAccuracy)
         {
             // Close enough.
             moving = false;
             motor.Stop();
         }
         else
         {
             motor.MoveTowards(destination);
         }
     }
 }
Exemplo n.º 28
0
 public void Sabotage() {
         ComSat.Trace(this, "Sabotage");
         sabotageTime += sabotageRepairTime;
 }
Exemplo n.º 29
0
 public void Stop()
 {
     ComSat.Trace(this, "Stop");
     entity.velocity = DVector2.FromAngle(entity.rotation) * minSpeed;
 }
Exemplo n.º 30
0
 void Awake()
 {
     ComSat.Trace(this, "Awake");
     entity = GetComponent <Entity>();
 }