Beispiel #1
0
        public override void Update(float aDeltaTime)
        {
            float          dist  = 0.0f;
            float          ang   = 0.0f;
            Vector2        force = new Vector2();
            MagnetNode     magnet;
            MagnetableNode item;

            for (int i = 0, n = _magnetNodes.Count; i < n; i++)
            {
                magnet = _magnetNodes[i];
                for (int j = _magnetableNodes.Count - 1; j >= 0; j--)
                {
                    item = _magnetableNodes[j];
                    item.Magnetable.lifeTime += aDeltaTime;
                    dist = AntMath.Distance(magnet.entity.Position, item.entity.Position);
                    if ((magnet.Magnet.magnetKind == item.Magnetable.kind ||
                         magnet.Magnet.magnetKind == ItemKind.All) &&
                        dist < magnet.Magnet.distance &&
                        item.Magnetable.lifeTime > 2.0f)
                    {
                        ang     = AntMath.AngleRad(item.entity.Position, magnet.entity.Position);
                        force.x = magnet.Magnet.force * Mathf.Cos(ang);
                        force.y = magnet.Magnet.force * Mathf.Sin(ang);
                        item.Magnetable.body.AddForce(force, ForceMode2D.Impulse);

                        if (dist <= magnet.Magnet.collectDistance)
                        {
                            CollectItem(magnet, item);
                            Engine.RemoveEntity(item.entity);
                            GameObject.Destroy(item.entity.gameObject);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Updates vision sensor.
        /// </summary>
        private void UpdateVision()
        {
            var G = Game.Instance;

            // 1. See on the items.
            // --------------------
            float   dist;
            float   angle;
            Vector2 seeTo;
            Vector2 seeFrom;

            for (int i = G.collectableItems.Count - 1; i >= 0; i--)
            {
                // Calc distance to the item.
                dist = AntMath.Distance(_t.position, G.collectableItems[i].Position);
                if (dist < _control.visionRadius)
                {
                    // If distance in our vision field.
                    // Try to make raycast to it.
                    angle   = AntMath.AngleRad(_t.position, G.collectableItems[i].Position);
                    seeFrom = new Vector2(
                        _t.position.x + 0.4f * Mathf.Cos(angle),
                        _t.position.y + 0.4f * Mathf.Sin(angle)
                        );

                    seeTo = new Vector2(
                        dist * Mathf.Cos(angle),
                        dist * Mathf.Sin(angle)
                        );

                    var hit = Physics2D.Raycast(seeFrom, seeTo);
                    if (hit.collider != null)
                    {
                        var ci = hit.collider.GetComponent <CollectableItem>();
                        if (ci != null)
                        {
                            // Yay! We actually can see some item.
                            // Just add it to our see list.
                            AddItem(ci);

                            // Save vision points for debug drawing.
                            if (showVision)
                            {
                                seeTo += (Vector2)G.collectableItems[i].Position;
                                _seeFrom.Add(seeFrom);
                                _seeTo.Add(G.collectableItems[i].Position);
                            }
                        }
                    }
                }
            }

            // 2. See on the enemies.
            // ----------------------
            for (int i = G.tanks.Count - 1; i >= 0; i--)
            {
                if (G.tanks[i].team == _control.team)
                {
                    // Friendly tank. Skip...
                    continue;
                }

                // Calc distance to the enemy tank.
                dist = AntMath.Distance(_t.position, G.tanks[i].Position);
                if (dist < _control.visionRadius)
                {
                    // If distance in our vision field.
                    // Try to make raycast to it.
                    angle   = AntMath.AngleRad(_t.position, G.tanks[i].Position);
                    seeFrom = new Vector2(
                        _t.position.x + 0.4f * Mathf.Cos(angle),
                        _t.position.y + 0.4f * Mathf.Sin(angle)
                        );

                    seeTo = new Vector2(
                        dist * Mathf.Cos(angle),
                        dist * Mathf.Sin(angle)
                        );

                    var hit = Physics2D.Raycast(seeFrom, seeTo);
                    if (hit.collider != null)
                    {
                        var tc = hit.collider.GetComponent <TankControl>();
                        if (tc != null && tc.team != _control.team)
                        {
                            // We actually can see enemy tank.
                            // Add it to our see list.
                            AddTank(tc);

                            // Save vision points for debug drawing.
                            if (showVision)
                            {
                                seeTo += (Vector2)G.tanks[i].Position;
                                _seeFrom.Add(seeFrom);
                                _seeTo.Add(G.tanks[i].Position);
                            }
                        }
                    }
                }
            }
        }
Beispiel #3
0
        public override void Execute(float aDeltaTime, float aTimeScale)
        {
            if (_targetItem != null)
            {
                float dist = AntMath.Distance(_control.Position, _targetItem.Position);
                if (dist <= _control.magnetRadius)
                {
                    // Magneting our loot!
                    float angle = AntMath.AngleRad(_targetItem.Position, _control.Position);
                    var   force = new Vector2(
                        0.1f * Mathf.Cos(angle),
                        0.1f * Mathf.Sin(angle)
                        );

                    _targetItem.AddForce(force);
                    if (dist < _control.collectRadius)
                    {
                        // Yay! We got the item!
                        _targetItem.Collect();

                        switch (_itemKind)
                        {
                        case ItemKind.Gun:
                            if (_control.Tower.IsHasBomb)
                            {
                                // Drop bomb on the map.
                                Game.Instance.SpawnItem(
                                    _control.Position,
                                    ItemKind.Bomb,
                                    _control.transform.parent.transform
                                    );
                            }
                            _control.Tower.IsHasGun = true;
                            break;

                        case ItemKind.Ammo:
                            _control.Tower.GiveAmmo(3);
                            break;

                        case ItemKind.Bomb:
                            if (_control.Tower.IsHasGun)
                            {
                                // Drop gun on the map.
                                Game.Instance.SpawnItem(
                                    _control.Position,
                                    ItemKind.Gun,
                                    _control.transform.parent.transform
                                    );
                            }
                            _control.Tower.IsHasBomb = true;
                            break;

                        case ItemKind.Repair:
                            _control.Health = _control.maxHealth;
                            break;
                        }

                        Finish();
                    }
                }
            }
            else
            {
                // Item is lost :(
                // Finish the action.
                Finish();
            }
        }