// 专门判断子弹是否击中坦克的函数 public boolean hitTank(Bullet b, Tank et) { boolean b1 = false; // 判断该坦克的方向 switch (et.direct) { // 向上和向下范围一样 case 0: case 2: if (b.x >= et.x && b.x <= et.x + 20 && b.y >= et.y && b.y <= et.y + 30) { // 击中1.子弹死亡 2.目标死亡 b.alive = false; et.alive = false; b1 = true; // 创建一颗炸弹,放入Vector中 Bomb bomb = new Bomb(et.x, et.y); bombs.add(bomb); } break; case 1: case 3: if (b.x >= et.x && b.x <= et.x + 30 && b.y >= et.y && b.y <= et.y + 20) { // 击中 b.alive = false; et.alive = false; b1 = true; // 创建一颗炸弹,放入Vector中 Bomb bomb = new Bomb(et.x, et.y); bombs.add(bomb); } break; // default:System.out.println("xxxxxxxxx"); } return b1; }
@Override public void actionPerformed(ActionEvent arg0) { ArrayList<Bullet> bs = boat.getBulletsList(); for (int i = 0; i < bs.size(); i++) { Bullet b = (Bullet) bs.get(i); if (b.isVisible()) b.move(); else bs.remove(i); } for (int i = 0; i < buoys.size(); i++) { Buoy b = (Buoy) buoys.get(i); if (!(b.isVisible())) { gamePoint += b.getPoint(); buoys.remove(i); // System.out.println("Remaining Buoys: " + buoys.size()); } } for (int i = 0; i < bonuses.size(); i++) { Bonus b = (Bonus) bonuses.get(i); if (!(b.isVisible())) { gamePoint += b.getPoint(); bonuses.remove(i); } } // level olayı burada hallediliyor!! // en son level kalmadığında highscore' a bak yazdır if (nextLevelFlag) { System.out.println("next Level a geçtin"); currentLevel++; System.out.println("level: " + currentLevel); initGame(); // oyunu restart edecek nextLevelFlag = false; if (currentLevel > LAST_LEVEL) { // tüm levellar bittikten sonra terminate/ana menüye dönüş, tam bu anda // high score yazdır. Ancak oyunu bitirenler highscore table a girmeye hak // kazanır. // High scores to table try { writeHighScoreToFile(computeTotalScore()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } currentLevel = 1; gamePoint = 0; initGame(); CardLayout c2 = (CardLayout) (mgf.getCanvas().getLayout()); c2.show(mgf.getCanvas(), "GUI"); // System.exit(0); } } else { } boat.move(); checkCollisions(); repaint(); }
/* public void onPaint(Graphics2D g) { Iterator i = _renderables.iterator(); while(i.hasNext()){ Renderable r = (Renderable) i.next(); r.render(g); } _renderables.clear(); } */ public Wave processBulletReturnFiringWave(Bullet bullet, long currentTime) { int tightMatchDistanceThreshold = 50; Wave hitWave = DUtils.findClosestWave( _waves, new Point2D.Double(bullet.getX(), bullet.getY()), _robot.getTime(), DUtils.ANY_WAVE, DUtils.FIRING_WAVE, tightMatchDistanceThreshold); if (hitWave == null) { return NO_WAVE_FOUND; } for (int x = 0; x < _bulletHitRegisters.size(); x++) { BulletHitRegister bhr = (BulletHitRegister) _bulletHitRegisters.get(x); bhr.registerBulletHit(bullet, hitWave, currentTime); } hitWave.processedBulletHit = true; return hitWave; }
@Override public void update(double t) { { // UPDATE TOWERS for (Tile[] tiles : getTiles()) for (Tile tile : tiles) if (tile.hasTower()) tile.getTower().update(t); } { // UPDATE BULLETS final Iterator<Bullet> it = getBullets().iterator(); while (it.hasNext()) { final Bullet b = it.next(); if (b.update(t)) it.remove(); } } { // UPDATE MOBS final Iterator<Mob> it = getMobs().iterator(); while (it.hasNext()) { final Mob m = it.next(); m.update(t); if (m.shouldBeRemoved()) { m.onRemove(getGame()); it.remove(); } } } }
public static void main(String[] args) { World world = new World("defense"); ArrayList<Enemy> enemies = new ArrayList<Enemy>(); ArrayList<Tower> towers = new ArrayList<Tower>(); ArrayList<Bullet> bullets = new ArrayList<Bullet>(); while (true) { world.draw(); for (Bullet bullet : bullets) { bullet.move(); // TODO bullet.draw(); // TODO } for (int i = 0; i < enemies.size(); i++) { Enemy enemy = enemies.get(i); enemy.move(); enemy.draw(); for (int j = 0; j < bullets.size(); j++) { Bullet bullet = bullets.get(j); if (bullet.isHitting(enemy)) { enemies.remove(i); bullets.remove(j); i--; break; } } } if (Window.mouse.clicked()) { int x = Window.mouse.getX() / world.getScale(); int y = Window.mouse.getY() / world.getScale(); Space space = world.getSpace(x, y); String color = "orange"; int reload = 50; int range = world.getScale() * 5; if (space.getType() == world.GRASS && space.hasTower() == false) { Tower tower = new Tower(space, world, color, reload, range, null); tower.setComparator(new CloseComparator(tower)); towers.add(tower); } } for (Tower tower : towers) { tower.draw(world.getScale() / 2); // TODO Bullet bullet = tower.fire(enemies); if (bullet != null) { bullets.add(bullet); } } Window.frame(); if (Window.random(-40, 40) == 0) { enemies.add(new Enemy(10, 3, world)); } } }
public void mouseReleased(MouseEvent e) { left_mouse_pressed = false; double kvadrat = Math.sqrt(angleX * angleX + angleY * angleY); double tempx = angleX / kvadrat; double tempy = Math.sqrt(1 - tempx * tempx); if (angleY < 0) tempy = -tempy; Bullet b = new Bullet(); boolean next = false; if (type_bullet1 == true && count_sphere > 0) { b = new Sphere(); count_sphere--; next = true; } if (type_bullet2 == true && count_fireball > 0) { b = new FireBall(); count_fireball--; next = true; } if (type_bullet3 == true && count_rocket > 0) { b = new Rocket(); count_rocket--; next = true; } if (next == true) { b.bullet_x = player.getX() + 52 + 5 * Math.cos(angle); // + 50; //koef; b.bullet_y = player.getY() + 49 + 5 * Math.sin(angle); // player.getY() + 35; //25; b.current_direction_x = tempx; b.current_direction_y = tempy; bullet.addElement(b); allow_shoot = false; } }
public boolean processInterception() { Quadrant bulletQuadrant = bullet.getLocation(); AbstractTank opponent = getOpponent(); Quadrant opponentQuadrant = getOpponentLocation(); if (opponentQuadrant.equals(bulletQuadrant)) { bullet.destroy(); boolean isDestroyed = opponent.destroy(); if (isDestroyed) { System.out.println("TANK DESTROYED: " + opponent); gameOver(opponent); } return true; } FieldObject object = battleField.scan(bulletQuadrant); if (object != null) { battleField.update(bulletQuadrant, null); System.out.println( "QUADRANT CLEANED, vertical: " + bulletQuadrant.v + ", horizontal: " + bulletQuadrant.h); if (object.getClass() == Eagle.class) { gameOver(object); } return true; } return false; }
public void render(Bullet bullet) { Vector adjustedPosition = bullet.position().add(viewPoint); int x1 = (int) adjustedPosition.x(); int y1 = (int) adjustedPosition.y(); Vector tail = bullet.heading().reverse(); int x2 = x1 + (int) tail.x() * 10; int y2 = y1 + (int) tail.y() * 10; graphics.drawLine(x1, y1, x2, y2); }
/** Checks all collisions between collidables */ public void checkCollisions() { ArrayList<Bullet> bulletList = boat .getBulletsList(); // fire methodu çağıtıldığında boatun içinde oluşan bulletların // listesi Rectangle boatBound = boat.getBounds(); // boat ve ada çarpışması for (int i = 0; i < islands.size(); i++) { Island is = (Island) islands.get(i); Rectangle islandBound = is.getBounds(); if (boatBound.intersects(islandBound)) { boat.setVisible(false); } } // boat ve buoy çarpışması for (int i = 0; i < buoys.size(); i++) { Buoy b = (Buoy) buoys.get(i); Rectangle buoyBound = b.getBounds(); if (boatBound.intersects(buoyBound)) { b.setVisible(false); // timer.stop(); } } // boat ve uncracked/cracked bonus çarpışması for (int i = 0; i < bonuses.size(); i++) { Bonus b = (Bonus) bonuses.get(i); Rectangle bonusBound = b.getBounds(); if (boatBound.intersects(bonusBound)) { if (!b.isCracked()) { // boat&uncracked bonus çarpışması boat.setVisible(false); } else { // boat&bonus çarpışması b.setVisible(false); } } } // bullet ve bonus(uncracked) çarpışması for (int i = 0; i < bulletList.size(); i++) { Bullet bu = (Bullet) bulletList.get(i); Rectangle bulletBound = bu.getBounds(); for (int k = 0; k < bonuses.size(); k++) { Bonus bo = (Bonus) bonuses.get(k); Rectangle bonusBound = bo.getBounds(); if (bonusBound.intersects(bulletBound) && !bo.isCracked()) { bo.setCracked(true); // bo.setBonusImage(bo.getBonusType());//burası çok önemli, bonus baştan yaratılıyor // sayılır. bo.setRandomBonusImage(); bu.setVisible(false); } } } }
public void fireBullet() { game.bullets.add( new Bullet( new vec2f( pos.x + getTexture().getWidth() / 2f - Bullet.getTexture().getWidth() / 2f, pos.y + getTexture().getHeight() / 2f - Bullet.getTexture().getHeight() / 2f), new vec2f(0.6f, 0), game)); }
public void fireBullet() { final Bullet b = createBullet(); b.setRotation(this.getRotation()); final ConstantMovementController c = ConstantMovementController.createPolar(getBulletSpeed(), getRotation()); b.addController(c); snapAnchor(b); // b.moveAtAngle(getWidth() / 2 + 20, getRotation()); this.addSibling(b); }
public void renderGL() { for (Bullet b : getBullets()) if (b != null) b.renderGL(); GL11.glPushMatrix(); GL11.glTranslatef(location.x + 12, location.y + 12, 0); GL11.glRotatef(angleDeg, 0.f, 0.f, 1.0f); GL11.glTranslatef(-location.x - 12, -location.y - 12, 0); Project.drawQuad(shipTex, location.x, location.y, 24, 24); GL11.glPopMatrix(); }
public void drawBullets(Graphics g) { g.setColor(bulletColor); for (int i = 0; i < bullets.size(); i++) { Bullet bullet = bullets.get(i); bullet.move(); g.drawLine( bullet.x, bullet.y, bullet.targetX - (Screen.width - bullet.x), bullet.targetY - (Screen.height - bullet.y)); } }
@Override protected void paintComponent(Graphics g) { super.paintComponent(g); int i = 0; Color cc; for (int v = 0; v < 9; v++) { for (int h = 0; h < 9; h++) { if (COLORDED_MODE) { if (i % 2 == 0) { cc = new Color(252, 241, 177); } else { cc = new Color(233, 243, 255); } } else { cc = new Color(180, 180, 180); } i++; g.setColor(cc); g.fillRect(h * 64, v * 64, 64, 64); } } for (int j = 0; j < battleField.getDimentionY(); j++) { for (int k = 0; k < battleField.getDimentionX(); k++) { if (battleField.scanQuadrant(j, k).equals("B")) { String coordinates = getQuadrantXY(j + 1, k + 1); int separator = coordinates.indexOf("_"); int y = Integer.parseInt(coordinates.substring(0, separator)); int x = Integer.parseInt(coordinates.substring(separator + 1)); g.setColor(new Color(0, 0, 255)); g.fillRect(x, y, 64, 64); } } } g.setColor(new Color(255, 0, 0)); g.fillRect(tank.getX(), tank.getY(), 64, 64); g.setColor(new Color(0, 255, 0)); if (tank.getDirection() == Direction.UP) { g.fillRect(tank.getX() + 20, tank.getY(), 24, 34); } else if (tank.getDirection() == Direction.DOWN) { g.fillRect(tank.getX() + 20, tank.getY() + 30, 24, 34); } else if (tank.getDirection() == Direction.LEFT) { g.fillRect(tank.getX(), tank.getY() + 20, 34, 24); } else { g.fillRect(tank.getX() + 30, tank.getY() + 20, 34, 24); } g.setColor(new Color(255, 255, 0)); g.fillRect(bullet.getX(), bullet.getY(), 14, 14); }
private boolean processInterception() { String str = getQuadrantXY(bullet.getX(), bullet.getY()); int i = Integer.valueOf(str.substring(0, 1)); int q = Integer.valueOf(str.substring(2, str.length())); if (i >= battleField.getBattleField().length - battleField.getBattleField().length && i < battleField.getBattleField().length && q >= battleField.getBattleField().length - battleField.getBattleField().length && q < battleField.getBattleField().length && battleField.scanQuadrant(i, q) == "B") { battleField.updateQuadrant(i, q, " "); return true; } else return false; }
public void processFire(Bullet bullet) throws InterruptedException { this.bullet = bullet; Direction tankDirection = tank.getDirection(); while (bullet.getY() > minusLimit && bullet.getY() < plusLimit && bullet.getX() > minusLimit && bullet.getX() < plusLimit) { if (tankDirection == Direction.UP) { bullet.updateY(-1); } else if (tankDirection == Direction.DOWN) { bullet.updateY(1); } else if (tankDirection == Direction.LEFT) { bullet.updateX(-1); } else if (tankDirection == Direction.RIGHT) { bullet.updateX(1); } if (processInterception()) { bullet.destroy(); } repaint(); Thread.sleep(bullet.getSpeed()); } }
public boolean fire() { if (lastFired > fireRate) { SOUNDS.FIRE_GUN.play(); Bullet b = new Bullet(); b.setTeam(this.getTeam()); Gamestate.getInstance().addObject(b); b.fire(new Vector3f(this.turret.getPos()), new Vector2f(turret.getDir())); lastFired = 0; return true; } return false; }
/** Checks if any active bullets contact user and deletes those that are off-screen */ public void manageBullets(float dt) { for (int i = 0; i < activeBullets.size(); i++) { Bullet bullet = activeBullets.get(i); if (bullet.shouldDelete() || wallCollision(bullet)) { activeBullets.remove(i); i--; } else { bullet.update(dt); if (bullet.collidesWith(playerModel.getBoundingBox())) { playerDeath(); } } } }
/** Shoots a {@link Bullet} at the specified position */ public void shoot(Vector2 position) { Vector2 direction = new Vector2(position); direction.sub(this.getPosition()); direction.nor(); this.setAngle(direction.angleRad()); this.setTransform(body.getPosition(), direction.angleRad()); Vector2 velocity = new Vector2(direction.x * BULLET_SPEED, direction.y * BULLET_SPEED); Bullet bullet = new Bullet(level, this.getPosition(), velocity); bullet.setAngle(direction.angleRad()); level.add(bullet); }
public void tick() { for (int i = 0; i < b.size(); i++) { TempBullet = b.get(i); if (TempBullet.getY() < 0) removeBullet(TempBullet); TempBullet.tick(); } for (int i = 0; i < e.size(); i++) { TempEnemy = e.get(i); ; TempEnemy.tick(); } }
public boolean wallCollision(Bullet bullet) { Rectangle boundingBox = bullet.getBoundingBox(); int tile; int tileWidth = tileMap.getTileWidth(); int tileHeight = tileMap.getTileHeight(); /* * nested four loop only is grabbing the 4 corners of the * bullet's hit box and checking the type of tiles that they * contact. The current bullets range from 8x8 to 17x17, so they * all can contact the same range of tiles at any given time: * 1-4 A bullet expires on contact with a solid object(besides * ring bullets) */ for (int y = bullet.yPos; y <= bullet.yPos + boundingBox.getHeight(); y += boundingBox.getHeight()) { for (int x = bullet.xPos; x <= bullet.xPos + boundingBox.getWidth(); x += boundingBox.getWidth()) { int tileCoordX = (int) (x + distanceScrolled) / tileWidth; int tileCoordY = y / tileHeight; tile = tileMap.getTile(((tileCoordY) * tileMap.getTileMapWidth()) + (tileCoordX)); if (tile < 17 || 23 < tile) { return true; } } } return false; }
/** * Sets the data * * @param buffer Bytebuffer to put data into */ public void get(ByteBuffer buffer) { buffer.putInt((int) world.x); buffer.putInt((int) world.y); buffer.putDouble(radians); buffer.put((byte) (moving ? 1 : 0)); for (Bullet b : gun.getBullets()) { if (b != null) { buffer.putInt((int) b.getWorld().x); buffer.putInt((int) b.getWorld().y); buffer.putDouble(b.getAngle()); } else { buffer.putInt(-1); buffer.putInt(-1); buffer.putDouble(-1); } } }
public final void Draw(GameTime gameTime, SpriteBatch batch) { for (Bullet bullet : this.bullets) { bullet.Draw(batch); } if (!this.isAlive()) { this.removeTime = Math.max( (this.removeTime - gameTime.getElapsedGameTime()), 0f); } LColor colour = LColor.white; this.animationPlayer .Draw(gameTime, batch, this.position, colour, 0f, (Math.abs(this.rotation) > 1.570796f) ? SpriteEffects.FlipHorizontally : SpriteEffects.None); batch.draw( this.gameContent.weapon[(int) this.rank.getValue()], this.position, null, colour, MathUtils.radToDeg(this.rotation), 15, 15, 1f, (Math.abs(this.rotation) > 1.570796f) ? SpriteEffects.FlipVertically : SpriteEffects.None); if (this.isAlive()) { batch.draw(this.gameContent.healthBar, this.position.sub( (this.gameContent.healthBar.getWidth() / 2), 20f), LColor.white); rect.setBounds( 0, 0, (int) ((this.health / this.MaxHealth) * this.gameContent.healthBar .getWidth()), this.gameContent.healthBar .getHeight()); batch.draw(this.gameContent.healthBar, this.position.sub( (this.gameContent.healthBar.getWidth() / 2), 20f), rect, LColor.red); batch.flush(); } }
@Override public void collide(final Vector2D normal, final Bullet bullet) { if (energy >= 0) { energy -= BULLET_DAMAGE; energy = Math.max(energy, 0); } bullet.collide(normal, this); }
/** Writes <code>this</code> to a stream for client/server transmission. */ @Override public void flatten(DataOutputStream stream) throws IOException { super.flatten(stream); stream.writeInt(speed); // Flatten all of the units. stream.writeInt(units.size()); for (Unit u : units) ((Bullet) u).flatten(stream); }
private boolean processInterception() { String currentCoordinate = getQuadrant(bullet.getX(), bullet.getY()); int y = Integer.parseInt(currentCoordinate.substring(0, 1)); int x = Integer.parseInt(currentCoordinate.substring(2, 3)); boolean result = false; if (battleField.scanQuadrant(y, x) == " ") { result = false; } if (!battleField.scanQuadrant(y, x).trim().isEmpty()) { battleField.updateQuadrant(y, x, " "); result = true; } return result; }
private void shoot() { long now = TimeUtils.nanosToMillis(TimeUtils.nanoTime()); if (now - lastShootTime > shootInterval) { lastShootTime = now; float x = position.x; float y = position.y; // Use random floats and shootDeviation to add a feeling of accuracy to the shoot float directionX = dirX + (ran.nextFloat() * (shootDeviation + shootDeviation) - shootDeviation); float directionY = dirY + (ran.nextFloat() * (shootDeviation + shootDeviation) - shootDeviation); lastShoot = new Bullet(bulletTex, x, y, directionX, directionY, false, attack); lastShoot.xDisplacement = 28 * (size.x / 32); lastShoot.yDisplacement = 28 * (size.y / 32); lastShoot.updateAABB(); } }
@Override public void draw() { this.update(); for (float x = -0.2f; x <= 0.2f; x += 0.1f) { for (float y = 0f; y <= 0.2f; y += 0.1f) { Bullet bullet; bullet = new Bullet(this.getX() + x, this.getY() - 0.5f + y); bullet.setPos(this.getX() + x, this.getY() - 0.5f + y, this.getZ()); // bulletSet.add(bullet); } } for (Bullet bullet : bulletSet) { // bullet.draw(); } super.draw(); cleanBullet(); }
@Override protected Set<BulletListener> filter(Bullet bullet, Set<BulletListener> set) { if (map == null) { throw new NoMapException(); } Vector2 collision = map.getBulletCollision(bullet); if (collision == null) { throw new IllegalBulletException(); } Set<BulletListener> removeSet = new HashSet<BulletListener>(); for (BulletListener l : set) { if (l.getTeam() == bullet.getTeam()) { removeSet.add(l); } } set.removeAll(removeSet); float maxRange = bullet.getPosition().dst(collision); BulletListener reciever = null; for (BulletListener l : set) { Vector2 listenerCollision = bullet.intersects(l.getHitBox()); float distance = bullet.getPosition().dst(listenerCollision); if (!listenerCollision.isZero() && distance < maxRange) { collision = listenerCollision; reciever = l; maxRange = distance; } } FlyingBullet flyingBullet; Set<BulletListener> recieveSet = new HashSet<BulletListener>(); if (reciever != null) { recieveSet.add(reciever); } flyingBullet = new FlyingBullet(bullet.getPosition(), collision); flyingBullets.add(flyingBullet); return recieveSet; }
public void render(Graphics g) { for (int i = 0; i < b.size(); i++) { TempBullet = b.get(i); TempBullet.render(g); } for (int i = 0; i < e.size(); i++) { TempEnemy = e.get(i); ; TempEnemy.render(g); } }