public void Update(int elapsed_time) // detect if explosion needs to be drawn, determine what the monster needs to do, handle dead monsters
        {
            for (int i = 0; i < monsters.Count(); ++i)
            {
                Enemy monster      = monsters.ElementAt(i);
                bool  mons_removed = false;

                if (monster.getHealth() <= 0)
                {
                    game_state.monster_engine.Remove(monster);
                    mons_removed = true;
                    game_state.fx_engine.RequestExplosion(explosionType.SMALL, monster.getX() + (monster.getWidth() / 2), monster.getY() + (monster.getHeight() / 2));
                    game_state.fx_engine.RequestSound(soundType.ENEMY_DIE);
                }
                if (mons_removed == false)
                {
                    if (IsVisible(monster))
                    {
                        move_towards_target(monster);

                        if (monster.next_think_time >= 2000) //TIME DELAY
                        {
                            actionDecision action = think(monster);
                            act(monster, action);
                            monster.next_think_time = 0;
                        }
                        else
                        {
                            monster.next_think_time += elapsed_time;
                        }
                    }
                }
            }
        }
        //Two basic functions for the Monster engine
        actionDecision think(Enemy monster)
        {
            float[] ratings = new float[(int)actionDecision.NUM_ACTIONS];
            int     dist_x  = game_state.local_player.getX() - monster.getX();
            int     dist_y  = game_state.local_player.getY() - monster.getY();

            int Dp  = GetRating(Math.Sqrt(Math.Pow(dist_x, 2) + Math.Pow(dist_y, 2)), 800.0f);
            int Db  = 0; //Bullet system not in yet
            int Alg = 0;

            if (Math.Abs(dist_x) < Math.Abs(dist_y))
            {
                Alg = GetRating(Math.Abs(dist_x), 400);
            }
            else
            {
                Alg = GetRating(Math.Abs(dist_y), 240);
            }

            int Hlt = GetRating(monster.getHealth(), monster.getMaxHealth());

            actionDecision retval    = actionDecision.FLEE;
            float          max_value = 0;

            for (int i = 0; i < (int)actionDecision.NUM_ACTIONS; ++i)
            {
                ratings[i] =
                    decision_matrix[(int)monster.getType(), i, (int)actionFactor.DP] * Dp +
                    decision_matrix[(int)monster.getType(), i, (int)actionFactor.DB] * Db +
                    decision_matrix[(int)monster.getType(), i, (int)actionFactor.AL] * Alg +
                    decision_matrix[(int)monster.getType(), i, (int)actionFactor.HL] * Hlt;

                if (ratings[i] > max_value)
                {
                    retval    = (actionDecision)i;
                    max_value = ratings[i];
                }
            }

            if (max_value < 5)
            {
                return(actionDecision.IDLE);
            }
            return(retval);
        }
        public void Update(int elapsed_time)
        {
            for (int i = 0; i < monsters.Count(); ++i)
            {
                Enemy monster      = monsters.ElementAt(i);
                bool  mons_removed = false;

                /*
                 * if (monster.col_tok.HasCollisions())//&& monster.col_tok.GetHitType() == ColType.BULLET)
                 * {
                 *
                 *  List<ColToken> cols = monster.col_tok.GetCollisions();
                 *  for (int j = 0; j < cols.Count(); ++j)
                 *  {
                 *      if (cols.ElementAt(j).GetLocalType() == ColType.BULLET )
                 *      {
                 *          //MAGIC NUMBER
                 *          Bullet bull = (Bullet)cols.ElementAt(j).GetParent();
                 *          int damage = 0;
                 *          switch (bull.type)
                 *          {
                 *              case bulletType.SMALL:
                 *                  damage = 5;
                 *                  break;
                 *              case bulletType.SWORD:
                 *                  damage = 10;
                 *                  break;
                 *
                 *          }
                 *          monster.setHealth(monster.getHealth() - (game_state.local_player.getAttackBonus()+damage));
                 *          //dmg_sound.Play();
                 *          if (monster.getHealth() <= 0)
                 *          {
                 *              game_state.monster_engine.Remove(monster);
                 *              mons_removed = true;
                 *              game_state.fx_engine.RequestExplosion(explosionType.SMALL, monster.getX()+(monster.getWidth()/2), monster.getY()+(monster.getHeight()/2));
                 *          }
                 *          game_state.fx_engine.RequestSound(soundType.HURT);
                 *      }
                 *      else if (cols.ElementAt(j).GetLocalType() == ColType.MAP)
                 *      {
                 *          //monster.revertX();
                 *          //monster.revertY();
                 *      }
                 *  }
                 *
                 *  monster.col_tok.ResetCollisions();
                 * }
                 * */
                if (monster.getHealth() <= 0)
                {
                    game_state.monster_engine.Remove(monster);
                    mons_removed = true;
                    game_state.fx_engine.RequestExplosion(explosionType.SMALL, monster.getX() + (monster.getWidth() / 2), monster.getY() + (monster.getHeight() / 2));
                }
                if (mons_removed == false)
                {
                    if (IsVisible(monster))
                    {
                        move_towards_target(monster);

                        if (monster.next_think_time >= 2000) //TIME DELAY
                        {
                            actionDecision action = think(monster);
                            act(monster, action);
                            monster.next_think_time = 0;
                        }
                        else
                        {
                            monster.next_think_time += elapsed_time;
                        }
                    }
                }
            }
        }
Exemple #4
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);
                    }
                }
            }
        }
        //Two basic functions for the Monster engine
        actionDecision think(Enemy monster)
        {
            float[] ratings = new float[(int)actionDecision.NUM_ACTIONS];
            int dist_x = game_state.local_player.getX() - monster.getX();
            int dist_y = game_state.local_player.getY() - monster.getY();

            int Dp = GetRating(Math.Sqrt(Math.Pow(dist_x, 2) + Math.Pow(dist_y, 2)), 800.0f);
            int Db = 0; //Bullet system not in yet
            int Alg = 0;
            if (Math.Abs(dist_x) < Math.Abs(dist_y))
            {
                Alg = GetRating(Math.Abs(dist_x), 400);
            }
            else
            {
                Alg = GetRating(Math.Abs(dist_y), 240);
            }

            int Hlt = GetRating(monster.getHealth(), monster.getMaxHealth());

            actionDecision retval = actionDecision.FLEE;
            float max_value = 0;

            for (int i = 0; i < (int)actionDecision.NUM_ACTIONS; ++i)
            {
                ratings[i] =
                    decision_matrix[(int)monster.getType(), i, (int)actionFactor.DP]*Dp +
                    decision_matrix[(int)monster.getType(), i, (int)actionFactor.DB]*Db +
                    decision_matrix[(int)monster.getType(), i, (int)actionFactor.AL]*Alg +
                    decision_matrix[(int)monster.getType(), i, (int)actionFactor.HL]*Hlt;

                if (ratings[i] > max_value)
                {
                    retval = (actionDecision)i;
                    max_value = ratings[i];
                }
            }

            if (max_value < 5)
            {
                return actionDecision.IDLE;
            }
            return retval;
        }
        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);
                    }
                }
            }
        }