/** * This method detects whether the first sprite is overtop of the second sprite. * * @param one The first sprite * @param two The second sprite * @return Returns true if the two sprites are overtop of each other and false if they are not */ public static boolean detectCollision(Sprite one, Sprite two) { // To prevent errors concerning concurrent modification if (one != null && two != null) { // X coordinate detection int oneLeft = one.getX(); int oneRight = one.getX() + one.getWidth(); int twoLeft = two.getX(); int twoRight = two.getX() + two.getWidth(); boolean overLeftSide = (oneRight >= twoLeft) && (oneRight <= twoRight); boolean overRightSide = (oneLeft <= twoRight) && (oneLeft >= twoLeft); boolean overHoriz = (oneLeft <= twoLeft && oneRight >= twoRight); // Y coordinate detection int oneTop = one.getY(); int oneBottom = one.getY() + one.getHeight(); int twoTop = two.getY(); int twoBottom = two.getY() + two.getHeight(); boolean overTopSide = (oneBottom >= twoTop) && (oneBottom <= twoBottom); boolean overBottomSide = (oneTop <= twoBottom) && (oneTop >= twoTop); boolean overVert = (oneTop <= twoTop && oneBottom >= twoBottom); if ((overLeftSide || overRightSide || overHoriz) && (overTopSide || overBottomSide || overVert)) { return true; } } return false; }
/** Force a packet to be sent. */ public void sendPacket() { // Also save stuff to a player's local hard-drive. if (activationEvent) { View MyView = MyHacker.getView(); Object[] send = new Object[] {new Integer(activationID), new Integer(activationType), MyHacker.getIP()}; MyView.addFunctionCall( new RemoteFunctionCall(Hacker.HACKTENDO_PLAYER, "hacktendoActivate", send)); } else if (targetEvent) { View MyView = MyHacker.getView(); Object[] send = null; if (playerSprite != null) send = new Object[] { new Integer(targetX), new Integer(targetY), MyHacker.getIP(), new Integer(playerSprite.getX()), new Integer(playerSprite.getY()) }; else send = new Object[] { new Integer(targetX), new Integer(targetY), MyHacker.getIP(), new Integer(targetX), new Integer(targetY) }; MyView.addFunctionCall( new RemoteFunctionCall(Hacker.HACKTENDO_PLAYER, "hacktendoTarget", send)); } activationEvent = false; targetEvent = false; }
public void paint(Graphics g) { super.paint(g); Graphics2D g2d = (Graphics2D) g; c.getAlpha(); g.setFont(font2); g.setColor(c); g.drawImage(fon, 0, 0, 1024, 700, null); if (type_bullet1 == true) g.drawImage(pointer, 30, 550, 49, 47, null); image_bullet = new Sphere(); g.drawImage(image_bullet.getImage(), 40, 600, 24, 24, null); g.drawString(Integer.toString(count_sphere), 35, 650); if (type_bullet2 == true) g.drawImage(pointer, 85, 550, 49, 47, null); image_bullet = new FireBall(); g.drawImage(image_bullet.getImage(), 95, 600, 24, 24, null); g.drawString(Integer.toString(count_fireball), 95, 650); if (type_bullet3 == true) g.drawImage(pointer, 140, 550, 49, 47, null); image_bullet = new Rocket(); g.drawImage(image_bullet.getImage(), 145, 595, 40, 40, null); g.drawString(Integer.toString(count_rocket), 150, 650); distance = 10; // Draw life for (int i = 0; i < player_live; i++) { g.drawImage(live, distance, 10, 11, 57, null); distance += 20; } // Create bullet for (int i = 0; i < bullet.size(); i++) { if (type_bullet1 == true && count_sphere > 0) { g2d.drawImage( bullet.get(i).getImage(), (int) bullet.get(i).bullet_x, (int) bullet.get(i).bullet_y, this); } if (type_bullet2 == true && count_fireball > 0) { g2d.drawImage( bullet.get(i).getImage(), (int) bullet.get(i).bullet_x, (int) bullet.get(i).bullet_y, this); } if (type_bullet3 == true && count_rocket != 0) { bullet.get(i).set_rotate(angle); g2d.setTransform(bullet.get(i).rotateX()); g2d.drawImage( bullet.get(i).getImage(), (int) bullet.get(i).bullet_x, (int) bullet.get(i).bullet_y, this); } } g2d.setTransform(player.rotateX(angle)); g2d.drawImage(player.getImage(), (int) player.getX(), (int) player.getY(), this); for (int i = 0; i < v_explosion.size(); i++) { g.drawImage(explosion, (int) v_explosion.get(i).x, (int) v_explosion.get(i).y, 59, 57, null); } for (int i = 0; i < enemy.size(); i++) { double povorot3 = Math.atan2(player.getY() - enemy.get(i).getY(), player.getX() - enemy.get(i).getX()) * 180 / 3.14; g2d.setTransform(enemy.get(i).rotateX(povorot3)); // new g2d.drawImage( enemy.get(i).getImage(), (int) enemy.get(i).getX(), (int) enemy.get(i).getY(), this); g2d.setTransform(enemy.get(i).rotateX(0)); // new g.drawString( Integer.toString(enemy.get(i).get_live()), (int) enemy.get(i).getX() + (int) enemy.get(i).get_spr_w() / 2, (int) enemy.get(i).getY()); } g.setFont(font); g2d.setTransform(affine); g.drawString("Level: " + Integer.toString(level), 710, 50); g.drawString("Next level: " + Integer.toString(time_level), 710, 90); }
public void compute() { for (int i = 0; i < bullet.size(); i++) { bullet.get(i).bullet_x = bullet.get(i).bullet_x + 3 * bullet.get(i).current_direction_x; bullet.get(i).bullet_y = bullet.get(i).bullet_y + 3 * bullet.get(i).current_direction_y; } for (int i = 0; i < bullet.size(); i++) { if (bullet.get(i).bullet_x > 1366 || bullet.get(i).bullet_x < 0 || bullet.get(i).bullet_y > 768 || bullet.get(i).bullet_y < 0) bullet.remove(i); } // Check collision bullet with enemy for (int i = 0; i < bullet.size(); i++) { for (int j = 0; j < enemy.size(); j++) { if (collision.collision_bullet_enemy( 18, (int) bullet.get(i).bullet_x + 18, (int) bullet.get(i).bullet_y + 18, // bullet (int) enemy.get(j).getX(), (int) enemy.get(j).getY(), // point x1, y1 (int) enemy.get(j).getX() + (int) enemy.get(j).get_spr_w(), (int) enemy.get(j).getY(), // point x2, y2 (int) enemy.get(j).getX() + (int) enemy.get(j).get_spr_w(), (int) enemy.get(j).getY() + (int) enemy.get(j).get_spr_h(), (int) enemy.get(j).getX(), (int) enemy.get(j).getY() + (int) enemy.get(j).get_spr_h() // point x4, y4 ) == true) { // create_explosion(bullet.get(i).bullet_x, bullet.get(i).bullet_y); if (bullet.get(i).get_type() == 's') // simple bullet { enemy.get(j).dec_live(); } if (bullet.get(i).get_type() == 'f') // fireball { enemy.get(j).dec_live(); enemy.get(j).dec_live(); } if (bullet.get(i).get_type() == 'r') // rocket { enemy.get(j).dec_live(); enemy.get(j).dec_live(); enemy.get(j).dec_live(); } if (enemy.get(j).get_live() <= 0) { enemy.remove(j); } else // push enemy { enemy.get(j).push(); } bullet.remove(i); break; } } } // Преследование игрока for (int i = 0; i < enemy.size(); i++) { if (enemy.get(i).get_type() == "enemy1" || enemy.get(i).get_type() == "enemy2" || enemy.get(i).get_type() == "enemy3") { angle2 = Math.atan2(player.getY() - enemy.get(i).getY(), player.getX() - enemy.get(i).getX()) * 180 / 3.14; double a = player.getX() - enemy.get(i).getX(); double b = player.getY() - enemy.get(i).getY(); double kvadrat = Math.sqrt((a * a + b * b)); double tempx = a / kvadrat; double tempy = Math.sqrt(1 - tempx * tempx); if (b < 0) tempy = -tempy; enemy.get(i).v_x = enemy.get(i).speed * tempx; enemy.get(i).v_y = enemy.get(i).speed * tempy; enemy.get(i).setX((enemy.get(i).getX() + enemy.get(i).speed * tempx)); enemy.get(i).setY((enemy.get(i).getY() + enemy.get(i).speed * tempy)); } else { if (enemy.get(i).created == false) { angle2 = Math.atan2(player.getY() - enemy.get(i).getY(), player.getX() - enemy.get(i).getX()) * 180 / 3.14; double a = player.getX() - enemy.get(i).getX(); double b = player.getY() - enemy.get(i).getY(); double kvadrat = Math.sqrt((a * a + b * b)); double tempx = a / kvadrat; double tempy = Math.sqrt(1 - tempx * tempx); if (b < 0) tempy = -tempy; enemy.get(i).v_x = enemy.get(i).speed * tempx; enemy.get(i).v_y = enemy.get(i).speed * tempy; enemy.get(i).created = true; } enemy.get(i).setX((enemy.get(i).getX() + enemy.get(i).get_vx())); enemy.get(i).setY((enemy.get(i).getY() + enemy.get(i).get_vy())); } } for (int j = 0; j < enemy.size(); j++) { if (collision.collision_with_box( (int) player.getX() + 14, (int) player.getY() + 28, (int) player.getX() + 114, (int) player.getY() + 95, (int) enemy.get(j).getX() + 22, (int) enemy.get(j).getY() + 30, (int) enemy.get(j).getX() + 102, (int) enemy.get(j).getY() + 96) == true) { enemy.remove(j); player_live--; if (player_live == 0) { JOptionPane.showMessageDialog(null, "Game over!"); start_game(); } } } if (time_game > frequency) { generate_enemy(); time_game = 0; } if (left_mouse_pressed == true && allow_shoot == true) {} time_game += 0.01; frequency -= 0.0001; }