コード例 #1
0
        public void Update()
        {
            for (int i = 0; i < bullets.Count(); ++i)
            {
                Bullet bullet    = bullets.ElementAt(i);
                bool   throw_out = false;
                if (bullet.col_tok.HasCollisions()) // if a bullet runs into a tree, for instance- stop displaying it
                {
                    List <ColToken> cols = bullet.col_tok.GetCollisions();
                    for (int j = 0; j < cols.Count(); ++j)
                    {
                        if ((bullet.owner == bulletOwner.PLAYER && cols.ElementAt(j).GetLocalType() == ColType.PLAYER))
                        {
                            continue;
                        }
                        else
                        {
                            if (bullet.owner == bulletOwner.ENEMY)
                            {
                                continue;
                            }
                            ColToken hit = cols.ElementAt(j);
                            if (hit.GetLocalType() == ColType.MONSTER) // if bullet runs into a monster
                            {
                                Enemy monster = (Enemy)hit.GetParent();
                                int   damage  = 0;
                                switch (bullet.type)   // a bullet can technically be a sword in our design, so determine what kind of weapon is being used
                                {
                                case bulletType.SMALL: // bullet does damage to the enemy
                                    damage = 5;
                                    break;

                                case bulletType.SWORD:     //sword does more damage then a bullet
                                    damage = 10;
                                    break;
                                }
                                monster.setHealth(monster.getHealth() - (game_state.local_player.getAttackBonus() + damage)); // substract damage from monster's health
                                game_state.fx_engine.RequestSound(soundType.ENEMY_HURT);                                      // play a sound when hitting enemies
                            }
                            else if (hit.GetLocalType() == ColType.PLAYER)                                                    // if enemy bullet hits our character
                            {
                                int damage = 0;
                                switch (bullet.type)
                                {
                                case bulletType.SMALL:     // take damage
                                    damage = 5;
                                    break;

                                case bulletType.SWORD:     //take more damage then a bullet
                                    damage = 10;
                                    break;
                                }
                                game_state.local_player.setHealth(game_state.local_player.getHealth() + game_state.local_player.getDefenseBonus() - damage); // reset health for our character
                            }
                            game_state.coll_engine.remove_object(bullet.col_tok);                                                                            // remove bullet from screen once it hits enemy/character
                            bullet.col_tok.ResetCollisions();
                            bullets.RemoveAt(i);
                            throw_out = true; // no need for this bullet object anymore
                        }
                    }
                    bullet.col_tok.ResetCollisions();
                }
                if (throw_out == false) // if the bullet hasnt hit anything yet, continue drawing it
                {
                    bullet.x += bullet.vel_x;
                    bullet.y += bullet.vel_y;
                    bullet.col_tok.update(bullet.x, bullet.y); // use the bullets velocity and update its position

                    //We need to dispose of bullets if they leave the area
                    if (bullet.x > game_state.tile_engine.getCurrentMap().getWidth() * game_state.tile_engine.getTileSize() || bullet.x < 0)
                    {
                        bullets.RemoveAt(i);
                        game_state.coll_engine.remove_object(bullet.col_tok);
                    }

                    if (bullet.y > game_state.tile_engine.getCurrentMap().getHeight() * game_state.tile_engine.getTileSize() || bullet.y < 0)
                    {
                        bullets.RemoveAt(i);
                        game_state.coll_engine.remove_object(bullet.col_tok);
                    }
                }
            }
        }
コード例 #2
0
        public void Update()
        {
            for (int i = 0; i < bullets.Count(); ++i)
            {
                Bullet bullet    = bullets.ElementAt(i);
                bool   throw_out = false;
                if (bullet.col_tok.HasCollisions())
                {
                    List <ColToken> cols = bullet.col_tok.GetCollisions();
                    for (int j = 0; j < cols.Count(); ++j)
                    {
                        if ((bullet.owner == bulletOwner.PLAYER && cols.ElementAt(j).GetLocalType() == ColType.PLAYER))
                        {
                            continue;
                        }
                        else
                        {
                            ColToken hit = cols.ElementAt(j);
                            if (hit.GetLocalType() == ColType.MONSTER)
                            {
                                Enemy monster = (Enemy)hit.GetParent();
                                int   damage  = 0;
                                switch (bullet.type)
                                {
                                case bulletType.SMALL:
                                    damage = 5;
                                    break;

                                case bulletType.SWORD:
                                    damage = 10;
                                    break;
                                }
                                monster.setHealth(monster.getHealth() - (game_state.local_player.getAttackBonus() + damage));
                                game_state.fx_engine.RequestSound(soundType.HURT);
                            }
                            else if (hit.GetLocalType() == ColType.PLAYER)
                            {
                                int damage = 0;
                                switch (bullet.type)
                                {
                                case bulletType.SMALL:
                                    damage = 5;
                                    break;

                                case bulletType.SWORD:
                                    damage = 10;
                                    break;
                                }
                                game_state.local_player.setHealth(game_state.local_player.getHealth() + game_state.local_player.getDefenseBonus() - damage);
                            }
                            game_state.coll_engine.remove_object(bullet.col_tok);
                            bullet.col_tok.ResetCollisions();
                            bullets.RemoveAt(i);
                            throw_out = true;
                        }
                    }
                    bullet.col_tok.ResetCollisions();
                }
                if (throw_out == false)
                {
                    bullet.x += bullet.vel_x;
                    bullet.y += bullet.vel_y;
                    bullet.col_tok.update(bullet.x, bullet.y);

                    //We need to dispose of bullets if they leave the area.
                    if (bullet.x > game_state.tile_engine.getCurrentMap().getWidth() * game_state.tile_engine.getTileSize() || bullet.x < 0)
                    {
                        bullets.RemoveAt(i);
                        game_state.coll_engine.remove_object(bullet.col_tok);
                    }

                    if (bullet.y > game_state.tile_engine.getCurrentMap().getHeight() * game_state.tile_engine.getTileSize() || bullet.y < 0)
                    {
                        bullets.RemoveAt(i);
                        game_state.coll_engine.remove_object(bullet.col_tok);
                    }
                }
            }
        }