Esempio n. 1
0
    /// <summary>
    /// 查找敌人
    /// </summary>
    private void FindEnemy()
    {
        ArrayList list = GameMgr.instance.GetEnemyList();

        mTrg = null;
        foreach (EnemyNode enemy in list)
        {
            Vector3 pos1 = transform.position;
            Vector3 pos2 = enemy.transform.position;

            float dis = Vector2.Distance(new Vector2(pos1.x, pos1.y), new Vector2(pos2.x, pos2.y));

            if (dis > attackRange)
            {
//                Debug.Log(string.Format("敌人{0} 未进入攻击范围,距离:{1}", enemy.name, dis));
            }
            else
            {
                if (enemy != mTrg)
                {
                    mTrg = enemy;
                }
//                Debug.LogError(string.Format("敌人{0} 进入攻击范围,距离:{1}", enemy.name, dis));
            }
        }
    }
Esempio n. 2
0
    private void OnSceneGUI()
    {
        //Handles.PositionHandle(transform.position, Quaternion.identity);

        Vector3 lastNodePos = transform.position;

        for (int i = 0; i < enemy.nodes.Count; i++)
        {
            EnemyNode node = enemy.nodes[i];

            EditorGUI.BeginChangeCheck();
            Vector3 wPos = transform.TransformPoint(node.position);
            Vector3 nPos = Handles.PositionHandle(wPos, Quaternion.identity);

            Handles.DrawDottedLine(lastNodePos, nPos, 10);

            if (EditorGUI.EndChangeCheck())
            {
                Undo.RecordObject(target, "Changed node pos " + i);
                node.position = transform.InverseTransformPoint(nPos);
            }

            lastNodePos = nPos;
        }
    }
Esempio n. 3
0
        private void updateEnemyBlock(EnemyBlockNode block)
        {
            int min_x = int.MaxValue, min_y = int.MaxValue, max_x = 0, max_y = 0;

            foreach (Node enemies in lst_enemies)
            {
                EnemyNode en = (EnemyNode)enemies;
                // min calcul
                if (min_x > en.pos.point.x)
                {
                    min_x = (int)en.pos.point.x;
                }

                if (min_y > en.pos.point.y)
                {
                    min_y = (int)en.pos.point.y;
                }

                // max calcul
                if (max_x < en.pos.point.x + en.display.bitmap.Width)
                {
                    max_x = (int)en.pos.point.x + en.display.bitmap.Width;
                }

                if (max_y < en.pos.point.y + en.display.bitmap.Height)
                {
                    max_y = (int)en.pos.point.y + en.display.bitmap.Height;
                }
            }

            block.block.upperLeft.x   = min_x;
            block.block.upperLeft.y   = min_y;
            block.block.bottomRight.x = max_x;
            block.block.bottomRight.y = max_y;
        }
Esempio n. 4
0
    void LoadEnemy()
    {
        CreateEnemyNode();

        EnemyNode enemy = new EnemyNode(GetXmlNode(XmlNodes.Enemey.enemy));

        SingletonManager.Enemy.GetComponent <EnemyAiScript>().SetSavedData(enemy.GetEnemyData());
    }
Esempio n. 5
0
        void StopEnemyOnDeath(int targetID)
        {
            EnemyNode node = nodesDB.QueryNode <EnemyNode>(targetID);

            node.movementComponent.navMesh.enabled = false;
            var capsuleCollider = node.movementComponent.capsuleCollider;

            capsuleCollider.isTrigger = true;
            capsuleCollider.GetComponent <Rigidbody>().isKinematic = true;
        }
    /// <summary>
    /// Spawnea un enemigo de un nodo de enmigos
    /// </summary>
    /// <param name="node">Node de enemigos</param>
    /// <returns>Enemigo spawneado</returns>
    public GameObject SpawnEnemyFromNode(EnemyNode node)
    {
        SpawningPositions[] spawners = node.spawningPos;
        int     pos      = Random.Range(1, 100) % spawners.Length;
        Vector2 spawnPos = CalculateSpawnPoint(spawners[pos]);

        GameObject spawnedEnemy = SpawnEnemyOfType(node.enemyType, node.bases, spawnPos);

        return(spawnedEnemy);
    }
Esempio n. 7
0
    public void Dequeue()
    {
        //Change first node for next node
        first = first.NextNode;

        //If there's no next node, the queue is empty
        if (first == null)
        {
            last = null;
        }
    }
Esempio n. 8
0
    public void InitData(EnemyNode enemy)
    {
        this.enemy = enemy;
        TweenMove move = gameObject.AddComponent<TweenMove>();
        move.from = transform.position;
        move.to = enemy.transform;
        move.duration = 0.5f;
        move.PlayForward();

        mSprite = GetComponent<UISprite>();
    }
Esempio n. 9
0
    public void InitData(EnemyNode enemy)
    {
        this.enemy = enemy;
        TweenMove move = gameObject.AddComponent <TweenMove>();

        move.from     = transform.position;
        move.to       = enemy.transform;
        move.duration = 0.5f;
        move.PlayForward();

        mSprite = GetComponent <UISprite>();
    }
    /// <summary>
    /// Se encarga de spawnear los enemigos de un nodo y suscribir el reductor
    /// del contador al evento de muerte
    /// </summary>
    /// <param name="enemies">Nodo de enemigos a spawnear</param>
    public IEnumerator SpawnEnemies(EnemyNode enemies)
    {
        Debug.Log("Spawneando enemigos!");
        AddNodeCount(enemies);
        for (int i = 0; i < enemies.quantity; i++)
        {
            yield return(new WaitForSeconds(enemies.delay[i]));

            GameObject enemy = SpawningSystem.Manager.SpawnEnemyFromNode(enemies);
            enemy.GetComponent <HealthManager>().onDepletedLife.AddListener((a, b) => ReduceEnemyCount());
        }
    }
Esempio n. 11
0
        public void Remove(INode obj)
        {
            if (obj is EnemyNode)
            {
                EnemyNode enemyNode = (obj as EnemyNode);

                enemyNode.targetTriggerComponent.entityInRange -= CheckTarget;
            }
            else
            {
                _targetNode = null;
            }
        }
Esempio n. 12
0
        public void Add(INode obj)
        {
            if (obj is EnemyNode)
            {
                EnemyNode enemyNode = (obj as EnemyNode);

                enemyNode.targetTriggerComponent.entityInRange += CheckTarget;
            }
            else
            {
                _targetNode = obj as EnemyTargetNode;
            }
        }
Esempio n. 13
0
    /// <summary>
    ///     创建敌人
    /// </summary>
    /// <returns></returns>
    private EnemyNode CreateEnemy()
    {
        GameObject model = Instantiate(enemyModel);

        model.transform.parent           = transform;
        model.transform.localEulerAngles = Vector3.zero;
        model.transform.localScale       = Vector3.one;
        model.transform.position         = startPathNode.transform.position; //使用世界坐标直接指定位置

        EnemyNode enemy = model.GetComponent <EnemyNode>();

        enemy.startNode       = startPathNode;
        enemy.OnDeathCallBack = OnDeathCallBack;
        GameMgr.instance.enemyList.Add(enemy);
        return(enemy);
    }
Esempio n. 14
0
    /// <summary>
    ///     加载数据
    /// </summary>
    /// <returns></returns>
    private IEnumerator LoadWaveEnemy()
    {
        WaveData wave = DataMgr.instance.WaveList[mWaveIndex - 1];

        mRemainingEnemyNums = wave.nums;
        yield return(new WaitForSeconds(wave.cd));

        for (int i = 0; i < wave.nums; i++)
        {
            EnemyNode enemy   = CreateEnemy();
            int       enemyID = wave.randomId[Random.Range(0, wave.randomId.Length - 1)];
            enemy.Hp        = enemyID;
            enemy.moveSpeed = Random.Range(0.15f, 0.3f);
            yield return(new WaitForSeconds(wave.delay));
        }
    }
Esempio n. 15
0
 /// <summary>
 ///     死亡回调
 /// </summary>
 /// <param name="node"></param>
 private void OnDeathCallBack(EnemyNode node)
 {
     Destroy(node.gameObject);
     GameMgr.instance.enemyList.Remove(node);
     mRemainingEnemyNums--;
     if (GameMgr.instance.enemyList.Count <= 0)
     {
         mWaveIndex++;
         if (mWaveIndex > DataMgr.instance.WaveList.Count)
         {
             //TODO: 怪物死光游戏结束
         }
         else
         {
             StartCoroutine(LoadWaveEnemy());
         }
     }
 }
Esempio n. 16
0
 /// <summary>
 ///     死亡回调
 /// </summary>
 /// <param name="node"></param>
 private void OnDeathCallBack(EnemyNode node)
 {
     Destroy(node.gameObject);
     GameMgr.instance.enemyList.Remove(node);
     mRemainingEnemyNums--;
     if (GameMgr.instance.enemyList.Count <= 0)
     {
         mWaveIndex++;
         if (mWaveIndex > DataMgr.instance.WaveList.Count)
         {
             //TODO: 怪物死光游戏结束
         }
         else
         {
             StartCoroutine(LoadWaveEnemy());
         }
     }
 }
Esempio n. 17
0
    public void Enqueue(Enemy newEnemy)
    {
        EnemyNode newNode = new EnemyNode(newEnemy);

        //If last isn't null, add the new node as next node
        if (last != null)
        {
            last.NextNode = newNode;
        }


        last = newNode;


        if (first == null)
        {
            first = last;
        }
    }
Esempio n. 18
0
    public bool ChooseEnemyToLockTargetHeap()
    {
        bool      findOne   = false;
        float     lessAngle = 500.0f;
        EnemyNode node;

        targetHeap = new Heap <EnemyNode>(20);

        for (int i = 0; i < usedEnemy.Count; i++)
        {
            if (i == 20)
            {
                break;
            }
            Vector3 diffV = new Vector3(usedEnemy[i].transform.position.x - playerTransform.position.x, 0, usedEnemy[i].transform.position.z - playerTransform.position.z);
            float   dist  = diffV.sqrMagnitude;
            if (dist < 100.0f)
            {
                float angle = Vector3.Angle(playerTransform.forward, diffV);

                if (Mathf.Abs(angle) > 0.1f)
                {
                    node = new EnemyNode(usedEnemy[i], angle + Mathf.Sign(angle) * dist * 0.05f);
                }
                else
                {
                    node = new EnemyNode(usedEnemy[i], angle + dist * 0.05f);
                }

                if (Mathf.Abs(lessAngle) > Mathf.Abs(node.weight))
                {
                    lessAngle       = angle;
                    targetLockEnemy = node;
                    findOne         = true;
                }
                targetHeap.Add(targetLockEnemy);
            }
        }

        return(findOne);
    }
Esempio n. 19
0
    /// <summary>
    /// 查找敌人
    /// </summary>
    private void FindEnemy()
    {
        ArrayList list = GameMgr.instance.GetEnemyList();
        mTrg = null;
        foreach (EnemyNode enemy in list)
        {
            Vector3 pos1 = transform.position;
            Vector3 pos2 = enemy.transform.position;

            float dis = Vector2.Distance(new Vector2(pos1.x, pos1.y), new Vector2(pos2.x, pos2.y));

            if (dis > attackRange)
            {
//                Debug.Log(string.Format("敌人{0} 未进入攻击范围,距离:{1}", enemy.name, dis));
            }
            else
            {
                if (enemy != mTrg) mTrg = enemy;
//                Debug.LogError(string.Format("敌人{0} 进入攻击范围,距离:{1}", enemy.name, dis));
            }
        }
    }
 /// <summary>
 /// Le agrega al contador la cantidad de enemigos del nodo
 /// </summary>
 /// <param name="enemies">Nodo de enemigos</param>
 public void AddNodeCount(EnemyNode enemies)
 {
     iEnemyCount = iEnemyCount + enemies.quantity;
 }
Esempio n. 21
0
 public void InitializeQueue()
 {
     first = null;
     last  = null;
 }
Esempio n. 22
0
    public override void OnInspectorGUI()
    {
        Color defaultColor = GUI.backgroundColor;
        bool  editing      = false;

        //Body transform
        if (RaStyle.ObjectField(target, "Body", ref enemy.body))
        {
            editing = true;
        }

        //Projectile parent
        if (RaStyle.ObjectField(target, "Projectile transform", ref enemy.rangeParent))
        {
            editing = true;
        }

        //Default rotation
        if (RaStyle.Toggle(target, "Default is right?", ref enemy.defaultIsRight))
        {
            editing = true;
        }

        #region SPEED
        EditorGUILayout.LabelField("Speed", EditorStyles.boldLabel);
        EditorGUI.indentLevel = 1;
        GUI.backgroundColor   = RaStyle.HexColor("#B3B3B3");
        EditorGUILayout.BeginVertical("Box");

        //Speed
        EditorGUI.BeginChangeCheck();
        float bSpeed = EditorGUILayout.FloatField("Speed", enemy.speed);
        if (EditorGUI.EndChangeCheck())
        {
            Undo.RecordObject(target, "Changed speed");
            enemy.speed = bSpeed;
            editing     = true;
        }

        //speed boost
        EditorGUI.BeginChangeCheck();
        float bSpBoost = EditorGUILayout.Slider("Sign speed boost", enemy.speedBooster, 1, 3);
        if (EditorGUI.EndChangeCheck())
        {
            Undo.RecordObject(target, "Changed speed boost");
            enemy.speedBooster = bSpBoost;
            editing            = true;
        }
        EditorGUILayout.EndVertical();
        GUI.backgroundColor   = defaultColor;
        EditorGUI.indentLevel = 0;
        #endregion

        #region DISTANCE
        EditorGUILayout.LabelField("Distances", EditorStyles.boldLabel);
        EditorGUI.indentLevel = 1;
        GUI.backgroundColor   = RaStyle.HexColor("#B3B3B3");
        EditorGUILayout.BeginVertical("Box");

        //Direction
        if (RaStyle.Toggle(target, "Both direction", ref enemy.bothDirection))
        {
            editing = true;
        }

        //Distance
        if (RaStyle.Slider(target, "Sign distance", ref enemy.viewDistance, 1, 10))
        {
            editing = true;
        }

        //Checked layers
        if (RaStyle.LayerMask(target, "Check layer", ref enemy.seeCheckLayer))
        {
            editing = true;
        }


        EditorGUI.indentLevel = 0;
        GUI.backgroundColor   = defaultColor;
        EditorGUILayout.EndVertical();
        #endregion

        #region ATTACK

        EditorGUILayout.LabelField("Attack", EditorStyles.boldLabel);
        EditorGUI.indentLevel = 1;

        //Attacking layer
        if (RaStyle.EnumPopup(target, "Current layer", ref enemy.interactLayer))
        {
            editing = true;
        }

        //Attack type
        if (RaStyle.EnumPopup(target, "Attack type", ref enemy.attackType))
        {
            editing = true;
        }

        GUI.backgroundColor = RaStyle.HexColor("#B3B3B3");
        EditorGUILayout.BeginVertical("Box");

        //Attack damage
        if (RaStyle.Slider(target, "Attack damage", ref enemy.attackDamage, 1f, 100))
        {
            editing = true;
        }

        //Attack cooldown
        if (RaStyle.FloatField(target, "Attack cool down", ref enemy.attackCoolDown))
        {
            editing = true;
        }

        //MEELE attack
        if (enemy.attackType == EnemyType.meele)
        {
            //Attack distance
            if (RaStyle.Slider(target, "Projectile force", ref enemy.attackDistance, 1f, enemy.viewDistance))
            {
                editing = true;
            }
        }
        else //RANGE attack
        {
            //Projectile spawnpoint
            if (RaStyle.ObjectField(target, "Projectile spawn", ref enemy.projectileStart))
            {
                editing = true;
            }

            //Projectile prefab
            if (RaStyle.ObjectField(target, "Projectile prefab", ref enemy.rangeProjectile))
            {
                editing = true;
            }

            //Projectile force
            if (RaStyle.Slider(target, "Projectile force", ref enemy.projectileForce, 0.1f, 10))
            {
                editing = true;
            }

            //Destroy projectile
            if (RaStyle.Toggle(target, "Destroy prejectile", ref enemy.destroyProjectile))
            {
                editing = true;
            }

            //Destroy time
            if (enemy.destroyProjectile)
            {
                if (RaStyle.Slider(target, "Projectile TTL", ref enemy.destroyProjectileTime, 0, 5))
                {
                    editing = true;
                }
            }
        }

        EditorGUI.indentLevel = 0;
        GUI.backgroundColor   = defaultColor;
        EditorGUILayout.EndVertical();
        #endregion

        #region DAMAGE
        EditorGUILayout.LabelField("Damage", EditorStyles.boldLabel);
        EditorGUI.indentLevel = 1;
        GUI.backgroundColor   = RaStyle.HexColor("#B3B3B3");
        EditorGUILayout.BeginVertical("Box");

        //Meele damage
        if (RaStyle.Slider(target, "Meele damage", ref enemy.damageMeele, 1, 100))
        {
            editing = true;
        }

        //Range damage
        if (RaStyle.Slider(target, "Range damage", ref enemy.damageRange, 1, 100))
        {
            editing = true;
        }

        //Destroy on ded
        if (RaStyle.Toggle(target, "Destroy on death", ref enemy.destroyOnDeath))
        {
            editing = true;
        }

        //Destroy time
        if (enemy.destroyOnDeath)
        {
            if (RaStyle.FloatField(target, "Destroy time", ref enemy.destroyTime))
            {
                editing = true;
            }
        }


        EditorGUI.indentLevel = 0;
        GUI.backgroundColor   = defaultColor;
        EditorGUILayout.EndVertical();
        #endregion

        #region CANVAS
        EditorGUILayout.LabelField("Canvas", EditorStyles.boldLabel);
        EditorGUI.indentLevel = 1;
        GUI.backgroundColor   = RaStyle.HexColor("#B3B3B3");
        EditorGUILayout.BeginVertical("Box");

        //Always show canvas
        if (RaStyle.Toggle(target, "Always show canvas", ref enemy.alwaysShowCanvas))
        {
            editing = true;
        }

        //Canvas
        if (RaStyle.ObjectField(target, "Canvas", ref enemy.canvas))
        {
            editing = true;
        }

        //Progress bar
        if (RaStyle.ObjectField(target, "Progress bar", ref enemy.progress))
        {
            editing = true;
        }

        EditorGUI.indentLevel = 0;
        GUI.backgroundColor   = defaultColor;
        EditorGUILayout.EndVertical();
        #endregion

        if (editing)
        {
            EditorUtility.SetDirty(target);
        }

        #region NODES
        int delete = -1;
        nodesFold = EditorGUILayout.Foldout(nodesFold, "Nodes", new GUIStyle(EditorStyles.foldout)
        {
            fontStyle = FontStyle.Bold
        });
        if (nodesFold)
        {
            EditorGUILayout.BeginVertical();

            for (int i = 0; i < enemy.nodes.Count; i++)
            {
                EditorGUI.BeginChangeCheck();
                EnemyNode node = enemy.nodes[i];

                EditorGUILayout.BeginVertical("Textfield");

                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("Node " + (i + 1).ToString() + "   " + string.Format("x:{0:0.000} y:{1:0.000}", node.position.x, node.position.y));
                if (GUILayout.Button("Delete", GUILayout.Width(50)))
                {
                    delete = i;
                }
                EditorGUILayout.EndHorizontal();

                EditorGUILayout.BeginHorizontal();

                EditorGUILayout.LabelField("", GUILayout.Width(5));
                float wDelay = EditorGUILayout.FloatField("Wait Time", node.waitOnNode);

                EditorGUILayout.EndHorizontal();
                EditorGUILayout.EndVertical();

                if (EditorGUI.EndChangeCheck())
                {
                    Undo.RecordObject(target, "Changed node " + i);
                    node.waitOnNode = wDelay;
                    EditorUtility.SetDirty(target);
                }
            }

            if (GUILayout.Button("Add Node"))
            {
                Undo.RecordObject(target, "Add node");
                Vector3 last = Vector3.right;
                if (enemy.nodes.Count > 0)
                {
                    last = enemy.nodes[enemy.nodes.Count - 1].position + new Vector3(2, 0);
                }
                enemy.nodes.Add(new EnemyNode()
                {
                    position = last
                });
                EditorUtility.SetDirty(target);
            }

            EditorGUILayout.EndVertical();

            if (delete > -1)
            {
                Undo.RecordObject(target, "Delete node " + delete);
                enemy.nodes.RemoveAt(delete);
                EditorUtility.SetDirty(target);
            }
        }
        #endregion
    }
Esempio n. 23
0
        public override void update(float time, Graphics g)
        {
            if (runnable)
            {
                #region basics movement
                GunControlNode gunnode = (GunControlNode)lstgun.First();
                foreach (Node n in lstmovement.ToList()) // We transform to list to entity from node
                {
                    MovementNode mn   = (MovementNode)n;
                    Velocity     v    = mn.vitesse;
                    Position     p    = mn.pos;
                    Vector2D     newv = time * v.speedVect + p.point;
                    if (newv.x < size.Width || newv.x > 0)
                    {
                        p.point = newv;
                    }


                    if (newv.y < 0)
                    {
                        factory.removeEntity(mn.entity);
                        gunnode.gun.shoot = true;
                    }
                    else if (newv.y > size.Height)
                    {
                        factory.removeEntity(mn.entity);
                    }
                }
                #endregion

                #region enemies movement
                GameStateNode  gstatenode = null;
                EnemyBlockNode eblocknode = null;
                if (lst_game.Count > 0)
                {
                    gstatenode = (GameStateNode)lst_game.First();
                }
                if (lst_block.Count > 0)
                {
                    eblocknode = (EnemyBlockNode)lst_block.First();
                }
                // First we move enemies
                int compteur = 0;
                foreach (Node enemies in lst_enemies)
                {
                    EnemyNode en = (EnemyNode)enemies;
                    switch (eblocknode.block.direction)
                    {
                    case EnemyBlock.Direction.Right:
                        en.pos.point.x += en.enemy.vitesse.x * time + (gstatenode.gs.level * time);
                        break;

                    case EnemyBlock.Direction.Left:
                        en.pos.point.x -= en.enemy.vitesse.x * time + (gstatenode.gs.level * time);
                        break;
                    }

                    if (eblocknode.block.collided)
                    {
                        if (time == 0)
                        {
                            time = (float)0.005; // In case the draw update happens at impact
                        }
                        en.pos.point.y     += en.enemy.vitesse.y * time;
                        en.enemy.vitesse.y += size.Width / 10 + (gstatenode.gs.level * time);
                        en.enemy.vitesse.x += 3 * gstatenode.gs.level;
                    }
                }
                if (eblocknode.block.collided)
                {
                    eblocknode.block.collided = false;
                }
                updateEnemyBlock(eblocknode);

                #endregion
            }
        }
Esempio n. 24
0
        public override void update(float time, Graphics g)
        {
            if (runnable)
            {
                // We set up the gunnode to allow it to shoot again
                GunControlNode         gunctrlnode = null;
                GameStateNode          gstatenode  = null;
                SpaceShipCollisionNode sscn        = null;
                EnemyBlockNode         eblocknode  = null;


                if (lst_gun.Count > 0)
                {
                    gunctrlnode = (GunControlNode)lst_gun.First();
                }
                if (lst_game.Count > 0)
                {
                    gstatenode = (GameStateNode)lst_game.First();
                }
                if (lst_block.Count > 0)
                {
                    eblocknode = (EnemyBlockNode)lst_block.First();
                }

                #region bullet bunker collision
                foreach (Node bullet in lst_bullet.ToList()) // to list to remove entity while looping
                {
                    foreach (Node bunker in lst_bunker.ToList())
                    {
                        BulletCollisionNode bullcn = (BulletCollisionNode)bullet;
                        BunkerCollisionNode bunkcn = (BunkerCollisionNode)bunker;



                        if (bunkcn.bunker.life > 0 &&
                            CheckRectangleCollision(bullcn.display, bullcn.pos, bunkcn.display, bunkcn.pos) &&
                            CheckPixelCollision(bullcn.display, bullcn.pos, bunkcn.display, bunkcn.pos)
                            )
                        {
                            CheckPixelCollision(bullcn.display, bullcn.pos, bunkcn.display, bunkcn.pos, bullcn.bullet.damage);

                            bunkcn.bunker.life -= pixel_destroyed;
                            pixel_destroyed     = 0;
                            if (gunctrlnode != null && bullcn.bullet.ally)
                            {
                                gunctrlnode.gun.shoot = true;
                            }
                            ef.removeEntity(bullcn.entity);
                        }
                    }
                }
                #endregion

                #region enemy bullet collision
                gstatenode.gs.enemiesCount = lst_enemy.Count;
                foreach (Node bullet in lst_bullet.ToList()) // to list to remove entity while looping
                {
                    foreach (Node enemy in lst_enemy.ToList())
                    {
                        BulletCollisionNode bullcn = (BulletCollisionNode)bullet;
                        EnemyNode           en     = (EnemyNode)enemy;



                        if (en.enemy.life > 0 &&
                            bullcn.bullet.ally &&
                            CheckRectangleCollision(bullcn.display, bullcn.pos, en.display, en.pos) &&
                            CheckPixelCollision(bullcn.display, bullcn.pos, en.display, en.pos)
                            )
                        {
                            ef.removeEntity(bullcn.entity);
                            en.enemy.life -= 1;
                            if (en.enemy.life <= 0)
                            {
                                if (gstatenode != null)
                                {
                                    switch (en.enemy.type)
                                    {
                                    case Enemy.Type.Big:
                                        gstatenode.gs.score += 500;
                                        break;

                                    case Enemy.Type.Medium:
                                        gstatenode.gs.score += 300;
                                        break;

                                    case Enemy.Type.Small:
                                        gstatenode.gs.score += 100;
                                        break;
                                    }
                                }
                                ef.removeEntity(en.entity);
                            }
                            pixel_destroyed = 0;
                            if (gunctrlnode != null)
                            {
                                gunctrlnode.gun.shoot = true;
                            }

                            if (Game.rand.Next(20) == 1) // Drop a bonus
                            {
                                Array values = Enum.GetValues(typeof(Bonus.Type));
                                ef.CreateBonus(en.pos, (Bonus.Type)values.GetValue(Game.rand.Next(values.Length)));
                            }
                        }
                    }
                }
                #endregion

                #region spaceship bullet collision
                if (lst_spaceship.Count > 0)
                {
                    sscn = (SpaceShipCollisionNode)lst_spaceship.First();
                    foreach (Node bullet in lst_bullet.ToList())
                    {
                        BulletCollisionNode bullcn = (BulletCollisionNode)bullet;



                        if (sscn.spaceship.life > 0 &&
                            !bullcn.bullet.ally &&
                            CheckRectangleCollision(bullcn.display, bullcn.pos, sscn.display, sscn.pos) &&
                            CheckPixelCollision(bullcn.display, bullcn.pos, sscn.display, sscn.pos)
                            )
                        {
                            ef.removeEntity(bullcn.entity);
                            sscn.spaceship.life -= bullcn.bullet.damage;
                            gstatenode.gs.lives  = sscn.spaceship.life;
                            pixel_destroyed      = 0;
                        }
                    }
                }
                #endregion

                #region bullet bullet collision
                foreach (Node bullet in lst_bullet.ToList()) // to list to remove entity while looping
                {
                    foreach (Node bull in lst_bullet.ToList())
                    {
                        BulletCollisionNode bullcn = (BulletCollisionNode)bullet;
                        BulletCollisionNode en     = (BulletCollisionNode)bull;
                        if (bullcn.entity.Name != en.entity.Name)
                        {
                            if (
                                CheckRectangleCollision(bullcn.display, bullcn.pos, en.display, en.pos) &&
                                CheckPixelCollision(bullcn.display, bullcn.pos, en.display, en.pos)
                                )
                            {
                                ef.removeEntity(bullcn.entity);
                                ef.removeEntity(en.entity);
                                if (gunctrlnode != null)
                                {
                                    gunctrlnode.gun.shoot = true;
                                }
                            }
                        }
                    }
                }
                #endregion

                #region spaceship bonus collision
                if (lst_spaceship.Count > 0)
                {
                    sscn = (SpaceShipCollisionNode)lst_spaceship.First();
                    foreach (Node bonus in lst_bonus.ToList())
                    {
                        BonusCollisionNode bonusn = (BonusCollisionNode)bonus;



                        if (sscn.spaceship.life > 0 &&
                            CheckRectangleCollision(bonusn.display, bonusn.pos, sscn.display, sscn.pos) &&
                            CheckPixelCollision(bonusn.display, bonusn.pos, sscn.display, sscn.pos)
                            )
                        {
                            switch (bonusn.bonus.type)
                            {
                            case Bonus.Type.PlusLife:
                                sscn.spaceship.life++;
                                bonus_label = "+ 1 life";
                                break;

                            case Bonus.Type.ScoreBoost:
                                gstatenode.gs.score *= 2;
                                bonus_label          = "Score doubled";
                                break;

                            case Bonus.Type.RestoreBunker:
                                foreach (Node bunker in lst_bunker.ToList())
                                {
                                    ef.removeEntity(bunker.entity);
                                }
                                ef.CreateBunkerEntities(size);
                                bonus_label = "Bunker restored";
                                break;

                            case Bonus.Type.EnemySlow:
                                foreach (Node enemy in lst_enemy.ToList())
                                {
                                    EnemyNode enem = (EnemyNode)enemy;
                                    enem.enemy.vitesse.x /= 5;
                                }
                                bonus_label = "Enemies slowdown";
                                break;

                            case Bonus.Type.doubleShoot:
                                Gun gun = (Gun)sscn.entity.GetComponent("Gun");
                                gun.doubleShoot = true;
                                bonus_label     = "Double bullet";
                                break;

                            case Bonus.Type.controlledBullet:
                                Gun pisto = (Gun)sscn.entity.GetComponent("Gun");
                                pisto.controllableShoot = true;
                                bonus_label             = "Controlled Bullet";
                                break;
                            }
                            Console.WriteLine(bonus_label);
                            ef.removeEntity(bonusn.entity);
                            bonus_sound.Play();
                            gstatenode.gs.lives = sscn.spaceship.life;
                            pixel_destroyed     = 0;
                        }
                    }

                    if (g != null && compteur_label++ < 1000)
                    {
                        g.DrawString(
                            bonus_label,
                            Game.font,
                            Game.blackBrush,
                            size.Width - 130,
                            size.Height - 50
                            );
                    }
                    else if (g != null && compteur_label++ > 200)
                    {
                        compteur_label = 0;
                        bonus_label    = "";
                    }
                }
                #endregion


                #region enemyblock collision
                if (eblocknode.block.direction == EnemyBlock.Direction.Left && eblocknode.block.upperLeft.x <= 0)
                {
                    eblocknode.block.collided          = true;
                    eblocknode.block.direction         = EnemyBlock.Direction.Right;
                    eblocknode.block.shootProbability -= 10;
                }
                else if (eblocknode.block.direction == EnemyBlock.Direction.Right && eblocknode.block.bottomRight.x >= size.Width)
                {
                    eblocknode.block.collided          = true;
                    eblocknode.block.direction         = EnemyBlock.Direction.Left;
                    eblocknode.block.shootProbability -= 10;
                }

                if (lst_spaceship.Count > 0)
                {
                    sscn = (SpaceShipCollisionNode)lst_spaceship.First();
                    // we check the collision we the spaceship and then set the game to lose if collision occured
                    if (
                        sscn.pos.point.x + sscn.display.bitmap.Width >= eblocknode.block.upperLeft.x &&
                        sscn.pos.point.x <= eblocknode.block.bottomRight.x &&
                        sscn.pos.point.y + sscn.display.bitmap.Height >= eblocknode.block.upperLeft.y &&
                        sscn.pos.point.y <= eblocknode.block.bottomRight.y
                        )
                    {
                        gstatenode.gs.state = Game.State.Lost;
                    }
                }
                #endregion
            }
        }
Esempio n. 25
0
        public override void update(float time, Graphics g)
        {
            if (runnable)
            {
                EnemyBlockNode ben = (EnemyBlockNode)lst_block.First();
                GameStateNode  gsn = (GameStateNode)lst_game.First();
                foreach (Node node in lst)
                {
                    GunControlNode gcn = (GunControlNode)node;
                    if (keyPool.Contains(gcn.gunControl.space) && gcn.gun.shoot)
                    {
                        gcn.gun.shootingPoint = gcn.pos.point + new Vector2D(gcn.display.bitmap.Width / 2, 0);
                        sp.Play();
                        gcn.gun.shoot = false;
                        keyPool.Remove(gcn.gunControl.space);

                        if (gcn.gun.doubleShoot && timer_bonus < 5)
                        {
                            timer_bonus++;
                        }
                        else
                        {
                            timer_bonus         = 0;
                            gcn.gun.doubleShoot = false;
                        }
                        ef.CreateSpaceShipBullet(gcn.gun);
                    }
                }



                // we update enemies gun shooting point
                foreach (Node enemy in lst_enemies)
                {
                    EnemyNode en = (EnemyNode)enemy;
                    en.gun.shootingPoint = en.pos.point + new Vector2D(en.display.bitmap.Width / 2, 0);
                }

                // we see if we shoot anything, if so we choose a random enemy to do so
                if (Game.rand.Next(ben.block.shootProbability) <= gsn.gs.level * 2)
                {
                    int index = Game.rand.Next(lst_enemies.Count);
                    if (lst_enemies.Count > 0)
                    {
                        EnemyNode en     = (EnemyNode)lst_enemies.ToList()[index];
                        int       damage = 0;
                        Bitmap    b      = null;
                        switch (en.enemy.type)
                        {
                        case Enemy.Type.Big:
                            b      = Properties.Resources.shoot4;
                            damage = 3 + gsn.gs.level;
                            break;

                        case Enemy.Type.Medium:
                            damage = 2 + gsn.gs.level;
                            b      = Properties.Resources.shoot3;
                            break;

                        case Enemy.Type.Small:
                            b      = Properties.Resources.shoot2;
                            damage = 1 + gsn.gs.level;
                            break;
                        }
                        ef.CreateEnemyBullet(en.gun, b, damage);
                    }
                }
            }
        }