void fire(Enemy monster) { int player_x = game_state.local_player.getX(); int player_y = game_state.local_player.getY(); int monster_x = monster.getX(); int monster_y = monster.getY(); int fire_x = 0; int fire_y = 0; int min = 0; int max = 0; PlayerDir dir = PlayerDir.UP; Random random = new Random(); bool fire = false; if (player_x < monster_x + monster.getWidth() && player_x > monster_x - monster.getWidth()) { fire = true; min = monster.getX(); max = monster.getX() + monster.getWidth(); fire_x = random.Next(min, max); if (monster_y - player_y >= 0) { dir = PlayerDir.UP; fire_y = monster.getY() + 5; } else { dir = PlayerDir.DOWN; fire_y = monster.getY() + monster.getHeight(); } } else if (player_y < monster_y + monster.getHeight() && player_y > monster_y - monster.getHeight()) { fire = true; min = monster.getY(); max = monster.getY() + monster.getHeight(); fire_y = random.Next(min, max); if (monster_x - player_x >= 0) { dir = PlayerDir.LEFT; fire_x = monster.getX() - 5; } else { dir = PlayerDir.RIGHT; fire_x = monster.getX() + monster.getWidth(); } } if (fire) { game_state.bullet_engine.fire(fire_x, fire_y, dir, bulletOwner.ENEMY, bulletType.SMALL); fire = false; } else { this.advance(monster); } //Place holder till bullet system works }
void move_towards_target(Enemy monster) { if (monster.getTarget() == null) { return; } int dist_x = monster.getTarget().loc_x - (monster.getX() + (monster.getWidth() / 2)) / game_state.tile_engine.getTileSize(); int dist_y = monster.getTarget().loc_y - (monster.getY() + (monster.getHeight())) / game_state.tile_engine.getTileSize(); if (dist_x == 0 && dist_y == 0) { monster.nextTarget(); return; } if (Math.Abs(dist_x) > Math.Abs(dist_y)) { //Advance in the X direction if (dist_x > 0) { monster.setX(monster.getX() + monster.getSpeed()); monster.setDirection(PlayerDir.RIGHT); } else { monster.setX(monster.getX() - monster.getSpeed()); monster.setDirection(PlayerDir.LEFT); } } else { //Advance in the Y direction if (dist_y > 0) { monster.setY(monster.getY() + monster.getSpeed()); monster.setDirection(PlayerDir.DOWN); } else { monster.setY(monster.getY() - monster.getSpeed()); monster.setDirection(PlayerDir.UP); } } if (monster.getLastDirection() != monster.getDirection()) { monster.getSprite().StartAnimating((int)monster.getDirection() * 3, ((int)monster.getDirection() * 3) + 2); } }
void advance(Enemy monster) // go toward player { PathFind pf = new PathFind(game_state); int mons_tile_x = (monster.getX() + (monster.getWidth() / 2)) / game_state.tile_engine.getTileSize(); int mons_tile_y = (monster.getY() + (monster.getHeight())) / game_state.tile_engine.getTileSize(); int pl_tile_x = (game_state.local_player.getX() + (game_state.local_player.getWidth() / 2)) / game_state.tile_engine.getTileSize(); int pl_tile_y = (game_state.local_player.getY() + (game_state.local_player.getHeight())) / game_state.tile_engine.getTileSize(); monster.setPath(pf.FindPath(mons_tile_x, mons_tile_y, pl_tile_x, pl_tile_y)); }
void advance(Enemy monster) { PathFind pf = new PathFind(game_state); int mons_tile_x = (monster.getX() + (monster.getWidth() / 2)) / game_state.tile_engine.getTileSize(); int mons_tile_y = (monster.getY() + (monster.getHeight())) / game_state.tile_engine.getTileSize(); int pl_tile_x = (game_state.local_player.getX() + (game_state.local_player.getWidth() / 2)) / game_state.tile_engine.getTileSize(); int pl_tile_y = (game_state.local_player.getY() + (game_state.local_player.getHeight())) / game_state.tile_engine.getTileSize(); // pl_tile_y += 1;//get the enemies to head toward the bottom of the player so that they follow you on the bridge monster.setPath(pf.FindPath(mons_tile_x, mons_tile_y, pl_tile_x, pl_tile_y)); }
void align(Enemy monster) { PathFind pf = new PathFind(game_state); int dist_x = game_state.local_player.getX() - monster.getX(); int dist_y = game_state.local_player.getY() - monster.getY(); int mons_tile_x = (monster.getX() + (monster.getWidth() / 2)) / game_state.tile_engine.getTileSize(); int mons_tile_y = (monster.getY() + (monster.getHeight() / 2)) / game_state.tile_engine.getTileSize(); int pl_tile_x = game_state.local_player.getX() / game_state.tile_engine.getTileSize(); int pl_tile_y = game_state.local_player.getY() / game_state.tile_engine.getTileSize(); if (Math.Abs(dist_x) < Math.Abs(dist_y)) { //Advance in the X direction toward the player monster.setPath(pf.FindPath(mons_tile_x, mons_tile_y, pl_tile_x, mons_tile_y)); } else { //Advance in the Y direction toward the player monster.setPath(pf.FindPath(mons_tile_x, mons_tile_y, mons_tile_x, pl_tile_y - 1)); } }
void idle(Enemy monster) { //THIS IS FOR DEBUG PURPOSES //PathFind pf = new PathFind(game_state); //int mons_tile_x = (monster.getX() + (monster.getWidth() / 2)) / game_state.tile_engine.getTileSize(); //int mons_tiel_y = (monster.getY() + (monster.getHeight())) / game_state.tile_engine.getTileSize(); //monster.setPath(pf.FindPath(mons_tile_x, mons_tiel_y, 1, 1)); PathFind pf = new PathFind(game_state); int mons_tile_x = (monster.getX() + (monster.getWidth() / 2)) / game_state.tile_engine.getTileSize(); int mons_tile_y = (monster.getY() + (monster.getHeight() / 2)) / game_state.tile_engine.getTileSize(); Random r = new Random(); int mons_tile_xr = mons_tile_x + r.Next(-10, 10); int mons_tile_yr = mons_tile_y + r.Next(-10, 10); if (mons_tile_yr < 0) { mons_tile_yr = 0; } if (mons_tile_xr < 0) { mons_tile_xr = 0; } monster.setPath(pf.FindPath(mons_tile_x, mons_tile_y, mons_tile_xr, mons_tile_yr)); /* * //Random number * System.Random generator = new System.Random(); * int dir = generator.Next(4); * switch ((PlayerDir)dir) * { * case PlayerDir.DOWN: * monster.setY(monster.getY() + monster.getSpeed()); * monster.setDirection(PlayerDir.DOWN); * break; * case PlayerDir.LEFT: * monster.setX(monster.getX() - monster.getSpeed()); * monster.setDirection(PlayerDir.LEFT); * break; * case PlayerDir.RIGHT: * monster.setX(monster.getX() + monster.getSpeed()); * monster.setDirection(PlayerDir.RIGHT); * break; * case PlayerDir.UP: * monster.setY(monster.getY() - monster.getSpeed()); * monster.setDirection(PlayerDir.UP); * break; * } * */ }
public bool IsVisible(Enemy monster) { //MAGIC NUMBERS. Screen is 800 long by 480 //We can determine if the monster is visible based on the players position int screen_x = Math.Abs(game_state.local_player.getX() - monster.getX()); int screen_y = Math.Abs(game_state.local_player.getY() - monster.getY()); if (screen_x > 800 || screen_y > 400) { return(false); } return(true); }
public bool IsVisible(Enemy monster) { //MAGIC NUMBERS. Screen is 800 long by 480 //We can determine if the monster is visible based on the players position int screen_x = Math.Abs(game_state.local_player.getX() - monster.getX()); int screen_y = Math.Abs(game_state.local_player.getY() - monster.getY()); if (screen_x > 800 || screen_y > 400) { return false; } return true; }
//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); }
void idle(Enemy monster) // dont do anything { PathFind pf = new PathFind(game_state); int mons_tile_x = (monster.getX() + (monster.getWidth() / 2)) / game_state.tile_engine.getTileSize(); int mons_tile_y = (monster.getY() + (monster.getHeight() / 2)) / game_state.tile_engine.getTileSize(); Random r = new Random(); int mons_tile_xr = mons_tile_x + r.Next(-10, 10); int mons_tile_yr = mons_tile_y + r.Next(-10, 10); if (mons_tile_yr < 0) { mons_tile_yr = 0; } if (mons_tile_xr < 0) { mons_tile_xr = 0; } monster.setPath(pf.FindPath(mons_tile_x, mons_tile_y, mons_tile_xr, mons_tile_yr)); }
public void Update() { for (int i = 0; i < marked_objects.Count(); ++i) // for every object that has been "marked", or changed/modified via gameplay, check to see if there is collisions { ColToken obj = marked_objects.ElementAt(i); int loc_x = 0, loc_y = 0, width = 0, height = 0; switch (obj.GetLocalType()) { case ColType.PLAYER: Player temp_pl = (Player)obj.GetParent(); loc_x = temp_pl.getX(); loc_y = temp_pl.getY(); width = temp_pl.getWidth(); height = temp_pl.getHeight(); break; case ColType.MONSTER: Enemy temp_em = (Enemy)obj.GetParent(); loc_x = temp_em.getX(); loc_y = temp_em.getY(); width = temp_em.getWidth(); height = temp_em.getHeight(); break; case ColType.BULLET: Bullet temp_bu = (Bullet)obj.GetParent(); loc_x = temp_bu.x; loc_y = temp_bu.y; width = temp_bu.width; height = temp_bu.height; break; } if (check_map_col(loc_x, loc_y, width, height)) { obj.Collision(new ColToken(ColType.MAP, this, unique_id++, null)); } for (int j = 0; j < all_objects.Count(); ++j) // check to see if any object is involved in a collision { ColToken other_obj = all_objects.ElementAt(j); if (obj.getID() == other_obj.getID()) { continue; } int other_loc_x = 0, other_loc_y = 0, other_width = 0, other_height = 0; switch (other_obj.GetLocalType()) { case ColType.PLAYER: Player temp_pl = (Player)other_obj.GetParent(); other_loc_x = temp_pl.getX(); other_loc_y = temp_pl.getY(); other_width = temp_pl.getWidth(); other_height = temp_pl.getHeight(); break; case ColType.MONSTER: Enemy temp_em = (Enemy)other_obj.GetParent(); other_loc_x = temp_em.getX(); other_loc_y = temp_em.getY(); other_width = temp_em.getWidth(); other_height = temp_em.getHeight(); break; case ColType.BULLET: Bullet temp_bu = (Bullet)other_obj.GetParent(); other_loc_x = temp_bu.x; other_loc_y = temp_bu.y; other_width = temp_bu.width; other_height = temp_bu.height; break; } if (check_col(loc_x, loc_y, other_loc_x, other_loc_y, width, height, other_width, other_height)) { obj.Collision(other_obj); } } obj.updated_this_frame = false; } marked_objects.Clear(); // handled all of the modified objects }
void advance(Enemy monster) { PathFind pf = new PathFind(game_state); int mons_tile_x = (monster.getX() + (monster.getWidth() / 2)) / game_state.tile_engine.getTileSize(); int mons_tile_y = (monster.getY() + (monster.getHeight())) / game_state.tile_engine.getTileSize(); int pl_tile_x = (game_state.local_player.getX()+(game_state.local_player.getWidth()/2))/game_state.tile_engine.getTileSize(); int pl_tile_y = (game_state.local_player.getY()+(game_state.local_player.getHeight()))/game_state.tile_engine.getTileSize(); // pl_tile_y += 1;//get the enemies to head toward the bottom of the player so that they follow you on the bridge monster.setPath(pf.FindPath(mons_tile_x, mons_tile_y, pl_tile_x, pl_tile_y)); }
void align(Enemy monster) { PathFind pf = new PathFind(game_state); int dist_x = game_state.local_player.getX() - monster.getX(); int dist_y = game_state.local_player.getY() - monster.getY(); int mons_tile_x = (monster.getX() + (monster.getWidth() / 2)) / game_state.tile_engine.getTileSize(); int mons_tile_y = (monster.getY() + (monster.getHeight() / 2)) / game_state.tile_engine.getTileSize(); int pl_tile_x = game_state.local_player.getX() / game_state.tile_engine.getTileSize(); int pl_tile_y = game_state.local_player.getY() / game_state.tile_engine.getTileSize(); if (Math.Abs(dist_x) < Math.Abs(dist_y)) { //Advance in the X direction toward the player monster.setPath(pf.FindPath(mons_tile_x, mons_tile_y, pl_tile_x, mons_tile_y)); } else { //Advance in the Y direction toward the player monster.setPath(pf.FindPath(mons_tile_x, mons_tile_y, mons_tile_x, pl_tile_y-1)); } }
//Functions to calculate actions void flee(Enemy monster) { PathFind pf = new PathFind(game_state); int dist_x = game_state.local_player.getX() - monster.getX(); int dist_y = game_state.local_player.getY() - monster.getY(); int mons_tile_x = (monster.getX() + (monster.getWidth() / 2)) / game_state.tile_engine.getTileSize(); int mons_tile_y = (monster.getY() + (monster.getHeight() / 2)) / game_state.tile_engine.getTileSize(); int pl_tile_x = game_state.local_player.getX() / game_state.tile_engine.getTileSize(); int pl_tile_y = game_state.local_player.getY() / game_state.tile_engine.getTileSize(); Random r = new Random(); int mons_tile_xr = mons_tile_x; int mons_tile_yr = mons_tile_y; //int pl_tile_yr = pl_tile_y + r.Next(-5, 5); //int pl_tile_xr = pl_tile_x + r.Next(-5, 5); if (Math.Abs(dist_x) < Math.Abs(dist_y)) { //Flee in the Y direction from the player if (dist_y <= 0) { mons_tile_yr += r.Next(-5, -10); mons_tile_xr += r.Next(-10, 10); if (mons_tile_yr < 0) { mons_tile_yr = 0; } if (mons_tile_xr < 0) { mons_tile_xr = 0; } monster.setPath(pf.FindPath(mons_tile_x, mons_tile_y, mons_tile_xr, mons_tile_yr)); } else { mons_tile_yr += r.Next(5, 10); mons_tile_xr += r.Next(-10, 10); if (mons_tile_yr < 0) { mons_tile_yr = 0; } if (mons_tile_xr < 0) { mons_tile_xr = 0; } monster.setPath(pf.FindPath(mons_tile_x, mons_tile_y, mons_tile_xr, mons_tile_yr)); } } else { //Flee in the x direction from the player if (dist_x <= 0) { mons_tile_yr += r.Next(-10, 10); mons_tile_xr += r.Next(5, 10); if (mons_tile_yr < 0) { mons_tile_yr = 0; } if (mons_tile_xr < 0) { mons_tile_xr = 0; } monster.setPath(pf.FindPath(mons_tile_x, mons_tile_y, mons_tile_xr, mons_tile_yr)); } else { mons_tile_yr += r.Next(-10, 10); mons_tile_xr += r.Next(5, 10); if (mons_tile_yr < 0) { mons_tile_yr = 0; } if (mons_tile_xr < 0) { mons_tile_xr = 0; } monster.setPath(pf.FindPath(mons_tile_x, mons_tile_y, mons_tile_xr, mons_tile_yr)); } monster.setPath(pf.FindPath(mons_tile_x, mons_tile_y, mons_tile_xr, mons_tile_yr)); } //monster.setPath(pf.FindPath(mons_tile_x, mons_tile_y, mons_tile_xr, mons_tile_yr)); }
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; } } } } }
void fire(Enemy monster) { int player_x = game_state.local_player.getX(); int player_y = game_state.local_player.getY(); int monster_x = monster.getX(); int monster_y = monster.getY(); int fire_x = 0; int fire_y = 0; int min = 0; int max = 0; PlayerDir dir = PlayerDir.UP; Random random = new Random(); bool fire = false; if (player_x < monster_x + monster.getWidth() && player_x > monster_x - monster.getWidth()) { fire = true; min = monster.getX(); max = monster.getX()+monster.getWidth(); fire_x = random.Next(min,max); if (monster_y - player_y >= 0) { dir = PlayerDir.UP; fire_y = monster.getY() + 5; } else { dir = PlayerDir.DOWN; fire_y = monster.getY() + monster.getHeight(); } } else if (player_y < monster_y + monster.getHeight() && player_y > monster_y - monster.getHeight()) { fire = true; min = monster.getY(); max = monster.getY() + monster.getHeight(); fire_y = random.Next(min, max); if (monster_x - player_x >= 0) { dir = PlayerDir.LEFT; fire_x = monster.getX() - 5; } else { dir = PlayerDir.RIGHT; fire_x = monster.getX() + monster.getWidth(); } } if (fire) { game_state.bullet_engine.fire(fire_x, fire_y, dir, bulletOwner.ENEMY, bulletType.SMALL); fire = false; } else { this.advance(monster); } //Place holder till bullet system works }
// dont do anything void idle(Enemy monster) { PathFind pf = new PathFind(game_state); int mons_tile_x = (monster.getX() + (monster.getWidth() / 2)) / game_state.tile_engine.getTileSize(); int mons_tile_y = (monster.getY() + (monster.getHeight() / 2)) / game_state.tile_engine.getTileSize(); Random r = new Random(); int mons_tile_xr = mons_tile_x + r.Next(-10, 10); int mons_tile_yr = mons_tile_y + r.Next(-10, 10); if (mons_tile_yr < 0) mons_tile_yr = 0; if (mons_tile_xr < 0) mons_tile_xr = 0; monster.setPath(pf.FindPath(mons_tile_x, mons_tile_y, mons_tile_xr, mons_tile_yr)); }
//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; }
void move_towards_target(Enemy monster) { if (monster.getTarget() == null) { return; } int dist_x = monster.getTarget().loc_x - (monster.getX()+(monster.getWidth()/2))/game_state.tile_engine.getTileSize(); int dist_y = monster.getTarget().loc_y - (monster.getY()+(monster.getHeight())) / game_state.tile_engine.getTileSize(); if (dist_x == 0 && dist_y == 0) { monster.nextTarget(); return; } if (Math.Abs(dist_x) > Math.Abs(dist_y)) { //Advance in the X direction if (dist_x > 0) { monster.setX(monster.getX() + monster.getSpeed()); monster.setDirection(PlayerDir.RIGHT); } else { monster.setX(monster.getX() - monster.getSpeed()); monster.setDirection(PlayerDir.LEFT); } } else { //Advance in the Y direction if (dist_y > 0) { monster.setY(monster.getY() + monster.getSpeed()); monster.setDirection(PlayerDir.DOWN); } else { monster.setY(monster.getY() - monster.getSpeed()); monster.setDirection(PlayerDir.UP); } } if (monster.getLastDirection() != monster.getDirection()) { monster.getSprite().StartAnimating((int)monster.getDirection() * 3, ((int)monster.getDirection() * 3) + 2); } }
void idle(Enemy monster) { //THIS IS FOR DEBUG PURPOSES //PathFind pf = new PathFind(game_state); //int mons_tile_x = (monster.getX() + (monster.getWidth() / 2)) / game_state.tile_engine.getTileSize(); //int mons_tiel_y = (monster.getY() + (monster.getHeight())) / game_state.tile_engine.getTileSize(); //monster.setPath(pf.FindPath(mons_tile_x, mons_tiel_y, 1, 1)); PathFind pf = new PathFind(game_state); int mons_tile_x = (monster.getX() + (monster.getWidth() / 2)) / game_state.tile_engine.getTileSize(); int mons_tile_y = (monster.getY() + (monster.getHeight() / 2)) / game_state.tile_engine.getTileSize(); Random r = new Random(); int mons_tile_xr = mons_tile_x + r.Next(-10, 10); int mons_tile_yr = mons_tile_y + r.Next(-10, 10); if (mons_tile_yr < 0) mons_tile_yr = 0; if (mons_tile_xr < 0) mons_tile_xr = 0; monster.setPath(pf.FindPath(mons_tile_x, mons_tile_y, mons_tile_xr, mons_tile_yr)); /* //Random number System.Random generator = new System.Random(); int dir = generator.Next(4); switch ((PlayerDir)dir) { case PlayerDir.DOWN: monster.setY(monster.getY() + monster.getSpeed()); monster.setDirection(PlayerDir.DOWN); break; case PlayerDir.LEFT: monster.setX(monster.getX() - monster.getSpeed()); monster.setDirection(PlayerDir.LEFT); break; case PlayerDir.RIGHT: monster.setX(monster.getX() + monster.getSpeed()); monster.setDirection(PlayerDir.RIGHT); break; case PlayerDir.UP: monster.setY(monster.getY() - monster.getSpeed()); monster.setDirection(PlayerDir.UP); break; } * */ }
//Functions to calculate actions void flee(Enemy monster) { PathFind pf = new PathFind(game_state); int dist_x = game_state.local_player.getX() - monster.getX(); int dist_y = game_state.local_player.getY() - monster.getY(); int mons_tile_x = (monster.getX() + (monster.getWidth() / 2)) / game_state.tile_engine.getTileSize(); int mons_tile_y = (monster.getY() + (monster.getHeight() / 2)) / game_state.tile_engine.getTileSize(); int pl_tile_x = game_state.local_player.getX() / game_state.tile_engine.getTileSize(); int pl_tile_y = game_state.local_player.getY() / game_state.tile_engine.getTileSize(); Random r = new Random(); int mons_tile_xr = mons_tile_x; int mons_tile_yr = mons_tile_y; //int pl_tile_yr = pl_tile_y + r.Next(-5, 5); //int pl_tile_xr = pl_tile_x + r.Next(-5, 5); if (Math.Abs(dist_x) < Math.Abs(dist_y)) { //Flee in the Y direction from the player if (dist_y <= 0) { mons_tile_yr += r.Next(-10, -5); mons_tile_xr += r.Next(-10, 10); if (mons_tile_yr < 0) mons_tile_yr = 0; if (mons_tile_xr < 0) mons_tile_xr = 0; monster.setPath(pf.FindPath(mons_tile_x, mons_tile_y, mons_tile_xr, mons_tile_yr)); } else { mons_tile_yr += r.Next(5, 10); mons_tile_xr += r.Next(-10, 10); if (mons_tile_yr < 0) mons_tile_yr = 0; if (mons_tile_xr < 0) mons_tile_xr = 0; monster.setPath(pf.FindPath(mons_tile_x, mons_tile_y, mons_tile_xr, mons_tile_yr)); } } else { //Flee in the x direction from the player if (dist_x <= 0) { mons_tile_yr += r.Next(-10, 10); mons_tile_xr += r.Next(5, 10); if (mons_tile_yr < 0) mons_tile_yr = 0; if (mons_tile_xr < 0) mons_tile_xr = 0; monster.setPath(pf.FindPath(mons_tile_x, mons_tile_y, mons_tile_xr, mons_tile_yr)); } else { mons_tile_yr += r.Next(-10, 10); mons_tile_xr += r.Next(5, 10); if (mons_tile_yr < 0) mons_tile_yr = 0; if (mons_tile_xr < 0) mons_tile_xr = 0; monster.setPath(pf.FindPath(mons_tile_x, mons_tile_y, mons_tile_xr, mons_tile_yr)); } monster.setPath(pf.FindPath(mons_tile_x, mons_tile_y, mons_tile_xr, mons_tile_yr)); } //monster.setPath(pf.FindPath(mons_tile_x, mons_tile_y, mons_tile_xr, mons_tile_yr)); }
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; } } } } }
public void Update() { for (int i = 0; i < marked_objects.Count(); ++i) { ColToken obj = marked_objects.ElementAt(i); int loc_x = 0, loc_y = 0, width = 0, height = 0; switch (obj.GetLocalType()) { case ColType.PLAYER: Player temp_pl = (Player)obj.GetParent(); loc_x = temp_pl.getX(); loc_y = temp_pl.getY(); width = temp_pl.getWidth(); height = temp_pl.getHeight(); break; case ColType.MONSTER: Enemy temp_em = (Enemy)obj.GetParent(); loc_x = temp_em.getX(); loc_y = temp_em.getY(); width = temp_em.getWidth(); height = temp_em.getHeight(); break; case ColType.BULLET: Bullet temp_bu = (Bullet)obj.GetParent(); loc_x = temp_bu.x; loc_y = temp_bu.y; width = temp_bu.width; height = temp_bu.height; break; } if (check_map_col(loc_x, loc_y, width, height)) { // obj.Collision(new Collision(ColType.MAP, 0)); // REVERTS ITSELF SOME TIMES SO IT SAYS IT'S MARKED obj.Collision(new ColToken(ColType.MAP, this, unique_id++, null)); } for (int j = 0; j < all_objects.Count(); ++j) { ColToken other_obj = all_objects.ElementAt(j); if (obj.getID() == other_obj.getID()) { continue; } int other_loc_x = 0, other_loc_y = 0, other_width = 0, other_height = 0; switch (other_obj.GetLocalType()) { case ColType.PLAYER: Player temp_pl = (Player)other_obj.GetParent(); other_loc_x = temp_pl.getX(); other_loc_y = temp_pl.getY(); other_width = temp_pl.getWidth(); other_height = temp_pl.getHeight(); break; case ColType.MONSTER: Enemy temp_em = (Enemy)other_obj.GetParent(); other_loc_x = temp_em.getX(); other_loc_y = temp_em.getY(); other_width = temp_em.getWidth(); other_height = temp_em.getHeight(); break; case ColType.BULLET: Bullet temp_bu = (Bullet)other_obj.GetParent(); other_loc_x = temp_bu.x; other_loc_y = temp_bu.y; other_width = temp_bu.width; other_height = temp_bu.height; break; } //Check col /* * if (obj.GetLocalType() == ColType.BULLET && other_obj.GetLocalType() == ColType.MONSTER) * { * //HELP * continue; * } * */ if (check_col(loc_x, loc_y, other_loc_x, other_loc_y, width, height, other_width, other_height)) { // obj.Collision(new Collision(other_obj.GetLocalType(), other_obj.getID())); // other_obj.Collision(new Collision(obj.GetLocalType(), obj.getID())); obj.Collision(other_obj); } } obj.updated_this_frame = false; } marked_objects.Clear(); }
/// <summary> /// This is called when the game should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> public void Draw(GameTime gameTime, SpriteBatch spriteBatch) // draw everything on the screen { game_state.tile_engine.getCurrentMap().drawBackground(spriteBatch, game_state.local_player.getX(), game_state.local_player.getY()); game_state.tile_engine.getCurrentMap().drawObjects(spriteBatch, game_state.local_player.getX(), game_state.local_player.getY()); if (game_state.local_player.getX() <= 400 - 16) { character_sprite[(int)game_state.local_player.getWeapon()].loc.X = game_state.local_player.getX(); } else { if (game_state.local_player.getX() >= game_state.tile_engine.getCurrentMap().getWidth() * game_state.tile_engine.getTileSize() - 400 - 16) { character_sprite[(int)game_state.local_player.getWeapon()].loc.X = game_state.local_player.getX() - (game_state.tile_engine.getCurrentMap().getWidth() * game_state.tile_engine.getTileSize() - 800); } else { character_sprite[(int)game_state.local_player.getWeapon()].loc.X = (800 / 2) - 16;// -(32 / 2); } } if (game_state.local_player.getY() <= 240 - 16) { character_sprite[(int)game_state.local_player.getWeapon()].loc.Y = game_state.local_player.getY(); } else { if (game_state.local_player.getY() >= game_state.tile_engine.getCurrentMap().getHeight() * game_state.tile_engine.getTileSize() - 240 - 16) { character_sprite[(int)game_state.local_player.getWeapon()].loc.Y = game_state.local_player.getY() - (game_state.tile_engine.getCurrentMap().getHeight() * game_state.tile_engine.getTileSize() - 480); } else { character_sprite[(int)game_state.local_player.getWeapon()].loc.Y = (480 / 2) - 16;// - (32 / 2); } } // Fade a color over the player if they are hurt or have a boost active bool shouldFadeHurt = game_state.local_player.hurt; bool shouldFadeAttack = game_state.local_player.getAttackBonus() > 0; bool shouldFadeDefense = game_state.local_player.getDefenseBonus() > 0; Color toFade; //choose arbitrary colors for boosts if (shouldFadeHurt) { toFade = Color.Red; } else if (shouldFadeAttack && shouldFadeDefense) { toFade = Color.LightGreen; } else if (shouldFadeAttack) { toFade = Color.LightYellow; } else if (shouldFadeDefense) { toFade = Color.LightBlue; } else { toFade = Color.White; } if (sword_swing) // if the sword has been swung { sword_sprite.loc = character_sprite[(int)game_state.local_player.getWeapon()].loc; sword_sprite.Draw(spriteBatch, (int)game_state.local_player.getDirection(), (shouldFadeHurt || shouldFadeAttack || shouldFadeDefense), toFade); } else { character_sprite[(int)game_state.local_player.getWeapon()].Draw(spriteBatch, (shouldFadeHurt || shouldFadeAttack || shouldFadeDefense), toFade); } //Draw monsters List <Enemy> monsters = game_state.monster_engine.GetMonsters(); int offset_x = game_state.local_player.getX() - (int)character_sprite[(int)game_state.local_player.getWeapon()].loc.X; int offset_y = game_state.local_player.getY() - (int)character_sprite[(int)game_state.local_player.getWeapon()].loc.Y; for (int i = 0; i < monsters.Count(); ++i) { Enemy monster = monsters.ElementAt(i); if (game_state.monster_engine.IsVisible(monster)) { Sprite mons_sprite = monster.getSprite(); mons_sprite.loc.X = monster.getX() - offset_x; mons_sprite.loc.Y = monster.getY() - offset_y; if (mons_sprite.loc.X > 0 && mons_sprite.loc.X < 800) { if (mons_sprite.loc.Y > 0 && mons_sprite.loc.Y < 400) { mons_sprite.Draw(spriteBatch); //Draw if the location is not negative } } } } List <Bullet> bullets = game_state.bullet_engine.GetBullets(); //draw the bullets appropriately for (int i = 0; i < bullets.Count(); ++i) { Bullet bullet = bullets.ElementAt(i); if (bullet.type != bulletType.SWORD) { bullet_sprite.loc.X = bullet.x - offset_x; bullet_sprite.loc.Y = bullet.y - offset_y; bullet_sprite.Draw(spriteBatch); } } game_state.tile_engine.getCurrentMap().drawForeground(spriteBatch, game_state.local_player.getX(), game_state.local_player.getY()); game_state.fx_engine.Draw(spriteBatch, offset_x, offset_y); //Draw HUD //begin operations on display textures //gets spritebatch in a state to be 'ready' to draw //******************DRAWING ARROWS**************************// spriteBatch.Draw(uparrow, uparrowpos, null, trans, 0, imageOffset, 4.0f, SpriteEffects.None, 0); spriteBatch.Draw(downarrow, downarrowpos, null, trans, 0, imageOffset, 4.0f, SpriteEffects.None, 0); spriteBatch.Draw(leftarrow, leftarrowpos, null, trans, 0, imageOffset, 4.0f, SpriteEffects.None, 0); spriteBatch.Draw(rightarrow, rightarrowpos, null, trans, 0, imageOffset, 4.0f, SpriteEffects.None, 0); spriteBatch.Draw(fire_button, fire_button_pos, null, trans, 0, imageOffset, 3.0f, SpriteEffects.None, 0); spriteBatch.DrawString(displayFont, "FIRE!", new Vector2(fire_button_pos.X + 38, fire_button_pos.Y + 45), Color.Black); //draw the arrow graphics to the screen with given position, 3x as big as original size, no effects //************************************************************// //***********************DRAWING GRAPHIC SPRITES***********************// spriteBatch.Draw(backpack, backpackpos, null, trans, 0, imageOffset, new Vector2(0.4f, 1.0f), SpriteEffects.None, 0); //draw the backpack "button" spriteBatch.DrawString(displayFont, "Inventory", new Vector2(backpackpos.X + 20, backpackpos.Y + 12), Color.Black); spriteBatch.Draw(healthbar_empty, health_bar_empty_rec, null, trans); //draw health bar spriteBatch.Draw(healthbar_full, health_bar_rec, null, trans); //draw health bar string curHealth = "" + game_state.local_player.getHealth(); string maxHealth = "" + game_state.local_player.getMaxHealth(); spriteBatch.DrawString(displayFont, "Health: " + curHealth + "/" + maxHealth, new Vector2(health_bar_rec.X + 42, health_bar_rec.Y + 5), Color.Black); //********************************************************************// //***************************DRAWING STRINGS***********************************// spriteBatch.DrawString(displayFont, levelstring, scoreStringPos, Color.Cyan); // draw "level: " spriteBatch.DrawString(displayFont, game_state.tile_engine.getCurrentLevelName(), currScorePos, Color.Cyan); // draw level number spriteBatch.DrawString(displayFont, livesString + livesRemaining.ToString(), livesStringPos, Color.Cyan); // draw lives remaining //****************************************************************************// //check to see if the score needs to continue to be drawn (if time hasn't run out) //if it has, undraw it and display time expired string and menu if (!hasTimeLeft()) { die(); if (hasMoreLives()) { timex.Show(spriteBatch); // brings up time expired screen } } else { spriteBatch.DrawString(displayFont, timeLeft, timeLeftPos, Color.Cyan); spriteBatch.DrawString(displayFont, ((int)(currTime.TotalSeconds)).ToString(), timePos, Color.Cyan); } if (backpackmenu.backpack_touched) // backpack button has been pressed { if (hasTimeLeft()) // if time hasnt run out during the period where the inventory is brought up by the user, show the inventory { backpackmenu.Show(spriteBatch); // draw backpack menu drawItems(spriteBatch, itemfont); // draw items according to layout in itemfont } else // time has run out while the inventory is up, hide inventory and bring up time expired menu { backpackmenu.Hide(); timex.Show(spriteBatch); } } } // end draw function
// go toward player void advance(Enemy monster) { PathFind pf = new PathFind(game_state); int mons_tile_x = (monster.getX() + (monster.getWidth() / 2)) / game_state.tile_engine.getTileSize(); int mons_tile_y = (monster.getY() + (monster.getHeight())) / game_state.tile_engine.getTileSize(); int pl_tile_x = (game_state.local_player.getX()+(game_state.local_player.getWidth()/2))/game_state.tile_engine.getTileSize(); int pl_tile_y = (game_state.local_player.getY()+(game_state.local_player.getHeight()))/game_state.tile_engine.getTileSize(); monster.setPath(pf.FindPath(mons_tile_x, mons_tile_y, pl_tile_x, pl_tile_y)); }