/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> public void Update(GameTime gameTime) // is the user dead? if so handle it { if (game_state.local_player.getHealth() <= 0) { die(); return; } PlayerDir pot_dir = game_state.local_player.getDirection(); int pot_x = game_state.local_player.getX(); int pot_y = game_state.local_player.getY(); double new_width = ((double)health_bar_width) * (double)((double)game_state.local_player.getHealth() / (double)game_state.local_player.getMaxHealth()); health_bar_rec.Width = (int)new_width; TouchCollection touchCollection = TouchPanel.GetState(); foreach (TouchLocation tl in touchCollection) // for each place the screen has been touched at the point of "getState" { //if the screen is pressed on an arrow, move sprite accordingly if (tl.State == TouchLocationState.Pressed || tl.State == TouchLocationState.Moved) { if ((tl.Position.X >= 60) && (tl.Position.X <= 110) && (tl.Position.Y >= 330) && (tl.Position.Y <= 380)) // up arrow { pot_y -= game_state.local_player.getSpeed(); pot_dir = PlayerDir.UP; // -1 = up direction (axis goes down incresingly) } if ((tl.Position.X >= 60) && (tl.Position.X <= 110) && (tl.Position.Y >= 420) && (tl.Position.Y <= 470)) // down arrow { pot_y += game_state.local_player.getSpeed(); pot_dir = PlayerDir.DOWN; } if ((tl.Position.X >= 15) && (tl.Position.X <= 65) && (tl.Position.Y >= 375) && (tl.Position.Y <= 425)) // left arrow { pot_x -= game_state.local_player.getSpeed(); pot_dir = PlayerDir.LEFT; } if ((tl.Position.X >= 100) && (tl.Position.X <= 150) && (tl.Position.Y >= 375) && (tl.Position.Y <= 425)) // right arrow { pot_x += game_state.local_player.getSpeed(); pot_dir = PlayerDir.RIGHT; } //Collision and updating if (game_state.coll_engine.check_map_col(pot_x, pot_y, game_state.local_player.getWidth(), game_state.local_player.getHeight()) == false) { if (game_state.obj_mang.checkForGateAt(pot_x, pot_y, game_state.local_player.getWidth(), game_state.local_player.getHeight()) && !game_state.local_player.hasKey()) { //there is a gate and the player does not have a key } else //no gate or the player has the key { game_state.local_player.setX(pot_x); game_state.local_player.setY(pot_y); Item item = game_state.obj_mang.getItemAt(pot_x, pot_y, game_state.local_player.getWidth(), game_state.local_player.getHeight()); if (item != null) { game_state.fx_engine.RequestSound(soundType.ITEM_PICKUP); game_state.local_player.addItem(item); // item collected, set weapon type to respective type of item collected if (game_state.local_player.getWeapon() == weaponType.NONE) { if (item.getType() == itemType.SWORD) { game_state.local_player.setWeapon(weaponType.SWORD); } if (item.getType() == itemType.LASER) { game_state.local_player.setWeapon(weaponType.LASER); } } } if (!game_state.local_player.moving) // player is still { game_state.local_player.setDirection(pot_dir); game_state.local_player.moving = true; character_sprite[(int)game_state.local_player.getWeapon()].StartAnimating((int)pot_dir * 3, ((int)pot_dir * 3) + 2); } } } } else if (tl.State == TouchLocationState.Released) { if (tl.Position.X >= backpackpos.X && tl.Position.X <= backpackpos.X + backpack.Width && tl.Position.Y >= backpackpos.Y && tl.Position.Y <= backpackpos.Y + backpack.Height) { // has user elected to bring up inventory? backpackmenu.backpack_touched = true; //^^ yes } if (backpackmenu.backpack_touched == false) // if they havent { if ((tl.Position.X >= 700) && (tl.Position.Y >= 385)) // and the Fire button is tapped { game_state.fx_engine.RequestRumble(200); if (game_state.local_player.getWeapon() == weaponType.LASER) //if the proper weapon is selected and able to shoot bullets while the fire button is tapped { game_state.bullet_engine.fire(game_state.local_player.getX() + (int)character_sprite[(int)game_state.local_player.getWeapon()].size.X / 2, game_state.local_player.getY() + (int)character_sprite[(int)game_state.local_player.getWeapon()].size.Y / 2, game_state.local_player.getDirection(), bulletOwner.PLAYER, bulletType.SMALL); game_state.fx_engine.RequestSound(soundType.SHOOT); // shoot a bullet } else if (game_state.local_player.getWeapon() == weaponType.SWORD) // else swing the sword, dont shoot bullets { sword_swing = true; game_state.fx_engine.RequestSound(soundType.SWORD); int bullet_x = 0; int bullet_y = 0; switch (game_state.local_player.getDirection()) { case PlayerDir.DOWN: bullet_x = game_state.local_player.getX() - 2; bullet_y = game_state.local_player.getY() + game_state.local_player.getHeight(); break; case PlayerDir.UP: bullet_x = game_state.local_player.getX() - 2; bullet_y = game_state.local_player.getY() - game_state.local_player.getHeight(); break; case PlayerDir.LEFT: bullet_x = game_state.local_player.getX() - (game_state.local_player.getWidth()); bullet_y = game_state.local_player.getY(); break; case PlayerDir.RIGHT: bullet_x = game_state.local_player.getX() + game_state.local_player.getWidth(); bullet_y = game_state.local_player.getY(); break; } sword_bullet = game_state.bullet_engine.fire(bullet_x, bullet_y, game_state.local_player.getDirection(), bulletOwner.PLAYER, bulletType.SWORD); } } } else // bring up the inventory and display the items properly and in an orderly fashion { Vector2 formatpos = new Vector2(315, 170); int tileSize = game_state.tile_engine.getTileSize(); Item toRemove = null; foreach (Item i in game_state.local_player.getInventory()) { Rectangle dest = new Rectangle((int)formatpos.X, (int)formatpos.Y, 300, 40); if (dest.Contains((int)tl.Position.X, (int)tl.Position.Y)) { //menu item Clicked and ready to be used in game switch (i.getType()) { case itemType.LASER: game_state.local_player.setWeapon(weaponType.LASER); break; case itemType.SWORD: game_state.local_player.setWeapon(weaponType.SWORD); break; case itemType.ATT_BOOST: toRemove = i; att_boost_delay = 10000; game_state.local_player.setAttackBonus(5); break; case itemType.DEF_BOOST: toRemove = i; def_boost_delay = 10000; game_state.local_player.setDefenseBonus(5); break; case itemType.KEY: default: break; } backpackmenu.backpack_touched = false; // exit from inventory now } formatpos.Y += 50; } if (toRemove != null) { game_state.local_player.removeItem(toRemove); } if (backpackExit.Contains((int)tl.Position.X, (int)tl.Position.Y)) // coordinates of the "exit" button in inventory { backpackmenu.backpack_touched = false; //user has exited, return to main game screen } } character_sprite[(int)game_state.local_player.getWeapon()].StopAnimating(); game_state.local_player.moving = false; } } if (sword_swing) // swing sword { sword_delay += gameTime.ElapsedGameTime.Milliseconds; if (sword_delay > 200) { sword_delay = 0; sword_swing = false; game_state.bullet_engine.RemoveBullet(sword_bullet); } } if (game_state.local_player.getAttackBonus() > 0) //add attack bonuses { att_boost_delay -= gameTime.ElapsedGameTime.Milliseconds; if (att_boost_delay <= 0) { game_state.local_player.setAttackBonus(0); } } if (game_state.local_player.getDefenseBonus() > 0) // add defensive bonuses { def_boost_delay -= gameTime.ElapsedGameTime.Milliseconds; if (def_boost_delay <= 0) { game_state.local_player.setDefenseBonus(0); } } List <ColToken> cols = game_state.local_player.col_tok.GetCollisions(); for (int j = 0; j < cols.Count(); ++j) { ColToken coll = cols.ElementAt(j); if (coll.GetLocalType() != ColType.MAP) { if (!game_state.local_player.hurt) { int damage = 0; if (coll.GetLocalType() == ColType.BULLET) { Bullet bul = (Bullet)coll.GetParent(); if (bul.owner == bulletOwner.PLAYER) { continue; } } else if (coll.GetLocalType() == ColType.MONSTER) { Enemy enem = (Enemy)coll.GetParent(); damage = enem.getAttack(); } //Player gets hurt! game_state.local_player.setHealth(game_state.local_player.getHealth() + game_state.local_player.getDefenseBonus() - damage); game_state.local_player.hurt = true; game_state.fx_engine.RequestSound(soundType.PLAYER_HURT); //Get hurt by a little hurt_time = 500; } } } game_state.local_player.col_tok.ResetCollisions(); game_state.local_player.col_tok.update(game_state.local_player.getX(), game_state.local_player.getY()); if (game_state.local_player.hurt) { hurt_time -= gameTime.ElapsedGameTime.Milliseconds; // dont repeatedly get hurt, only get hurt once per attack by an enemy if (hurt_time <= 0) { game_state.local_player.hurt = false; // time to get hurt has run out } } currTime -= gameTime.ElapsedGameTime; // start timer on actual game if (currTime.TotalSeconds <= 0) // time per level has run out { return; } game_state.monster_engine.Update(gameTime.ElapsedGameTime.Milliseconds); game_state.bullet_engine.Update(); game_state.coll_engine.Update(); game_state.fx_engine.Update(gameTime.ElapsedGameTime.Milliseconds); if (testAtExit(game_state.local_player.getX(), (game_state.local_player.getY() + game_state.local_player.getHeight() - 1)) || testAtExit(game_state.local_player.getX() + game_state.local_player.getWidth(), (game_state.local_player.getY() + game_state.local_player.getHeight()))) { int tempn = game_state.tile_engine.getCurrentLevel() + 1; LoadLevel(tempn); } }
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> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> public void Update(GameTime gameTime) { if (game_state.local_player.getHealth() <= 0) { die(); return; } PlayerDir pot_dir = game_state.local_player.getDirection(); int pot_x = game_state.local_player.getX(); int pot_y = game_state.local_player.getY(); double new_width = ((double)health_bar_width) * (double)((double)game_state.local_player.getHealth() / (double)game_state.local_player.getMaxHealth()); health_bar_rec.Width = (int)new_width; //Put a nicer function here TouchCollection touchCollection = TouchPanel.GetState(); foreach (TouchLocation tl in touchCollection) // for each place the screen has been touched at the point of "getState" { //if the screen is pressed on an arrow, move sprite accordingly if (tl.State == TouchLocationState.Pressed || tl.State == TouchLocationState.Moved) { if ((tl.Position.X >= 50) && (tl.Position.X <= 100) && (tl.Position.Y >= 345) && (tl.Position.Y <= 385)) // up arrow { //local_player.setY(local_player.getY() - 3); pot_y -= game_state.local_player.getSpeed(); pot_dir = PlayerDir.UP; // -1 = up direction (axis goes down incresingly) } if ((tl.Position.X >= 50) && (tl.Position.X <= 100) && (tl.Position.Y >= 425) && (tl.Position.Y <= 465)) // down arrow { pot_y += game_state.local_player.getSpeed(); pot_dir = PlayerDir.DOWN; // local_player.setY(local_player.getY() + 3); } if ((tl.Position.X >= 15) && (tl.Position.X <= 55) && (tl.Position.Y >= 385) && (tl.Position.Y <= 425)) // left arrow { pot_x -= game_state.local_player.getSpeed(); pot_dir = PlayerDir.LEFT; //local_player.setX(local_player.getX() - 3); // -1 = left because left to right } if ((tl.Position.X >= 90) && (tl.Position.X <= 130) && (tl.Position.Y >= 385) && (tl.Position.Y <= 425)) // right arrow { pot_x += game_state.local_player.getSpeed(); pot_dir = PlayerDir.RIGHT; //local_player.setX(local_player.getX() + 3); } //Collision and updating if (game_state.coll_engine.check_map_col(pot_x, pot_y, game_state.local_player.getWidth(), game_state.local_player.getHeight()) == false) { if (game_state.obj_mang.checkForGateAt(pot_x, pot_y, game_state.local_player.getWidth(), game_state.local_player.getHeight()) && !game_state.local_player.hasKey()) { //there is a gate and the player does not have a key } else {//no gate or the player has the key game_state.local_player.setX(pot_x); game_state.local_player.setY(pot_y); //System.Diagnostics.Debug.WriteLine("Stuff: {0},{1}", pot_x, pot_y); Item item = game_state.obj_mang.getItemAt(pot_x, pot_y, game_state.local_player.getWidth(), game_state.local_player.getHeight()); if (item != null) { game_state.local_player.addItem(item); if (game_state.local_player.getWeapon() == weaponType.NONE) { if (item.getType() == itemType.SWORD) { game_state.local_player.setWeapon(weaponType.SWORD); } if (item.getType() == itemType.LASER) { game_state.local_player.setWeapon(weaponType.LASER); } } } if (!game_state.local_player.moving) { game_state.local_player.setDirection(pot_dir); game_state.local_player.moving = true; character_sprite[(int)game_state.local_player.getWeapon()].StartAnimating((int)pot_dir * 3, ((int)pot_dir * 3) + 2); } } } } else if (tl.State == TouchLocationState.Released) { if (tl.Position.X >= backpackpos.X && tl.Position.X <= backpackpos.X + backpack.Width && tl.Position.Y >= backpackpos.Y && tl.Position.Y <= backpackpos.Y + backpack.Height) { backpackmenu.backpack_touched = true; } if (backpackmenu.backpack_touched == false) { if ((tl.Position.X >= 700) && (tl.Position.Y >= 385)) // Fire button { if (game_state.local_player.getWeapon() == weaponType.LASER) { game_state.bullet_engine.fire(game_state.local_player.getX() + (int)character_sprite[(int)game_state.local_player.getWeapon()].size.X / 2, game_state.local_player.getY() + (int)character_sprite[(int)game_state.local_player.getWeapon()].size.Y / 2, game_state.local_player.getDirection(), bulletOwner.PLAYER, bulletType.SMALL); game_state.fx_engine.RequestSound(soundType.SHOOT); } else if (game_state.local_player.getWeapon() == weaponType.SWORD) { sword_swing = true; game_state.fx_engine.RequestSound(soundType.SWORD); int bullet_x = 0; int bullet_y = 0; switch (game_state.local_player.getDirection()) { case PlayerDir.DOWN: bullet_x = game_state.local_player.getX() - 2; bullet_y = game_state.local_player.getY() + game_state.local_player.getHeight(); break; case PlayerDir.UP: bullet_x = game_state.local_player.getX() - 2; bullet_y = game_state.local_player.getY() - game_state.local_player.getHeight(); break; case PlayerDir.LEFT: bullet_x = game_state.local_player.getX() - (game_state.local_player.getWidth()); bullet_y = game_state.local_player.getY(); //+ game_state.local_player.getHeight(); break; case PlayerDir.RIGHT: bullet_x = game_state.local_player.getX() + game_state.local_player.getWidth(); bullet_y = game_state.local_player.getY(); //+ game_state.local_player.getHeight(); break; } sword_bullet = game_state.bullet_engine.fire(bullet_x, bullet_y, game_state.local_player.getDirection(), bulletOwner.PLAYER, bulletType.SWORD); } } } else { Vector2 formatpos = new Vector2(315, 170); int tileSize = game_state.tile_engine.getTileSize(); Item toRemove = null; foreach (Item i in game_state.local_player.getInventory()) { Rectangle dest = new Rectangle((int)formatpos.X, (int)formatpos.Y, 300, 40); if (dest.Contains((int)tl.Position.X, (int)tl.Position.Y)) { //menu item Clicked switch (i.getType()) { case itemType.LASER: game_state.local_player.setWeapon(weaponType.LASER); break; case itemType.SWORD: game_state.local_player.setWeapon(weaponType.SWORD); break; case itemType.ATT_BOOST: toRemove = i; att_boost_delay = 10000; game_state.local_player.setAttackBonus(5); break; case itemType.DEF_BOOST: toRemove = i; def_boost_delay = 10000; game_state.local_player.setDefenseBonus(5); break; case itemType.KEY: default: break; } backpackmenu.backpack_touched = false; // exit from inventory now } formatpos.Y += 50; } if (toRemove != null) { game_state.local_player.removeItem(toRemove); } if (backpackExit.Contains((int)tl.Position.X, (int)tl.Position.Y)) // coordinates of the "exit" button in inventory { backpackmenu.backpack_touched = false; //user has exited, return to main game screen } } character_sprite[(int)game_state.local_player.getWeapon()].StopAnimating(); game_state.local_player.moving = false; } } if (sword_swing) { sword_delay += gameTime.ElapsedGameTime.Milliseconds; if (sword_delay > 200) { sword_delay = 0; sword_swing = false; game_state.bullet_engine.RemoveBullet(sword_bullet); } } if (game_state.local_player.getAttackBonus() > 0) { att_boost_delay -= gameTime.ElapsedGameTime.Milliseconds; if (att_boost_delay <= 0) { game_state.local_player.setAttackBonus(0); } } if (game_state.local_player.getDefenseBonus() > 0) { def_boost_delay -= gameTime.ElapsedGameTime.Milliseconds; if (def_boost_delay <= 0) { game_state.local_player.setDefenseBonus(0); } } List <ColToken> cols = game_state.local_player.col_tok.GetCollisions(); for (int j = 0; j < cols.Count(); ++j) { ColToken coll = cols.ElementAt(j); if (coll.GetLocalType() != ColType.MAP) { if (!game_state.local_player.hurt) { int damage = 0; if (coll.GetLocalType() == ColType.BULLET) { //BulletEngine will deal the damage for this /* * Bullet bull = (Bullet)coll.GetParent(); * if (bull.owner == bulletOwner.ENEMY) * { * switch (bull.type) * { * case bulletType.SMALL: * damage = 5; * break; * case bulletType.SWORD: * damage = 10; * break; * * } * }*/ Bullet bul = (Bullet)coll.GetParent(); if (bul.owner == bulletOwner.PLAYER) { continue; } } else if (coll.GetLocalType() == ColType.MONSTER) { Enemy enem = (Enemy)coll.GetParent(); damage = enem.getAttack(); } //Player gets hurt! game_state.local_player.setHealth(game_state.local_player.getHealth() + game_state.local_player.getDefenseBonus() - damage); game_state.local_player.hurt = true; //Get hurt by a little hurt_time = 500; } } } game_state.local_player.col_tok.ResetCollisions(); game_state.local_player.col_tok.update(game_state.local_player.getX(), game_state.local_player.getY()); if (game_state.local_player.hurt) { hurt_time -= gameTime.ElapsedGameTime.Milliseconds; if (hurt_time <= 0) { game_state.local_player.hurt = false; } } currTime -= gameTime.ElapsedGameTime; // start timer on actual game if (currTime.TotalSeconds <= 0) { return; } game_state.monster_engine.Update(gameTime.ElapsedGameTime.Milliseconds); game_state.bullet_engine.Update(); game_state.coll_engine.Update(); game_state.fx_engine.Update(); if (testAtExit(game_state.local_player.getX(), (game_state.local_player.getY() + game_state.local_player.getHeight() - 1)) || testAtExit(game_state.local_player.getX() + game_state.local_player.getWidth(), (game_state.local_player.getY() + game_state.local_player.getHeight()))) { int tempn = game_state.tile_engine.getCurrentLevel() + 1; LoadLevel(tempn); } }
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 }
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); } } } }
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); } } } }