Advertisement
Guest User

Gator4K by Philip Diffenderfer

a guest
Nov 8th, 2012
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 32.04 KB | None | 0 0
  1.  
  2.  
  3. import java.applet.Applet;
  4. import java.awt.BasicStroke;
  5. import java.awt.Color;
  6. import java.awt.Font;
  7. import java.awt.GradientPaint;
  8. import java.awt.Graphics;
  9. import java.awt.Graphics2D;
  10. import java.awt.Image;
  11. import java.awt.RenderingHints;
  12. import java.awt.event.KeyEvent;
  13. import java.awt.event.MouseEvent;
  14. import java.awt.geom.AffineTransform;
  15. import java.awt.image.BufferedImage;
  16. import java.awt.image.MemoryImageSource;
  17. import java.util.Random;
  18.  
  19. public class G extends Applet implements Runnable
  20. {
  21.  
  22.     //==========================================================================
  23.     // A P P L E T   V A R I A B L E S
  24.     //==========================================================================
  25.     static final int WIDTH = 500;
  26.     static final int HEIGHT = 400;
  27.  
  28.     //==========================================================================
  29.     // G A M E   V A R I A B L E S
  30.     //==========================================================================
  31.     // CONSTANTS
  32.     static final int LEVEL_BUOY = 20;
  33.     static final int LEVEL_GATOR = 15;
  34.     static final int LEVEL_BUOY_INC = 4;
  35.     static final int LEVEL_GATOR_INC = 5;
  36.     static final float LEVEL_TIME_ADD = 5f;
  37.     static final float LEVEL_TIME_OFFSET = 30f;
  38.     static final int MONEY_GATOR = 10;
  39.     static final int MONEY_SPEAR = -5;
  40.     static final float FRAME_UPDATE_TIME = 0.3f;
  41.     static final float MESSAGE_DURATION = 4f;
  42.     // VARIABLES
  43.     final BufferedImage buffer;
  44.     final Image water_buffer;
  45.     final MemoryImageSource water_source;
  46.     final Random rnd = new Random();
  47.     boolean running;
  48.     long lastUpdate;
  49.     float deltatime;
  50.    
  51. //  float frame_time;
  52. //  float frame_rate;
  53. //  int frame_count;
  54.    
  55.     int game_level;
  56.     int game_lives;
  57.     int game_money;
  58.     float game_remaining;
  59.     boolean game_reset;
  60.     boolean game_next;
  61.     boolean game_paused;
  62.  
  63.     //==========================================================================
  64.     // B O A T
  65.     //==========================================================================
  66.     // CONSTANTS
  67.     static final float BOAT_RIPPLE_SKIP = 3f;
  68.     static final int BOAT_RIPPLE = 3;
  69.     static final int BOAT_RADIUS = 10;
  70.     static final int BOAT_QUART = 5;
  71.     static final int BOAT_EIGTHT = 2;
  72.     static final int BOAT_SIXTEEN = 1;
  73.     static final float BOAT_WEIGHT = 10f;
  74.     // VARIABLES
  75. //  final AudioClip audioGator;
  76.     final BufferedImage boat_image;
  77.     float boat_fan;
  78.     float boat_x = WIDTH / 2;
  79.     float boat_y = HEIGHT / 2;
  80.     float boat_velx, boat_vely;
  81.  
  82.     //==========================================================================
  83.     // R I P P L E S
  84.     //==========================================================================
  85.     // VARIABLES
  86.     int old_index = WIDTH;
  87.     int new_index = WIDTH * (HEIGHT + 3);
  88.     final short[] ripple_map = new short[WIDTH * (HEIGHT + 2) * 2];
  89.     final int[] ripple = new int[WIDTH * HEIGHT];
  90.  
  91.     //==========================================================================
  92.     // B U O Y
  93.     //==========================================================================
  94.     // CONSTANTS
  95.     static final int BUOY_MAX = 128;
  96.     static final int BUOY_RADIUS = 12;
  97.     static final int BUOY_STRENGTH = 4;
  98.     static final int BUOY_RIPPLE = 3;
  99.     static final Color[] BUOY_COLOR = {
  100.         Color.red, Color.black, Color.darkGray, Color.gray, Color.lightGray
  101.     };
  102.     // VARIABLES
  103.     int buoy_count;
  104.     final int[] buoy_x = new int[BUOY_MAX];
  105.     final int[] buoy_y = new int[BUOY_MAX];
  106.     final int[] buoy_health = new int[BUOY_MAX];
  107.  
  108.     //==========================================================================
  109.     // G A T O R S
  110.     //==========================================================================
  111.     // CONSTANTS
  112.     static final int GATOR_MAX = 128;
  113.     static final int GATOR_RADIUS = 8;
  114.     static final int GATOR_RIPPLE = 4;
  115.     static final float GATOR_INTERVAL = 4f;
  116.     static final float GATOR_BLINK = 3f;
  117.     // VARIABLES
  118.     int gator_count;
  119.     final float[] gator_time = new float[GATOR_MAX];
  120.     final int[] gator_x = new int[GATOR_MAX];
  121.     final int[] gator_y = new int[GATOR_MAX];
  122.     final BufferedImage gator_image_open;
  123.     final BufferedImage gator_image_closed;
  124.  
  125.     //==========================================================================
  126.     // G A T O R   B L O O D
  127.     //==========================================================================
  128.     // CONSTANTS
  129.     public static final float BLOOD_LIFE_MIN = 1.2f;
  130.     public static final float BLOOD_LIFE_MAX = 2.0f;
  131.     public static final float BLOOD_SIZE_MIN = 4.0f;
  132.     public static final float BLOOD_SIZE_MAX = 6.0f;
  133.     public static final float BLOOD_SIZE_VEL_MIN = 10.0f;
  134.     public static final float BLOOD_SIZE_VEL_MAX = 20.0f;
  135.     public static final int BLOOD_BURST_MIN = 3;
  136.     public static final int BLOOD_BURST_MAX = 8;
  137.     public static final int BLOOD_MAX = 256;
  138.     // VARIABLES
  139.     int blood_count;
  140.     final float[] blood_time = new float[BLOOD_MAX];
  141.     final float[] blood_life = new float[BLOOD_MAX];
  142.     final float[] blood_size = new float[BLOOD_MAX];
  143.     final float[] blood_size_vel = new float[BLOOD_MAX];
  144.     final int[] blood_x = new int[BLOOD_MAX];
  145.     final int[] blood_y = new int[BLOOD_MAX];
  146.  
  147.     //==========================================================================
  148.     // S P E A R S
  149.     //==========================================================================
  150.     // CONSTANTS
  151.     static final int SPEAR_MAX = 128;
  152.     static final float SPEAR_LIFE = 1.0f;
  153.     static final float SPEAR_LENGTH = 15f;
  154.     static final float SPEAR_VEL = 300f;
  155.     static final float SPEAR_RELOAD = 0.4f;
  156.     // VARIABLES
  157.     int spear_count;
  158.     final float[] spear_life = new float[SPEAR_MAX];
  159.     final float[] spear_x = new float[SPEAR_MAX];
  160.     final float[] spear_y = new float[SPEAR_MAX];
  161.     final float[][] spear_vel = new float[SPEAR_MAX][2];
  162.     float spear_time;
  163.  
  164.     //==========================================================================
  165.     // S C O R E S
  166.     //==========================================================================
  167.     // CONSTANTS
  168.     static final int SCORE_MAX = 10;
  169.     // VARIABLES
  170.     int score_count;
  171.     final int[] scores = new int[SCORE_MAX];
  172.     final int[] score_level = new int[SCORE_MAX];
  173.    
  174.     //==========================================================================
  175.     // S P R I N G
  176.     //==========================================================================
  177.     // CONSTANTS
  178.     static final float SPRING_LENGTH = 4f;
  179.     static final float SPRINT_CONSTANT = 80f;
  180.     static final float SPRING_FRICTION = 40f;
  181.    
  182.     //==========================================================================
  183.     // M O U S E   E V E N T S
  184.     //==========================================================================
  185.     int mouse_x;
  186.     int mouse_y;
  187.  
  188.     //==========================================================================
  189.     // C A C H E
  190.     //==========================================================================
  191.     // CONSTANTS
  192.     static final BasicStroke STROKE_SINGLE = new BasicStroke(1);
  193.     static final BasicStroke STROKE_SPEAR = new BasicStroke(2);
  194.     static final BasicStroke STROKE_TEETH = new BasicStroke(2,
  195.             BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 1,
  196.             new float[] {1, 1}, 1);
  197.    
  198.     static final Font FONT_GAME = new Font("Courier", Font.BOLD, 12);
  199.    
  200.     // VARIABLES
  201.     final float[] dir = new float[2];
  202.     int i, j, k;
  203.    
  204.     // Instanties a new game setting up the
  205.     public G() {
  206.         buffer = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_4BYTE_ABGR);
  207.        
  208.         water_source = new MemoryImageSource(WIDTH, HEIGHT, ripple, 0, WIDTH);
  209.         water_source.setAnimated(true);
  210.         water_buffer = createImage(water_source);
  211.        
  212. //      audioGator = newAudioClip(getClass().getResource("c"));
  213.        
  214.         enableEvents(0x38); // 0x38
  215.         Graphics2D gr;
  216.  
  217.         //==================================================================
  218.         // Draw the boat on a buffered image
  219.         //==================================================================
  220.         boat_image = new BufferedImage(BOAT_RADIUS * 2, BOAT_RADIUS * 2, BufferedImage.TYPE_4BYTE_ABGR);
  221.         gr = boat_image.createGraphics();
  222.         // Boat outer
  223.         gr.setColor(new Color(178, 77, 255));
  224.         gr.fillRect(5, 5, 15, 10);
  225.         // Boat inner
  226.         gr.setColor(new Color(92, 0, 163));
  227.         gr.fillRect(5, 7, 12, 5);
  228.         // Boat fan casing
  229.         gr.setColor(new Color(100, 100, 100, 200));
  230.         gr.fillRect(0, 3, 5, 15);
  231.         gr.dispose();
  232.        
  233.         //==================================================================
  234.         // Draw the gator (eyes closed) on a buffered image
  235.         //==================================================================
  236.         gator_image_closed = new BufferedImage(GATOR_RADIUS * 3, GATOR_RADIUS * 2, BufferedImage.TYPE_4BYTE_ABGR);
  237.         gr = gator_image_closed.createGraphics();
  238.         gr.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  239.         // TEETH
  240.         gr.setColor(Color.white);
  241.         gr.setStroke(STROKE_TEETH);
  242.         gr.drawRoundRect(GATOR_RADIUS, GATOR_RADIUS / 2, GATOR_RADIUS * 2, GATOR_RADIUS, 4, 4);
  243.         gr.setStroke(STROKE_SINGLE);
  244.         // HEAD
  245.         gr.setColor(new Color(0, 200, 0));
  246.         gr.fillOval(0, 0, GATOR_RADIUS * 2, GATOR_RADIUS * 2);
  247.         gr.fillRoundRect(GATOR_RADIUS, GATOR_RADIUS / 2, GATOR_RADIUS * 2, GATOR_RADIUS, 5, 5);
  248.         // TAIL RIDGE
  249.         gr.setColor(new Color(0, 100, 0));
  250.         gr.drawLine(0, GATOR_RADIUS, GATOR_RADIUS, GATOR_RADIUS);
  251.         gr.dispose();
  252.        
  253.         //==================================================================
  254.         // Draw the gator (eyes open) on a buffered image
  255.         //==================================================================
  256.         gator_image_open = new BufferedImage(GATOR_RADIUS * 3, GATOR_RADIUS * 2, BufferedImage.TYPE_4BYTE_ABGR);
  257.         gr = gator_image_open.createGraphics();
  258.         gr.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  259.         gr.drawImage(gator_image_closed, 0, 0, null);
  260.         // EYES
  261.         gr.setColor(Color.white);
  262.         gr.fillOval(GATOR_RADIUS + GATOR_RADIUS / 2 - 2, GATOR_RADIUS / 2 - 2, 4, 4);
  263.         gr.fillOval(GATOR_RADIUS + GATOR_RADIUS / 2 - 2, GATOR_RADIUS + GATOR_RADIUS / 2 - 2, 4, 4);
  264.         // PUPILS
  265.         gr.setColor(Color.black);
  266.         gr.fillOval(GATOR_RADIUS + GATOR_RADIUS / 2, GATOR_RADIUS / 2 - 1, 2, 2);
  267.         gr.fillOval(GATOR_RADIUS + GATOR_RADIUS / 2, GATOR_RADIUS + GATOR_RADIUS / 2 - 1, 2, 2);
  268.         gr.dispose();
  269.     }
  270.  
  271.     // Initializes the applet requesting its size, setting up the initial
  272.     // background ripple map, and starts the thread which runs the applet.
  273.     public void init() {
  274.         setSize(WIDTH, HEIGHT);
  275.         updateRippleEffect();
  276.         running = true;
  277.         game_paused = true;
  278.         game_reset = true;
  279.         new Thread(this).start();
  280.     }
  281.  
  282.     // The game loop executed by a thread.
  283.     public void run() {
  284.        
  285.         lastUpdate = System.nanoTime();
  286.         while (running) {
  287.  
  288.             //==================================================================
  289.             // Update deltatime - time in seconds since the last game loop
  290.             //==================================================================
  291.             long time = System.nanoTime();
  292.             deltatime = (time - lastUpdate) * 0.000000001f;
  293.             lastUpdate = time;
  294.  
  295.             //==================================================================
  296.             // If the game is paused then freeze all updating
  297.             //==================================================================
  298.             if (!game_paused) {
  299.                
  300.                 //==================================================================
  301.                 // Update the time since the last spear throw
  302.                 //==================================================================
  303.                 spear_time += deltatime;
  304.    
  305.                 //==================================================================
  306.                 // U P D A T E   B O A T
  307.                 //==================================================================
  308.                 // Update the fan rotation
  309.                 boat_fan += deltatime * 5;
  310.                 if (boat_fan > 1) {
  311.                     boat_fan = 0;
  312.                 }
  313.    
  314.                 //==================================================================
  315.                 // Update the position of the boat with respect to the mouse
  316.                 // position. Apply spring physics between the boat and mouse.
  317.                 dir[0] = mouse_x - boat_x;
  318.                 dir[1] = mouse_y - boat_y;
  319.                 float distance = normalize(dir);
  320.                 if (distance > 1f) {
  321.                     // Force of the spring between the masses
  322.                     float force = Math.abs(SPRING_LENGTH - distance) * -SPRINT_CONSTANT;
  323.                     float scale = deltatime / BOAT_WEIGHT;
  324.                     // Adjust the boats velocity with the springs force
  325.                     boat_velx -= ((dir[0] * force) + boat_velx * SPRING_FRICTION) * scale;
  326.                     boat_vely -= ((dir[1] * force) + boat_vely * SPRING_FRICTION) * scale;
  327.                     // Updates the boats position
  328.                     boat_x += boat_velx * deltatime;
  329.                     boat_y += boat_vely * deltatime;               
  330.                 }
  331.                
  332.                 //==================================================================
  333.                 // Update the ripples behind the boat.
  334.                 addRipple((int)boat_x, (int)boat_y, BOAT_RIPPLE);
  335.    
  336.                 //==================================================================
  337.                 // Handle the boat hitting the gators
  338.                 //==================================================================
  339.                 final int BOAT_GATOR = BOAT_RADIUS + GATOR_RADIUS;
  340.                 for (i = 0; i < gator_count; i++) {
  341.                     // If the boat and gator intersect then kill the gator
  342.                     if (intersects(boat_x, boat_y, gator_x[i], gator_y[i], BOAT_GATOR)) {
  343.                         killGator(i);
  344.                     }
  345.                 }
  346.                
  347.                 //==================================================================
  348.                 // Handle the spears hitting the gators
  349.                 //==================================================================
  350.                 for (i = 0; i < spear_count; i++) {
  351.                     for (j = 0; j < gator_count; j++) {
  352.                         // If the spear and gator intersect remove them both
  353.                         if (intersects(spear_x[i], spear_y[i], gator_x[j], gator_y[j], GATOR_RADIUS)) {
  354.                             killGator(j);
  355.                             killSpear(i);
  356.                         }
  357.                     }
  358.                 }
  359.                
  360.                 //==================================================================
  361.                 // Handle the boat bouncing off of the buoys
  362.                 //==================================================================
  363.                 final int BOAT_BUOY = BOAT_RADIUS + BUOY_RADIUS;
  364.                 final int BOAT_BUOY_SQ = BOAT_BUOY * BOAT_BUOY;
  365.                 for (i = 0; i < buoy_count; i++)
  366.                 {
  367.                     dir[0] = boat_x - buoy_x[i];
  368.                     dir[1] = boat_y - buoy_y[i];
  369.                     // Has the boat intersected the buoy?
  370.                     if (dir[0] * dir[0] + dir[1] * dir[1] < BOAT_BUOY_SQ) {
  371.                         // Normalize the vector and get the distance between.
  372.                         float dist = normalize(dir);
  373.                         // How far was the boat inserted into the buoy
  374.                         float insert = (BOAT_BUOY + 2) - dist;
  375.                         // How fast was the boat going
  376.                         float speed = (float)Math.sqrt(boat_velx * boat_velx + boat_vely * boat_vely);
  377.                         // Move the boat outside of the buoy (+2 pixels)
  378.                         boat_x -= (boat_velx / speed) * insert;
  379.                         boat_y -= (boat_vely / speed) * insert;
  380.                        
  381.                         // Reflect the velocity of the boat to the normal of the buoy
  382.                         float dot = 2 * (-dir[1] * boat_velx + dir[0] * boat_vely);
  383.                         boat_velx = (dot * -dir[1]) - boat_velx;
  384.                         boat_vely = (dot * dir[0]) - boat_vely;
  385.                         // Decrease the buoy's health
  386.                         buoy_health[i]--;
  387.  
  388.                         // Add a ripple under the buoy
  389.                         addRipple(buoy_x[i], buoy_y[i], BUOY_RIPPLE);
  390.    
  391.                         // If the buoy has been completely destroyed...
  392.                         if (buoy_health[i] == 0) {
  393.                             // Remove the buoy from the list.
  394.                             buoy_count--;
  395.                             buoy_x[i] = buoy_x[buoy_count];
  396.                             buoy_y[i] = buoy_y[buoy_count];
  397.                             buoy_health[i] = buoy_health[buoy_count];
  398.                            
  399.                             // Decrease a game life
  400.                             game_lives--;
  401.                             // If there are no more lives the restart the game.
  402.                             if (game_lives <= 0) {
  403.                                 saveScore();
  404.                                 game_reset = true;
  405.                                 game_paused = true;
  406.                             }
  407.                         }
  408.                     }
  409.                 }          
  410.    
  411.                 //==================================================================
  412.                 // Update the Blood Effect
  413.                 //==================================================================
  414.                 for (i = 0; i < blood_count; i++) {
  415.                     blood_time[i] += deltatime;
  416.                     blood_size[i] += blood_size_vel[i] * deltatime;
  417.                     if (blood_time[i] > blood_life[i]) {
  418.                         blood_count--;
  419.                         blood_x[i] = blood_x[blood_count];
  420.                         blood_y[i] = blood_y[blood_count];
  421.                         blood_time[i] = blood_time[blood_count];
  422.                         blood_life[i] = blood_life[blood_count];
  423.                         blood_size[i] = blood_size[blood_count];
  424.                         blood_size_vel[i] = blood_size_vel[blood_count];
  425.                     }
  426.                 }
  427.    
  428.                 //==================================================================
  429.                 // Update the Spears
  430.                 //==================================================================
  431.                 for (i = 0; i < spear_count; i++) {
  432.                     spear_x[i] += spear_vel[i][0] * SPEAR_VEL * deltatime;
  433.                     spear_y[i] += spear_vel[i][1] * SPEAR_VEL * deltatime;
  434.                     spear_life[i] -= deltatime;
  435.                     if (spear_life[i] < 0) {
  436.                         killSpear(i);
  437.                     }
  438.                 }
  439.  
  440.                 //==================================================================
  441.                 // Update the Gators
  442.                 //==================================================================
  443.                 for (i = 0; i < gator_count; i++) {
  444.                     gator_time[i] += deltatime;
  445.                     if (gator_time[i] > GATOR_INTERVAL) {
  446.                         gator_time[i] = 0;
  447.                     }
  448.                 }
  449.                
  450.                 //==============================================================
  451.                 // Randomly add ripples
  452.                 //==============================================================
  453.                 i = rnd.nextInt(200);
  454.                 if (i == 0) {
  455.                     i = rnd.nextInt(buoy_count);
  456.                     addRipple(buoy_x[i], buoy_y[i], BUOY_RIPPLE);
  457.                 }
  458.                 else if (i == 1) {
  459.                     i = rnd.nextInt(gator_count);
  460.                     addRipple(gator_x[i], gator_y[i], GATOR_RIPPLE);
  461.                 }
  462.                
  463.                 //==================================================================
  464.                 // Update the Ripple effect if the game isn't paused.
  465.                 //==================================================================
  466.                 updateRippleEffect();  
  467.    
  468.                 //==============================================================
  469.                 // Update to the next level (or reset)
  470.                 //==============================================================
  471.                 game_remaining -= deltatime;
  472.                 if (game_remaining < 0) {
  473.                     if (game_level > 0) {
  474.                         saveScore();
  475.                     }
  476.                     game_reset = true;
  477.                     game_paused = true;
  478.                 }
  479.             }
  480.            
  481.             //==============================================================
  482.             // Update Frames-Per-Second
  483.             //==============================================================
  484. //          frame_time += deltatime;
  485. //          frame_count++;
  486. //          if (frame_time > FRAME_UPDATE_TIME) {
  487. //              frame_rate = frame_count / frame_time;
  488. //              frame_count = 0;
  489. //              frame_time -= FRAME_UPDATE_TIME;
  490. //          }
  491.            
  492.             if (game_reset) {
  493.                 game_lives = 0;
  494.                 game_level = 0;
  495.                 game_money = -(int)game_remaining;
  496.                 blood_count = 0;
  497.                 spear_count = 0;
  498.                 boat_x = WIDTH / 2;
  499.                 boat_y = HEIGHT / 2;
  500.                 boat_velx = 0;
  501.                 boat_vely = 0;
  502.                 mouse_x = WIDTH / 2 + 1;
  503.                 mouse_y = HEIGHT / 2 + 1;
  504.                 game_next = true;
  505.                 game_reset = false;
  506.                 game_paused = true;
  507.             }
  508.             if (game_next) {
  509.                 //=============================================================
  510.                 // PLACE BUOYS
  511.                 //=============================================================
  512.                 int x = 0, y = 0, total = LEVEL_BUOY + (game_level * LEVEL_BUOY_INC);
  513.                 buoy_count = 0;
  514.                 while (--total >= 0) {
  515.                     for (i = 0; i < 100; i++) {
  516.                         x = rnd.nextInt(WIDTH - (BUOY_RADIUS * 2)) + BUOY_RADIUS;
  517.                         y = rnd.nextInt(HEIGHT - (BUOY_RADIUS * 2)) + BUOY_RADIUS;
  518.                         if (canPlace(x, y, BUOY_RADIUS)) {
  519.                             break;
  520.                         }
  521.                     }
  522.                     buoy_x[buoy_count] = x;
  523.                     buoy_y[buoy_count] = y;
  524.                     buoy_health[buoy_count] = BUOY_STRENGTH;
  525.                     buoy_count++;
  526.                 }
  527.  
  528.                 //=============================================================
  529.                 // PLACE GATORS
  530.                 //=============================================================
  531.                 total = LEVEL_GATOR + (game_level * LEVEL_GATOR_INC);
  532.                 gator_count = 0;
  533.                 while (--total >= 0) {
  534.                     for (i = 0; i < 100; i++) {
  535.                         x = rnd.nextInt(WIDTH - (GATOR_RADIUS * 2)) + GATOR_RADIUS;
  536.                         y = rnd.nextInt(HEIGHT - (GATOR_RADIUS * 2)) + GATOR_RADIUS;
  537.                         if (canPlace(x, y, GATOR_RADIUS)) {
  538.                             break;
  539.                         }
  540.                     }
  541.                     gator_x[gator_count] = x;
  542.                     gator_y[gator_count] = y;
  543.                     gator_time[gator_count] = rnd.nextFloat() * GATOR_INTERVAL;
  544.                     gator_count++;
  545.                 }
  546.                
  547.                 game_level++;
  548.                 game_lives++;
  549.                 game_money += (int)game_remaining;
  550.                 game_remaining = game_level * LEVEL_TIME_ADD + LEVEL_TIME_OFFSET;
  551.                 game_next = false;
  552.             }
  553.            
  554.             //==================================================================
  555.             // D R A W I N G
  556.             //==================================================================
  557.             Graphics2D gr = (Graphics2D)buffer.getGraphics();
  558.             // Draw the water effect
  559.             gr.drawImage(water_buffer, 0, 0, null);
  560.             gr.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  561.  
  562.             //==================================================================
  563.             // Draw the Blood Effect
  564.             //==================================================================
  565.             for (i = 0; i < blood_count; i++) {
  566.                 final float t = blood_time[i] / blood_life[i];
  567.                 final int alpha = (int)((1 - t) * 255);
  568.                 final int size = (int)blood_size[i];
  569.                 gr.setColor(new Color(255, 0, 0, alpha));
  570.                 gr.fillOval(blood_x[i] - size, blood_y[i] - size, size * 2, size * 2);
  571.             }
  572.  
  573.             //==================================================================
  574.             // Draw the Buoys
  575.             //==================================================================
  576.             final int FRACTION = BUOY_RADIUS * 4 / 5;
  577.            
  578.             for (i = 0; i < buoy_count; i++) {
  579. //              int pixel_index = buoy_x[i] + (buoy_y[i] * WIDTH);
  580. //              int pixel = (ripple[pixel_index] >> 8) & 0xFF;
  581. //              int left = ((ripple[pixel_index - 1] >> 8) & 0xFF) - pixel;
  582. //              int right = ((ripple[pixel_index + 1] >> 8) & 0xFF) - pixel;
  583. //              int top = ((ripple[pixel_index - WIDTH] >> 8) & 0xFF) - pixel;
  584. //              int bottom = ((ripple[pixel_index + WIDTH] >> 8) & 0xFF) - pixel;
  585. //              int dx = (right - left) >> 4;
  586. //              int dy = (top - bottom) >> 4;
  587.                
  588.                 gr.setColor(BUOY_COLOR[buoy_health[i]]);
  589.                 gr.translate(buoy_x[i], buoy_y[i]);
  590.                 gr.fillOval(-BUOY_RADIUS, -BUOY_RADIUS,
  591.                         BUOY_RADIUS * 2, BUOY_RADIUS * 2);
  592.                 gr.setColor(BUOY_COLOR[buoy_health[i] - 1]);
  593. //              gr.translate(dx, dy);
  594.                 gr.fillOval(-BUOY_RADIUS / 2, -BUOY_RADIUS / 2,
  595.                         BUOY_RADIUS, BUOY_RADIUS);
  596. //              gr.translate(-dx, -dy);
  597.                 gr.drawLine(-FRACTION, -FRACTION, FRACTION, FRACTION);
  598.                 gr.drawLine(FRACTION, -FRACTION, -FRACTION, FRACTION);
  599.                 gr.translate(-buoy_x[i], -buoy_y[i]);
  600.             }
  601.  
  602.             //==================================================================
  603.             // Draw the gators
  604.             //==================================================================
  605.             for (i = 0; i < gator_count; i++) {
  606.                 dir[0] = boat_x - gator_x[i];
  607.                 dir[1] = boat_y - gator_y[i];
  608.                 normalize(dir);
  609.                
  610.                 AffineTransform orig = gr.getTransform();
  611.                 gr.translate(gator_x[i], gator_y[i]);
  612.                 gr.rotate(Math.atan2(dir[1], dir[0]));
  613.                 if (gator_time[i] < GATOR_BLINK) {
  614.                     gr.drawImage(gator_image_open, -GATOR_RADIUS, -GATOR_RADIUS, null);
  615.                 }
  616.                 else {
  617.                     gr.drawImage(gator_image_closed, -GATOR_RADIUS, -GATOR_RADIUS, null);
  618.                 }
  619.                 gr.setTransform(orig);
  620.             }
  621.  
  622.             //==================================================================
  623.             // Draw the boat
  624.             //==================================================================
  625.             dir[0] = mouse_x - boat_x;
  626.             dir[1] = mouse_y - boat_y;
  627.             normalize(dir);
  628.                
  629.             AffineTransform orig = gr.getTransform();
  630.             gr.translate(boat_x, boat_y);
  631.             gr.rotate(Math.atan2(dir[1], dir[0]));
  632.             gr.drawImage(boat_image, -BOAT_RADIUS, -BOAT_RADIUS, null);
  633.             // Top fan blade
  634.             int a = (int)(boat_fan * 255);
  635.             gr.setColor(new Color(a, a, a));
  636.             gr.fillRect(-10, -6, 3, 6);
  637.             // Bottom fan blade
  638.             a = 255 - a;
  639.             gr.setColor(new Color(a, a, a));
  640.             gr.fillRect(-10, 0, 3, 6);
  641.             gr.setTransform(orig);
  642.  
  643.             //==================================================================
  644.             // Draw the spears
  645.             //==================================================================
  646.             gr.setStroke(STROKE_SPEAR);
  647.             gr.setColor(new Color(158, 74, 0));
  648.             for (i = 0; i < spear_count; i++) {
  649.                 gr.drawLine(
  650.                         (int)spear_x[i],
  651.                         (int)spear_y[i],
  652.                         (int)(spear_x[i] + (spear_vel[i][0] * SPEAR_LENGTH)),
  653.                         (int)(spear_y[i] + (spear_vel[i][1] * SPEAR_LENGTH)));
  654.             }
  655.             gr.setStroke(STROKE_SINGLE);
  656.  
  657.             //==================================================================
  658.             // Draw FPS and Game State
  659.             //==================================================================
  660. //          gr.setColor(new Color(128, 128, 128, 128));
  661.             gr.setPaint(new GradientPaint(0, 0, new Color(0, 0, 0, 128), 0, 20, new Color(255, 255, 255, 128)));
  662.             gr.fillRect(0, 0, WIDTH, 20);
  663.             gr.setColor(Color.white);
  664.             gr.setFont(FONT_GAME);
  665. //          gr.drawString(String.format("%.2f", frame_rate), 5, 16);
  666.             gr.drawString(String.format("Level: %d", game_level), 80, 16);
  667.             gr.drawString(String.format("Lives: %d", game_lives), 155, 16);
  668.             gr.drawString(String.format("Time: %.1f", game_remaining), 230, 16);
  669.             gr.drawString(String.format("Money: $%d", game_money), 325, 16);
  670.  
  671.             //==================================================================
  672.             // Draw Top 10 Scores
  673.             //==================================================================
  674.             if (game_paused) { 
  675. //              gr.setColor(new Color(128, 128, 128, 200));
  676.                 gr.setFont(FONT_GAME);
  677.                 gr.setPaint(new GradientPaint(150, 60, new Color(0, 0, 0, 128), 350, 310, new Color(255, 255, 255, 128)));
  678.                 gr.fillRect(150, 40, 200, 270);
  679.                
  680.                 gr.setColor(new Color(255, 255, 255, 200));
  681.                 gr.fillRect(158, 68, 184, 234);
  682.                 gr.setColor(Color.white);
  683.                 gr.drawString("PAUSED", WIDTH / 2 - 25, 60);
  684.                 gr.setColor(Color.black);
  685.                 gr.drawString("Top 10", 230, 80);
  686.                 gr.drawString("Level", 190, 100);
  687.                 gr.drawString("Money", 275, 100);
  688.                 for (i = 0; i < score_count; i++) {
  689.                     gr.drawString(String.format("%5d%12d", score_level[i], scores[i]), 190, 120 + i * 18);
  690.                 }
  691.             }
  692.  
  693.             //==================================================================
  694.             // Draw to the applet
  695.             //==================================================================
  696.             paint(getGraphics());
  697.         }
  698.     }
  699.  
  700.     // Saves the current score to the top 10
  701.     private void saveScore() {
  702.         // Update the score list
  703.         for (j = 0; j < score_count; j++) {
  704.             if (game_money > scores[j]) {
  705.                 break;
  706.             }
  707.         }
  708.         // Can it be added?
  709.         if (j < SCORE_MAX) {
  710.             // Move any scores
  711.             for (k = Math.min(score_count, SCORE_MAX - 1); k > j; k--) {
  712.                 scores[k] = scores[k - 1];
  713.                 score_level[k] = score_level[k - 1];
  714.             }
  715.             // Save score finally
  716.             scores[j] = game_money;
  717.             score_level[j] = game_level;
  718.             // Was it added?
  719.             if (score_count < SCORE_MAX) {
  720.                 score_count++;
  721.             }
  722.         }
  723.     }
  724.    
  725.     // Removes the given gator from the list. Also adds a blood and ripple at
  726.     // the gators location. If there are no more gators then this will make sure
  727.     // the game proceeds to the next level. This also adds to the money counter.
  728.     private void killGator(int i) {
  729.         addBlood(gator_x[i], gator_y[i]);
  730.         addRipple(gator_x[i], gator_y[i], GATOR_RIPPLE);
  731.         gator_count--;
  732.         gator_x[i] = gator_x[gator_count];
  733.         gator_y[i] = gator_y[gator_count];
  734.         gator_time[i] = gator_time[gator_count];
  735.        
  736.         if (gator_count == 0) {
  737.             game_next = true;
  738.         }
  739.        
  740.         game_money += MONEY_GATOR;
  741. //      audioGator.play();
  742.     }
  743.  
  744.     // Removes the given spear from the list.
  745.     private void killSpear(int i) {
  746.         spear_count--;
  747.         spear_x[i] = spear_x[spear_count];
  748.         spear_y[i] = spear_y[spear_count];
  749.         spear_vel[i][0] = spear_vel[spear_count][0];
  750.         spear_vel[i][1] = spear_vel[spear_count][1];
  751.         spear_life[i] = spear_life[spear_count];
  752.     }
  753.  
  754.     // Adds a pool of blood at the given location
  755.     private final void addBlood(int x, int y) {
  756.         int bursts = rnd.nextInt(BLOOD_BURST_MAX - BLOOD_BURST_MIN) + BLOOD_BURST_MIN;
  757.         if (bursts + blood_count >= BLOOD_MAX) {
  758.             bursts = BLOOD_MAX - blood_count - 1;
  759.         }
  760.         while (--bursts >= 0) {
  761.             int dx = rnd.nextInt(GATOR_RADIUS * 2) - GATOR_RADIUS;
  762.             int dy = rnd.nextInt(GATOR_RADIUS * 2) - GATOR_RADIUS;
  763.  
  764.             float life = rnd.nextFloat() * (BLOOD_LIFE_MAX - BLOOD_LIFE_MIN) + BLOOD_LIFE_MIN;
  765.             float size = rnd.nextFloat() * (BLOOD_SIZE_MAX - BLOOD_SIZE_MIN) + BLOOD_SIZE_MIN;
  766.             float size_vel = rnd.nextFloat() * (BLOOD_SIZE_VEL_MAX - BLOOD_SIZE_VEL_MIN) + BLOOD_SIZE_VEL_MIN;
  767.            
  768.             blood_x[blood_count] = x + dx;
  769.             blood_y[blood_count] = y + dy;
  770.             blood_time[blood_count] = 0f;
  771.             blood_life[blood_count] = life;
  772.             blood_size[blood_count] = size;
  773.             blood_size_vel[blood_count] = size_vel;
  774.             blood_count++;
  775.         }
  776.     }
  777.  
  778.     // Adds a ripple at the given point with the given radius
  779.     private void addRipple(int x, int y, int radius) {
  780.         for (j = y - radius; j < y + radius;j++) {
  781.             for (k = x - radius; k < x + radius; k++) {
  782.                 if (j >= 0 && j < HEIGHT && k >= 0 && k < WIDTH) {
  783.                     ripple_map[old_index + (j * WIDTH) + k] += 512;            
  784.                 }
  785.             }
  786.         }
  787.     }
  788.  
  789.     // Updates the ripple effect
  790.     private void updateRippleEffect() {
  791.         int data, map_index;
  792.         // Swap the buffers
  793.         i = old_index;
  794.         old_index = new_index;
  795.         new_index = i;
  796.         // Reset pointers into the image (i) and previous buffer (map_index)
  797.         i = 0;
  798.         map_index = old_index;
  799.         // For every pixel on the ripple image...
  800.         for (j = 0; j < HEIGHT; j++) {
  801.             for (k = 0; k < WIDTH; k++) {
  802.                 // Get the value of the current point based on the previous
  803.                 // ripple map buffer.
  804.                 data = ((ripple_map[map_index - WIDTH] +
  805.                          ripple_map[map_index + WIDTH] +
  806.                          ripple_map[map_index - 1] +
  807.                          ripple_map[map_index + 1]) >> 1);
  808.                
  809.                 // Adjust based on current point and then slowly fade it.
  810.                 data -= ripple_map[new_index + i];
  811.                 data -= data >> 5;
  812.                 // Update the current point
  813.                 ripple_map[new_index + i] = (short)data;
  814.  
  815.                 // Transform data into a color component
  816.                 data = ((1024 - data) >> 4) - 20;
  817.                 if (data > 255) data = 255;
  818.                 if (data < 0) data = 0;
  819.                 // Set the color at this point between white and blue
  820.                 ripple[i++] = 0xFF0000FF | (data << 16) | (data << 8);
  821.                 map_index++;
  822.             }
  823.         }
  824.         // Nofity the image that pixels have been set.
  825.         water_source.newPixels();
  826.     }
  827.  
  828.     // Given a circle can it be placed on the field so it doesn't overlap with
  829.     // the boat, any buoys, and any gators.
  830.     private boolean canPlace(float x, float y, float r) {
  831.         if (intersects(x, y, boat_x, boat_y, r + BOAT_RADIUS)) {
  832.             return false;
  833.         }
  834.         for (k = 0; k < buoy_count; k++) {
  835.             if (intersects(x, y, buoy_x[k], buoy_y[k], r + BUOY_RADIUS)) {
  836.                 return false;
  837.             }
  838.         }
  839.         for (k = 0; k < gator_count; k++) {
  840.             if (intersects(x, y, gator_x[k], gator_y[k], r + GATOR_RADIUS)) {
  841.                 return false;
  842.             }
  843.         }
  844.         return true;
  845.     }
  846.    
  847.     // Returns true if the two circles intersect. dist is the sum of their radii
  848.     private boolean intersects(float cx1, float cy1, float cx2, float cy2, float dist) {
  849.         float dx = cx2 - cx1;
  850.         float dy = cy2 - cy1;
  851.         return (dx * dx + dy * dy <= dist * dist);
  852.     }
  853.    
  854.     // Normalizes the given vector and returns the magnitude of the vector
  855.     private float normalize(float[] vec) {
  856.         float dot = vec[0] * vec[0] + vec[1] * vec[1];
  857.         if (dot != 0f) {
  858.             dot = (float)Math.sqrt(dot);
  859.             vec[0] /= dot;
  860.             vec[1] /= dot;
  861.             return dot;
  862.         }
  863.         return 0f;
  864.     }
  865.  
  866.     // Paints the game
  867.     public void paint(Graphics g) {
  868.         update(g);
  869.     }
  870.    
  871.     // Paints the game
  872.     public void update(Graphics g) {
  873.         if (g != null) {
  874.             g.drawImage(buffer, 0, 0, null);
  875.         }
  876.     }
  877.  
  878.     // Processes all key events
  879.     public void processKeyEvent(KeyEvent e) {
  880.         int code = e.getKeyChar();
  881.         if (e.getID() == KeyEvent.KEY_PRESSED) {
  882.             if (code == 'p') {
  883.                 game_paused = !game_paused;
  884.             }
  885.             else if (code == '\n') {
  886.                 game_reset = true;
  887.             }
  888.         }
  889.     }
  890.  
  891.     // Processes all mouse events
  892.     public void processMouseEvent(MouseEvent e) {
  893.         if (e.getButton() == 1 && e.getID() == MouseEvent.MOUSE_PRESSED) {
  894.             if (game_paused || spear_time < SPEAR_RELOAD || spear_count == SPEAR_MAX) {
  895.                 return;
  896.             }
  897.             spear_time = 0f;
  898.                
  899.             spear_x[spear_count] = boat_x;
  900.             spear_y[spear_count] = boat_y;
  901.             spear_vel[spear_count][0] = mouse_x - boat_x;
  902.             spear_vel[spear_count][1] = mouse_y - boat_y;
  903.             normalize(spear_vel[spear_count]);
  904.             spear_life[spear_count] = SPEAR_LIFE;
  905.             spear_count++;
  906.            
  907.             game_money += MONEY_SPEAR;
  908.         }
  909.     }
  910.  
  911.     // Processes all mouse motion events.
  912.     public void processMouseMotionEvent(MouseEvent e) {
  913.         if (!game_paused) {
  914.             mouse_x = e.getX();
  915.             mouse_y = e.getY();
  916.         }
  917.     }
  918.  
  919. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement