Advertisement
Itsme301

Shooter game UI not working

Aug 5th, 2023
1,512
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 26.63 KB | Help | 0 0
  1. //UI variables
  2. ArrayList<DamageIndicator> damageIndicators = new ArrayList<>();
  3. float damageIndicatorPadding = 5; // Padding between damage indicators
  4. PFont customFont; // Replace with your custom font variable
  5. int customFontSize = 15; // Replace with your custom font size
  6. boolean showMainUI = false;
  7. boolean showOptionsUI = false;
  8. boolean isUIVisible = false;
  9. int buttonPadding = 15;
  10.  
  11. // PowerUps variables
  12. boolean isPlayerPoweredUp = false;
  13. int powerupStartTime = 0;
  14. int powerupDuration = 5; // Power-up duration in seconds
  15. float originalPlayerSize; // Store the original player size for later use
  16. color originalPlayerColor; // Store the original player color for later use
  17. int originalMaxTriangleShotDamage; // Store the original maxTriangleShotDamage for later use
  18. int originalMaxSingleTriangleShotDamage; // Store the original maxSingleTriangleShotDamage for later use
  19. boolean isPurpleSquareVisible = false;
  20. float purpleSquareX, purpleSquareY;
  21. float purpleSquareSize = 20;
  22.  
  23. // Player variables
  24. int playerX, playerY;
  25. float playerSpeed = 5;
  26. float playerSize = 30;
  27. float maxPlayerHealth = 100000;
  28. float playerHealth = maxPlayerHealth;
  29. boolean[] isMoving = new boolean[4]; // 0: W, 1: A, 2: S, 3: D
  30. boolean isPlayerAlive = true;
  31. int healthIncreaseInterval = 2000; // 2 seconds interval for health increase
  32. int lastHealthIncreaseTime = 0; // Time when health was last increased
  33.  
  34. // Attack variables
  35. final int TRIANGLE_SHOT = 0;
  36. final int SINGLE_TRIANGLE_SHOT = 1;
  37. int currentAttackMode = SINGLE_TRIANGLE_SHOT;
  38. final int SINGLE_TRIANGLE_SHOT_AMMO = 1;
  39. final int TRIANGLE_SHOT_AMMO = 3;
  40. int maxTriangleShotDamage = 750;
  41. int maxSingleTriangleShotDamage = 1200;
  42.  
  43. // Bullets variables
  44. ArrayList<PoisonBullet> bullets = new ArrayList<>();
  45. final int bulletDamage = 1250;
  46. int maxAmmoCount = 3;
  47. int ammoCount = maxAmmoCount;
  48. int baseAmmoRespawnInterval = 1000;
  49. int ammoRespawnInterval = 100;
  50. int lastAmmoRespawnTime = 0;
  51. int lastRefilledAmmoIndex = -3;
  52. int ammoRefillTime = 0;
  53. float bulletSpreadAngle = radians(10);
  54. float leftBulletOffset = -bulletSpreadAngle;
  55. float rightBulletOffset = bulletSpreadAngle;
  56.  
  57. // Enemy square variables
  58. float enemyX, enemyY;
  59. float enemySize = 40;
  60. float enemySpeed = 3;
  61. float maxEnemyHealth = 1;
  62. float enemyHealth = maxEnemyHealth;
  63. boolean isEnemyVisible = true;
  64.  
  65. // Triangle enemy variables
  66. float triangleEnemyX;
  67. float triangleEnemyY;
  68. float triangleEnemySize = 40; // Adjust the size as needed
  69. int maxTriangleEnemyHealth = 5000; // Adjust the max health as needed
  70. int triangleEnemyHealth = maxTriangleEnemyHealth;
  71. boolean isTriangleEnemyVisible = true;
  72.  
  73. // Universal enemy variables
  74. boolean isEnemyDefeated = false;
  75. int defeatTimer = 0;
  76. final int defeatDuration = 1000; // 1 second in milliseconds
  77. int maxEnemies = 1; // Change this value to set the maximum number of enemies
  78. ArrayList<Enemy> enemies = new ArrayList<>();
  79.  
  80. class Enemy {
  81.   float x, y;
  82.   float size = 40; // Adjust the size as needed
  83.   float speed = 3;
  84.   float maxHealth = 10000;
  85.   float health = maxHealth;
  86.   boolean isDefeated = false;
  87.   boolean isVisible = true;
  88.   int defeatTimer = 0;
  89.  
  90.   Enemy(float x, float y) {
  91.     this.x = x;
  92.     this.y = y;
  93.   }
  94.  
  95.   void drawEnemy() {
  96.     if (health > 0) {
  97.       fill(255, 0, 0); // Red color for enemy
  98.       ellipse(x + (size / 2), y + (size / 2), size, size);
  99.     } else if (isDefeated) {
  100.       // Shrink the enemy gradually until it disappears after defeat
  101.       if (size > 0) {
  102.         size -= 2; // Adjust the shrinking speed as needed
  103.       } else {
  104.         // Enemy is destroyed, remove it from the list of enemies
  105.         isVisible = false;
  106.         enemies.remove(this);
  107.       }
  108.     }
  109.  
  110.     // Draw health bar for visible enemies
  111.     if (isVisible) {
  112.       fill(100); // Gray color for health bar background
  113.       rect(x, y - 20, size, 10);
  114.  
  115.       // Draw enemy health bar
  116.       fill(255, 0, 0); // Red color for health bar
  117.       float healthBarWidth = map(health, 0, maxHealth, 0, size);
  118.       rect(x, y - 20, healthBarWidth, 10);
  119.  
  120.       // Display current enemy health amount above the health bar
  121.       fill(0); // Black color for text
  122.       textAlign(CENTER, CENTER); // Align the text to the bottom of the bounding box
  123.       textSize(customFontSize); // Set the font size
  124.       text((int) health, x + size / 2, y - 20 - healthBarPadding); // Display health as an integer
  125.     }
  126.   }
  127. }
  128.  
  129. // DamageIndicator class
  130. class DamageIndicator {
  131.   float x, y;
  132.   String text;
  133.   int lifeSpan = 60; // The number of frames the indicator will be visible
  134.   int frameCount = 0;
  135.   PFont customFont; // Custom font variable
  136.   int customFontSize; // Custom font size variable
  137.  
  138.   DamageIndicator(float x, float y, String damage, PFont font, int fontSize) {
  139.     this.x = x;
  140.     this.y = y;
  141.     this.text = damage;
  142.     this.customFont = font;
  143.     this.customFontSize = fontSize;
  144.   }
  145.  
  146.   void update() {
  147.     y -= 1; // Move the indicator upwards to make it float
  148.     frameCount++;
  149.   }
  150.  
  151.   void setY(float y) {
  152.     this.y = y;
  153.   }
  154.  
  155.   void display() {
  156.     fill(255, 0, 0); // Red color for damage indicator text
  157.  
  158.     // Set custom font and size
  159.     textFont(customFont);
  160.     textSize(customFontSize);
  161.  
  162.     text(text, x, y);
  163.   }
  164.  
  165.   boolean isFinished() {
  166.     return frameCount >= lifeSpan;
  167.   }
  168. }
  169.  
  170. void updateEnemies() {
  171.   for (int i = enemies.size() - 1; i >= 0; i--) {
  172.     Enemy enemy = enemies.get(i);
  173.     if (enemy.isVisible) {
  174.       if (isEnemyVisible) {
  175.         float targetX = playerX + (playerSize / 2);
  176.         float targetY = playerY + (playerSize / 2);
  177.         float dx = targetX - (enemy.x + (enemy.size / 2));
  178.         float dy = targetY - (enemy.y + (enemy.size / 2));
  179.         float distance = sqrt(dx * dx + dy * dy);
  180.         if (distance > enemy.speed) {
  181.           float angle = atan2(dy, dx);
  182.           float vx = cos(angle) * enemy.speed;
  183.           float vy = sin(angle) * enemy.speed;
  184.           enemy.x += vx;
  185.           enemy.y += vy;
  186.         } else {
  187.           enemy.x = targetX - (enemy.size / 2);
  188.           enemy.y = targetY - (enemy.size / 2);
  189.         }
  190.       }
  191.  
  192.       // Check collisions between enemy and the player
  193.       float dx = enemy.x - (playerX + (playerSize / 2));
  194.       float dy = enemy.y - (playerY + (playerSize / 2));
  195.       float distance = sqrt(dx * dx + dy * dy);
  196.       if (distance < enemy.size / 2 + playerSize / 2) {
  197.         if (millis() - lastAmmoRespawnTime > 1000) {
  198.           playerHealth -= 1004; // Decrease player health when collided with the enemy
  199.           lastAmmoRespawnTime = millis();
  200.         }
  201.         if (playerHealth <= 0) {
  202.           playerHealth = 0;
  203.           isPlayerAlive = false;
  204.           // Handle player destroyed event (e.g., game over)
  205.         }
  206.       }
  207.       // Check collisions between enemy and the triangle bullets
  208.       for (int j = bullets.size() - 1; j >= 0; j--) {
  209.         PoisonBullet bullet = bullets.get(j);
  210.         if (bullet.hitsEnemy(enemy.x, enemy.y, enemy.size)) {
  211.           bullets.remove(j);
  212.           enemy.health -= bullet.damage; // Decrease enemy health when hit by a bullet
  213.           if (enemy.health <= 0) {
  214.             enemy.health = 0;
  215.             enemy.isDefeated = true; // The enemy is defeated
  216.             enemy.defeatTimer = millis();
  217.           }
  218.  
  219.           // Add a new damage indicator at the enemy's position
  220.           String damageText = Integer.toString(bullet.damage);
  221.           damageIndicators.add(new DamageIndicator(enemy.x + enemy.size / 2, enemy.y, damageText, customFont, customFontSize));
  222.         }
  223.       }
  224.     } else if (enemy.isDefeated) {
  225.       // If the enemy is defeated, check if the defeatDuration has passed to remove it completely
  226.       if (millis() - enemy.defeatTimer > defeatDuration) {
  227.         enemies.remove(i);
  228.         break; // Exit the loop to prevent concurrent modification error
  229.       }
  230.     }
  231.   }
  232. }
  233.  
  234. void setup() {
  235.   for (int i = 0; i < maxEnemies; i++) {
  236.     spawnEnemy();
  237.   }
  238.   size(400, 300);
  239.   playerX = width / 2;
  240.   playerY = height / 2;
  241.   enemyX = random(width - enemySize);
  242.   enemyY = random(height - enemySize);
  243.   triangleEnemyX = random(width - triangleEnemySize);
  244.   triangleEnemyY = random(height - triangleEnemySize);
  245.  
  246.   // Load your custom font in the setup (if you have one, replace "YourCustomFont.ttf" with the actual font file)
  247.   // Place the font file in the "data" folder of your Processing sketch
  248.   customFont = createFont("Nougat-ExtraBlack.ttf", customFontSize); // Replace "YourCustomFont.ttf" with the actual font file name
  249. }
  250.  
  251. void draw() {
  252.   if (isUIVisible) {
  253.     if (showMainUI) {
  254.       drawMainUI();
  255.     } else if (showOptionsUI) {
  256.       drawOptionsUI();
  257.     }
  258.   }
  259.   updateEnemies();
  260.   for (Enemy enemy : enemies) {
  261.     if (enemy.isVisible) {
  262.       enemy.drawEnemy();
  263.     }
  264.   }
  265.  
  266.     if (enemies.size() < maxEnemies && random(100) < 1) {
  267.     spawnEnemy();
  268.   }
  269.   if (enemyHealth <= 0 && !isEnemyDefeated) {
  270.     isEnemyDefeated = true;
  271.     defeatTimer = millis();
  272.   }
  273.   background(255);
  274.   // Draw damage indicators
  275.   for (int i = damageIndicators.size() - 1; i >= 0; i--) {
  276.     DamageIndicator indicator = damageIndicators.get(i);
  277.     indicator.update();
  278.     indicator.display();
  279.     if (indicator.isFinished()) {
  280.       damageIndicators.remove(i);
  281.     }
  282.   }
  283.  
  284.   // Update and draw player if the player is alive
  285.   if (isPlayerAlive) {
  286.     movePlayer();
  287.     updateAmmoCount();
  288.     checkCollisions();
  289.     drawPlayer();
  290.     drawBullets();
  291.     drawAmmoCount();
  292.     drawPlayerHealthBar();
  293.   }
  294.  
  295.   // Update and draw enemy if visible
  296.   for (Enemy enemy : enemies) {
  297.     if (enemy.isVisible) {
  298.       enemy.drawEnemy();
  299.     }
  300.   }
  301.  
  302.  
  303.  // Randomly generate the purple square once
  304.   if (!isPurpleSquareVisible && !isPlayerPoweredUp && random(100) <= 0.5) {
  305.     purpleSquareX = random(width - purpleSquareSize);
  306.     purpleSquareY = random(height - purpleSquareSize);
  307.     isPurpleSquareVisible = true;
  308.   }
  309.  
  310.   // Draw the purple square if it's visible
  311.   if (isPurpleSquareVisible) {
  312.     fill(150, 0, 150); // Purple color for the square
  313.     rect(purpleSquareX, purpleSquareY, purpleSquareSize, purpleSquareSize);
  314.  
  315.     // Check collision between the player and the purple square
  316.     if (isPlayerCollidingWithPurpleSquare()) {
  317.       // Remove the purple square
  318.       isPurpleSquareVisible = false;
  319.  
  320.       // Do something when the player collides with the square (e.g., activate power-up)
  321.       isPlayerPoweredUp = true;
  322.       powerupStartTime = millis();
  323.  
  324.       // Store the original player size, color, and damage values for later use
  325.       originalPlayerSize = playerSize;
  326.       originalPlayerColor = get(playerX, playerY); // Store the original fill color
  327.       originalMaxTriangleShotDamage = maxTriangleShotDamage;
  328.       originalMaxSingleTriangleShotDamage = maxSingleTriangleShotDamage;
  329.  
  330.       // Increase player size
  331.       playerSize *= 1.5;
  332.  
  333.       // Change player color to purple
  334.       fill(150, 0, 150); // Purple color
  335.  
  336.       // Multiply the maxTriangleShotDamage and maxSingleTriangleShotDamage by two
  337.       maxTriangleShotDamage *= 2;
  338.       maxSingleTriangleShotDamage *= 2;
  339.     }
  340.   }
  341.  
  342.   // Check if the power-up has ended
  343.   if (isPlayerPoweredUp && millis() >= powerupStartTime + powerupDuration * 1000) {
  344.     // Deactivate the power-up
  345.     isPlayerPoweredUp = false;
  346.    
  347.    // Restore the original player size, color, and damage values
  348.     playerSize = originalPlayerSize;
  349.     fill(originalPlayerColor); // Restore the original fill color
  350.     maxTriangleShotDamage = originalMaxTriangleShotDamage;
  351.     maxSingleTriangleShotDamage = originalMaxSingleTriangleShotDamage;
  352.   }
  353. }
  354.  
  355. void drawOptionsUI() {
  356.   int boxWidth = 250; // Increased box width
  357.   int boxHeight = 150; // Increased box height
  358.   int boxX = width / 2 - boxWidth / 2;
  359.   int boxY = height / 2 - boxHeight / 2;
  360.  
  361.   fill(255);
  362.   stroke(0); // Outline color
  363.   rect(boxX, boxY, boxWidth, boxHeight, 8); // Rounded rectangle
  364.  
  365.   fill(0);
  366.   textAlign(CENTER, CENTER);
  367.   textSize(24);
  368.   text("Options", width / 2, boxY + 30); // Title
  369.  
  370.   fill(150);
  371.   rect(boxX + 10, boxY + 10, 30, 30); // Back button
  372.  
  373.   fill(0);
  374.   textSize(18);
  375.   text("<<", boxX + 25, boxY + 25);
  376. }
  377.  
  378.   void drawMainUI() {
  379.   int boxWidth = 250; // Increased box width
  380.   int boxHeight = 150; // Increased box height
  381.   int boxX = width / 2 - boxWidth / 2;
  382.   int boxY = height / 2 - boxHeight / 2;
  383.  
  384.   fill(255);
  385.   stroke(0); // Outline color
  386.   rect(boxX, boxY, boxWidth, boxHeight, 8); // Rounded rectangle
  387.  
  388.   fill(0);
  389.   textAlign(CENTER, CENTER);
  390.   textSize(24);
  391.   text("Game Paused", width / 2, boxY + 30); // Title
  392.  
  393.   textSize(18);
  394.  
  395.   // Draw hitbox for Button 1
  396.   int button1X = boxX + boxWidth / 2 - 50;
  397.   int button1Y = boxY + 60;
  398.   fill(150);
  399.   rect(button1X, button1Y, 100, 30);
  400.   fill(0);
  401.   text("Continue", boxX + boxWidth / 2, button1Y + 15);
  402.  
  403.   // Draw hitbox for Button 2
  404.   int button2X = boxX + boxWidth / 2 - 50;
  405.   int button2Y = boxY + 60 + buttonPadding + 30;
  406.   fill(150);
  407.   rect(button2X, button2Y, 100, 30);
  408.   fill(0);
  409.   text("Options", boxX + boxWidth / 2, button2Y + 15);
  410.   }
  411. void spawnEnemy() {
  412.   float enemySize = 40; // Adjust the size as needed
  413.   float enemyX = random(width - enemySize);
  414.   float enemyY = random(height - enemySize);
  415.   enemies.add(new Enemy(enemyX, enemyY));
  416. }
  417.  
  418. void drawTriangleEnemy() {
  419.   if (isTriangleEnemyVisible && triangleEnemyHealth <= 0 && !isEnemyDefeated) {
  420.     isEnemyDefeated = true;
  421.     defeatTimer = millis();
  422.   }
  423.  
  424.   // Hide the triangle enemy if defeated
  425.   if (isTriangleEnemyVisible && millis() - defeatTimer > defeatDuration) {
  426.     isTriangleEnemyVisible = false;
  427.   }
  428.   if (isTriangleEnemyVisible && triangleEnemyHealth > 0) {
  429.     fill(255, 165, 0); // Orange color for triangle enemy
  430.     triangle(triangleEnemyX, triangleEnemyY + triangleEnemySize,
  431.              triangleEnemyX + triangleEnemySize / 2, triangleEnemyY,
  432.              triangleEnemyX + triangleEnemySize, triangleEnemyY + triangleEnemySize);
  433.   } else {
  434.     // Shrink the triangle enemy gradually until it disappears
  435.     if (triangleEnemySize > 0) {
  436.       triangleEnemySize -= 2; // Adjust the shrinking speed as needed
  437.     } else {
  438.       // Triangle enemy is destroyed, stop rendering it
  439.       isTriangleEnemyVisible = false;
  440.     }
  441.   }
  442.  
  443.   // Draw health bar for the triangle enemy
  444.   fill(100); // Gray color for health bar background
  445.   rect(triangleEnemyX, triangleEnemyY - 20, triangleEnemySize, 10);
  446.  
  447.   // Draw triangle enemy health bar
  448.   fill(255, 0, 0); // Red color for health bar
  449.   float triangleEnemyHealthBarWidth = map(triangleEnemyHealth, 0, maxTriangleEnemyHealth, 0, triangleEnemySize);
  450.   rect(triangleEnemyX, triangleEnemyY - 20, triangleEnemyHealthBarWidth, 10);
  451.  
  452.   // Display current triangle enemy health amount above the health bar
  453.   fill(0); // Black color for text
  454.   textAlign(CENTER, CENTER); // Align the text to the bottom of the bounding box
  455.   textSize(customFontSize); // Set the font size
  456.   text((int)triangleEnemyHealth, triangleEnemyX + triangleEnemySize / 2, triangleEnemyY - 20 - healthBarPadding); // Display health as an integer
  457. }
  458.  
  459. // Function to check collision between the player and the purple square
  460. boolean isPlayerCollidingWithPurpleSquare() {
  461.   return (playerX + playerSize / 2 > purpleSquareX &&
  462.           playerX - playerSize / 2 < purpleSquareX + purpleSquareSize &&
  463.           playerY + playerSize / 2 > purpleSquareY &&
  464.           playerY - playerSize / 2 < purpleSquareY + purpleSquareSize);
  465. }
  466.  
  467. // Player functions
  468. void movePlayer() {
  469.   if (isMoving[0]) playerY -= playerSpeed;
  470.   if (isMoving[1]) playerX -= playerSpeed;
  471.   if (isMoving[2]) playerY += playerSpeed;
  472.   if (isMoving[3]) playerX += playerSpeed;
  473. }
  474.  
  475. // Add a new function to draw the player
  476. void drawPlayer() {
  477.   if (isPlayerAlive) {
  478.     // Check if the player is not moving and not attacking
  479.     boolean isPlayerIdle = !isMoving[0] && !isMoving[1] && !isMoving[2] && !isMoving[3] && millis() - lastAmmoRespawnTime > healthIncreaseInterval;
  480.  
  481.     // Increase player health if the player is idle and hasn't reached the maximum health
  482.     if (isPlayerIdle && playerHealth < maxPlayerHealth) {
  483.       playerHealth += 5; // Increase the health by 5 (you can adjust this value as needed)
  484.       if (playerHealth > maxPlayerHealth) {
  485.         playerHealth = maxPlayerHealth;
  486.       }
  487.       lastHealthIncreaseTime = millis(); // Update the time when health was last increased
  488.     }
  489.  
  490.     // Draw the player as a colored ellipse
  491.     if (isPlayerPoweredUp) {
  492.       fill(150, 0, 150); // Purple color if the player is powered up
  493.     } else {
  494.       fill(0, 255, 0); // Green color if the player is not powered up
  495.     }
  496.     ellipse(playerX, playerY, playerSize, playerSize);
  497.   } else {
  498.     // Player health is 0 or below, make the player object disappear
  499.     return;
  500.   }
  501. }
  502.  
  503. void checkCollisions() {
  504.   // Check collisions between bullets and the enemy base
  505.   for (int i = bullets.size() - 1; i >= 0; i--) {
  506.     PoisonBullet bullet = bullets.get(i);
  507.     if (bullet.hitsEnemy(enemyX, enemyY, enemySize)) {
  508.       bullets.remove(i);
  509.       enemyHealth -= bullet.damage; // Decrease enemy health when hit by a bullet
  510.       if (enemyHealth <= 0) {
  511.         enemyHealth = 0;
  512.         isEnemyVisible = false; // The enemy is destroyed
  513.       }
  514.  
  515.       // Add a new damage indicator at the enemy's position
  516.       String damageText = Integer.toString(bullet.damage);
  517.       damageIndicators.add(new DamageIndicator(enemyX + enemySize / 2, enemyY, damageText, customFont, customFontSize));
  518.     }
  519.   } // Check collisions between bullets and the triangle enemy
  520.   for (int i = bullets.size() - 1; i >= 0; i--) {
  521.     PoisonBullet bullet = bullets.get(i);
  522.     if (bullet.hitsEnemy(triangleEnemyX, triangleEnemyY, triangleEnemySize)) {
  523.       bullets.remove(i);
  524.       triangleEnemyHealth -= bullet.damage; // Decrease triangle enemy health when hit by a bullet
  525.       if (triangleEnemyHealth <= 0) {
  526.         triangleEnemyHealth = 0;
  527.         isTriangleEnemyVisible = false; // The triangle enemy is destroyed
  528.       }
  529.  
  530.       // Add a new damage indicator at the triangle enemy's position
  531.       String damageText = Integer.toString(bullet.damage);
  532.       damageIndicators.add(new DamageIndicator(triangleEnemyX + triangleEnemySize / 2, triangleEnemyY, damageText, customFont, customFontSize));
  533.     }
  534.   }
  535.  
  536.   // Check collisions between enemy and the player
  537.   float dx = enemyX - (playerX + (playerSize / 2));
  538.   float dy = enemyY - (playerY + (playerSize / 2));
  539.   float distance = sqrt(dx * dx + dy * dy);
  540.   if (distance < enemySize / 2 + playerSize / 2) {
  541.     if (millis() - lastAmmoRespawnTime > 1000) {
  542.       playerHealth -= 1004; // Decrease player health when collided with the enemy
  543.       lastAmmoRespawnTime = millis();
  544.     }
  545.     if (playerHealth <= 0) {
  546.       playerHealth = 0;
  547.       isPlayerAlive = false;
  548.       // Handle player destroyed event (e.g., game over)
  549.     }
  550.   }
  551.  
  552.   // Check collisions between triangle enemy and the player
  553.   dx = triangleEnemyX - (playerX + (playerSize / 2));
  554.   dy = triangleEnemyY - (playerY + (playerSize / 2));
  555.   distance = sqrt(dx * dx + dy * dy);
  556.   if (distance < triangleEnemySize / 2 + playerSize / 2) {
  557.     if (millis() - lastAmmoRespawnTime > 1000) {
  558.       playerHealth -= 1004; // Decrease player health when collided with the triangle enemy
  559.       lastAmmoRespawnTime = millis();
  560.     }
  561.     if (playerHealth <= 0) {
  562.       playerHealth = 0;
  563.       isPlayerAlive = false;
  564.       // Handle player destroyed event (e.g., game over)
  565.     }
  566.   }
  567. }
  568.  
  569. void drawBullets() {
  570.   for (int i = bullets.size() - 1; i >= 0; i--) {
  571.     PoisonBullet bullet = bullets.get(i);
  572.     bullet.update();
  573.     bullet.display();
  574.   }
  575. }
  576.  
  577. // Ammo functions
  578. void drawAmmoCount() {
  579.   if (isPlayerAlive) {
  580.     float squareWidth = 15;
  581.     float squareHeight = 10;
  582.     float padding = 2;
  583.     float startX = playerX - ((maxAmmoCount * squareWidth) + (padding * (maxAmmoCount - 1))) / 2;
  584.     float startY = playerY - playerSize / 2 - padding - squareHeight;
  585.  
  586.     for (int i = 0; i < maxAmmoCount; i++) {
  587.       float x = startX + i * (squareWidth + padding);
  588.       float width = squareWidth;
  589.       float height = squareHeight;
  590.  
  591.       if (i < ammoCount) {
  592.         // Draw a filled orange rectangle for available ammo squares
  593.         fill(255, 165, 0); // Orange color for ammo squares
  594.       } else {
  595.         // Draw empty gray rectangles for the rest of the ammo squares
  596.         fill(200); // Gray color for empty ammo squares
  597.       }
  598.       rect(x, startY, width, height);
  599.     }
  600.   }
  601. }
  602.  
  603. void updateAmmoCount() {
  604.   // Calculate the respawn interval based on the current ammo count
  605.   int ammoRespawnInterval = baseAmmoRespawnInterval + (maxAmmoCount - ammoCount) * 500;
  606.  
  607.   if (ammoCount < maxAmmoCount && millis() - lastAmmoRespawnTime > ammoRespawnInterval) {
  608.     ammoCount++;
  609.     lastRefilledAmmoIndex = (lastRefilledAmmoIndex + 1) % maxAmmoCount; // Update the last refilled ammo index
  610.     lastAmmoRespawnTime = millis(); // Update the refill start time
  611.   }
  612. }
  613.  
  614. int healthBarPadding = 13; // Padding between health bar and text
  615.  
  616. void drawPlayerHealthBar() {
  617.   if (isPlayerAlive) {
  618.     float healthBarWidth = map(playerHealth, 0, maxPlayerHealth, 0, playerSize);
  619.     float healthBarHeight = 10;
  620.  
  621.     // Adjust the position of the health bar above the player
  622.     float healthBarX = playerX - playerSize / 2;
  623.     float healthBarY = playerY - playerSize / 2 - healthBarHeight - healthBarPadding;
  624.  
  625.     fill(100); // Gray color for health bar background
  626.     rect(healthBarX, healthBarY, playerSize, healthBarHeight);
  627.  
  628.     fill(0, 255, 0); // Green color for health bar
  629.     rect(healthBarX, healthBarY, healthBarWidth, healthBarHeight);
  630.  
  631.     // Display current player health amount above the health bar
  632.     fill(0); // Black color for text
  633.     textAlign(CENTER, CENTER); // Align the text to the bottom of the bounding box
  634.     textSize(customFontSize); // Set the font size
  635.     text((int)playerHealth, playerX, healthBarY - healthBarPadding); // Display health as an integer
  636.   }
  637. }
  638.  
  639. void mouseClicked() {
  640.   if (showMainUI) {
  641.     int boxWidth = 250; // Increased box width
  642.     int boxHeight = 150; // Increased box height
  643.     int boxX = width / 2 - boxWidth / 2;
  644.     int boxY = height / 2 - boxHeight / 2;
  645.    
  646.     // Check if Button 1 is clicked
  647.     int button1X = boxX + boxWidth / 2 - 50;
  648.     int button1Y = boxY + 60;
  649.     if (mouseX > button1X && mouseX < button1X + 100 &&
  650.         mouseY > button1Y && mouseY < button1Y + 30) {
  651.       // Button 1 clicked
  652.       showMainUI = false;
  653.     }
  654.    
  655.     // Check if Button 2 is clicked
  656.     int button2X = boxX + boxWidth / 2 - 50;
  657.     int button2Y = boxY + 60 + buttonPadding + 30;
  658.     if (mouseX > button2X && mouseX < button2X + 100 &&
  659.         mouseY > button2Y && mouseY < button2Y + 30) {
  660.       // Button 2 clicked
  661.       showMainUI = false;
  662.       showOptionsUI = true;
  663.     }
  664.   } else if (showOptionsUI) {
  665.     int boxWidth = 250; // Increased box width
  666.     int boxHeight = 150; // Increased box height
  667.     int boxX = width / 2 - boxWidth / 2;
  668.     int boxY = height / 2 - boxHeight / 2;
  669.    
  670.     if (mouseX > boxX + 10 && mouseX < boxX + 40 &&
  671.         mouseY > boxY + 10 && mouseY < boxY + 40) {
  672.       showOptionsUI = false;
  673.       showMainUI = true;
  674.     }
  675.   }
  676. }
  677.  
  678. void keyPressed() {
  679.   if (key == 'w' || key == 'W') isMoving[0] = true;
  680.   else if (key == 'a' || key == 'A') isMoving[1] = true;
  681.   else if (key == 's' || key == 'S') isMoving[2] = true;
  682.   else if (key == 'd' || key == 'D') isMoving[3] = true;
  683.   else if (key == 'c' || key == 'C') {
  684.     currentAttackMode = (currentAttackMode + 1) % 2;
  685.   }
  686.   if (key == TAB) {
  687.     isUIVisible = !isUIVisible;
  688.     if (isUIVisible) {
  689.       // Reset the UI states when it becomes visible
  690.       showMainUI = false;
  691.       showOptionsUI = false;
  692.     }
  693.   }
  694. }
  695.  
  696. // Function to handle key releases
  697. void keyReleased() {
  698.   if (key == 'w' || key == 'W') isMoving[0] = false;
  699.   else if (key == 'a' || key == 'A') isMoving[1] = false;
  700.   else if (key == 's' || key == 'S') isMoving[2] = false;
  701.   else if (key == 'd' || key == 'D') isMoving[3] = false;
  702. }
  703.  
  704. // Declare angle, lastMouseX, and lastMouseY as global variables
  705. float angle;
  706. float lastMouseX;
  707. float lastMouseY;
  708.  
  709. void mousePressed() {
  710.   if (isPlayerAlive && millis() - lastAmmoRespawnTime > ammoRespawnInterval) {
  711.     // Calculate the angle based on the mouse position
  712.     angle = atan2(mouseY - playerY, mouseX - playerX);
  713.  
  714.     // Store the last mouse position
  715.     lastMouseX = mouseX;
  716.     lastMouseY = mouseY;
  717.  
  718.     // Determine the attack mode and add bullets accordingly
  719.     switch (currentAttackMode) {
  720.       case TRIANGLE_SHOT:
  721.         if (ammoCount >= TRIANGLE_SHOT_AMMO) {
  722.           // Create the central bullet
  723.           bullets.add(new PoisonBullet(playerX, playerY, angle, maxTriangleShotDamage));
  724.  
  725.           // Create the left bullet
  726.           float leftBulletAngle = angle + leftBulletOffset;
  727.           bullets.add(new PoisonBullet(playerX, playerY, leftBulletAngle, maxTriangleShotDamage));
  728.  
  729.           // Create the right bullet
  730.           float rightBulletAngle = angle + rightBulletOffset;
  731.           bullets.add(new PoisonBullet(playerX, playerY, rightBulletAngle, maxTriangleShotDamage));
  732.  
  733.           ammoCount -= TRIANGLE_SHOT_AMMO;
  734.           lastAmmoRespawnTime = millis();
  735.         }
  736.         break;
  737.  
  738.       case SINGLE_TRIANGLE_SHOT:
  739.         if (ammoCount >= SINGLE_TRIANGLE_SHOT_AMMO) {
  740.           // Create a single triangle bullet
  741.           bullets.add(new PoisonBullet(playerX, playerY, angle, maxSingleTriangleShotDamage));
  742.  
  743.           ammoCount -= SINGLE_TRIANGLE_SHOT_AMMO;
  744.           lastAmmoRespawnTime = millis();
  745.         }
  746.         break;
  747.     }
  748.   }
  749. }
  750.  
  751. // PoisonBullet class
  752. class PoisonBullet {
  753.   float x, y;
  754.   float speed = 10;
  755.   float angle;
  756.  
  757.   int damage; // Damage value for the bullet
  758.  
  759.   PoisonBullet(float x, float y, float angle) {
  760.     this.x = x;
  761.     this.y = y;
  762.     this.angle = angle;
  763.     this.damage = 5000; // Default damage for the triangle shot
  764.   }
  765.  
  766.   PoisonBullet(float x, float y, float angle, int damage) {
  767.     this.x = x;
  768.     this.y = y;
  769.     this.angle = angle;
  770.     this.damage = damage;
  771.   }
  772.  
  773.   void update() {
  774.     x += cos(angle) * speed;
  775.     y += sin(angle) * speed;
  776.   }
  777.  
  778.   void display() {
  779.     fill(0, 90, 255); // Blue color for bullets
  780.     pushMatrix();
  781.     translate(x, y);
  782.     rotate(angle - HALF_PI);
  783.     triangle(0, 0, -5, -15, 5, -15);
  784.     popMatrix();
  785.   }
  786.  
  787.   boolean isOffScreen() {
  788.     return (x < 0 || x > width || y < 0 || y > height);
  789.   }
  790.  
  791.  boolean hitsEnemy(float enemyX, float enemyY, float enemySize) {
  792.   float dx = enemyX + enemySize / 2 - x;
  793.   float dy = enemyY + enemySize / 2 - y;
  794.   float distance = sqrt(dx * dx + dy * dy);
  795.   return distance < enemySize / 2 + 15 / 2; // 15 is the width of the bullet triangle
  796. }
  797. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement