Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //UI variables
- ArrayList<DamageIndicator> damageIndicators = new ArrayList<>();
- float damageIndicatorPadding = 5; // Padding between damage indicators
- PFont customFont; // Replace with your custom font variable
- int customFontSize = 15; // Replace with your custom font size
- boolean showMainUI = false;
- boolean showOptionsUI = false;
- boolean isUIVisible = false;
- int buttonPadding = 15;
- // PowerUps variables
- boolean isPlayerPoweredUp = false;
- int powerupStartTime = 0;
- int powerupDuration = 5; // Power-up duration in seconds
- float originalPlayerSize; // Store the original player size for later use
- color originalPlayerColor; // Store the original player color for later use
- int originalMaxTriangleShotDamage; // Store the original maxTriangleShotDamage for later use
- int originalMaxSingleTriangleShotDamage; // Store the original maxSingleTriangleShotDamage for later use
- boolean isPurpleSquareVisible = false;
- float purpleSquareX, purpleSquareY;
- float purpleSquareSize = 20;
- // Player variables
- int playerX, playerY;
- float playerSpeed = 5;
- float playerSize = 30;
- float maxPlayerHealth = 100000;
- float playerHealth = maxPlayerHealth;
- boolean[] isMoving = new boolean[4]; // 0: W, 1: A, 2: S, 3: D
- boolean isPlayerAlive = true;
- int healthIncreaseInterval = 2000; // 2 seconds interval for health increase
- int lastHealthIncreaseTime = 0; // Time when health was last increased
- // Attack variables
- final int TRIANGLE_SHOT = 0;
- final int SINGLE_TRIANGLE_SHOT = 1;
- int currentAttackMode = SINGLE_TRIANGLE_SHOT;
- final int SINGLE_TRIANGLE_SHOT_AMMO = 1;
- final int TRIANGLE_SHOT_AMMO = 3;
- int maxTriangleShotDamage = 750;
- int maxSingleTriangleShotDamage = 1200;
- // Bullets variables
- ArrayList<PoisonBullet> bullets = new ArrayList<>();
- final int bulletDamage = 1250;
- int maxAmmoCount = 3;
- int ammoCount = maxAmmoCount;
- int baseAmmoRespawnInterval = 1000;
- int ammoRespawnInterval = 100;
- int lastAmmoRespawnTime = 0;
- int lastRefilledAmmoIndex = -3;
- int ammoRefillTime = 0;
- float bulletSpreadAngle = radians(10);
- float leftBulletOffset = -bulletSpreadAngle;
- float rightBulletOffset = bulletSpreadAngle;
- // Enemy square variables
- float enemyX, enemyY;
- float enemySize = 40;
- float enemySpeed = 3;
- float maxEnemyHealth = 1;
- float enemyHealth = maxEnemyHealth;
- boolean isEnemyVisible = true;
- // Triangle enemy variables
- float triangleEnemyX;
- float triangleEnemyY;
- float triangleEnemySize = 40; // Adjust the size as needed
- int maxTriangleEnemyHealth = 5000; // Adjust the max health as needed
- int triangleEnemyHealth = maxTriangleEnemyHealth;
- boolean isTriangleEnemyVisible = true;
- // Universal enemy variables
- boolean isEnemyDefeated = false;
- int defeatTimer = 0;
- final int defeatDuration = 1000; // 1 second in milliseconds
- int maxEnemies = 1; // Change this value to set the maximum number of enemies
- ArrayList<Enemy> enemies = new ArrayList<>();
- class Enemy {
- float x, y;
- float size = 40; // Adjust the size as needed
- float speed = 3;
- float maxHealth = 10000;
- float health = maxHealth;
- boolean isDefeated = false;
- boolean isVisible = true;
- int defeatTimer = 0;
- Enemy(float x, float y) {
- this.x = x;
- this.y = y;
- }
- void drawEnemy() {
- if (health > 0) {
- fill(255, 0, 0); // Red color for enemy
- ellipse(x + (size / 2), y + (size / 2), size, size);
- } else if (isDefeated) {
- // Shrink the enemy gradually until it disappears after defeat
- if (size > 0) {
- size -= 2; // Adjust the shrinking speed as needed
- } else {
- // Enemy is destroyed, remove it from the list of enemies
- isVisible = false;
- enemies.remove(this);
- }
- }
- // Draw health bar for visible enemies
- if (isVisible) {
- fill(100); // Gray color for health bar background
- rect(x, y - 20, size, 10);
- // Draw enemy health bar
- fill(255, 0, 0); // Red color for health bar
- float healthBarWidth = map(health, 0, maxHealth, 0, size);
- rect(x, y - 20, healthBarWidth, 10);
- // Display current enemy health amount above the health bar
- fill(0); // Black color for text
- textAlign(CENTER, CENTER); // Align the text to the bottom of the bounding box
- textSize(customFontSize); // Set the font size
- text((int) health, x + size / 2, y - 20 - healthBarPadding); // Display health as an integer
- }
- }
- }
- // DamageIndicator class
- class DamageIndicator {
- float x, y;
- String text;
- int lifeSpan = 60; // The number of frames the indicator will be visible
- int frameCount = 0;
- PFont customFont; // Custom font variable
- int customFontSize; // Custom font size variable
- DamageIndicator(float x, float y, String damage, PFont font, int fontSize) {
- this.x = x;
- this.y = y;
- this.text = damage;
- this.customFont = font;
- this.customFontSize = fontSize;
- }
- void update() {
- y -= 1; // Move the indicator upwards to make it float
- frameCount++;
- }
- void setY(float y) {
- this.y = y;
- }
- void display() {
- fill(255, 0, 0); // Red color for damage indicator text
- // Set custom font and size
- textFont(customFont);
- textSize(customFontSize);
- text(text, x, y);
- }
- boolean isFinished() {
- return frameCount >= lifeSpan;
- }
- }
- void updateEnemies() {
- for (int i = enemies.size() - 1; i >= 0; i--) {
- Enemy enemy = enemies.get(i);
- if (enemy.isVisible) {
- if (isEnemyVisible) {
- float targetX = playerX + (playerSize / 2);
- float targetY = playerY + (playerSize / 2);
- float dx = targetX - (enemy.x + (enemy.size / 2));
- float dy = targetY - (enemy.y + (enemy.size / 2));
- float distance = sqrt(dx * dx + dy * dy);
- if (distance > enemy.speed) {
- float angle = atan2(dy, dx);
- float vx = cos(angle) * enemy.speed;
- float vy = sin(angle) * enemy.speed;
- enemy.x += vx;
- enemy.y += vy;
- } else {
- enemy.x = targetX - (enemy.size / 2);
- enemy.y = targetY - (enemy.size / 2);
- }
- }
- // Check collisions between enemy and the player
- float dx = enemy.x - (playerX + (playerSize / 2));
- float dy = enemy.y - (playerY + (playerSize / 2));
- float distance = sqrt(dx * dx + dy * dy);
- if (distance < enemy.size / 2 + playerSize / 2) {
- if (millis() - lastAmmoRespawnTime > 1000) {
- playerHealth -= 1004; // Decrease player health when collided with the enemy
- lastAmmoRespawnTime = millis();
- }
- if (playerHealth <= 0) {
- playerHealth = 0;
- isPlayerAlive = false;
- // Handle player destroyed event (e.g., game over)
- }
- }
- // Check collisions between enemy and the triangle bullets
- for (int j = bullets.size() - 1; j >= 0; j--) {
- PoisonBullet bullet = bullets.get(j);
- if (bullet.hitsEnemy(enemy.x, enemy.y, enemy.size)) {
- bullets.remove(j);
- enemy.health -= bullet.damage; // Decrease enemy health when hit by a bullet
- if (enemy.health <= 0) {
- enemy.health = 0;
- enemy.isDefeated = true; // The enemy is defeated
- enemy.defeatTimer = millis();
- }
- // Add a new damage indicator at the enemy's position
- String damageText = Integer.toString(bullet.damage);
- damageIndicators.add(new DamageIndicator(enemy.x + enemy.size / 2, enemy.y, damageText, customFont, customFontSize));
- }
- }
- } else if (enemy.isDefeated) {
- // If the enemy is defeated, check if the defeatDuration has passed to remove it completely
- if (millis() - enemy.defeatTimer > defeatDuration) {
- enemies.remove(i);
- break; // Exit the loop to prevent concurrent modification error
- }
- }
- }
- }
- void setup() {
- for (int i = 0; i < maxEnemies; i++) {
- spawnEnemy();
- }
- size(400, 300);
- playerX = width / 2;
- playerY = height / 2;
- enemyX = random(width - enemySize);
- enemyY = random(height - enemySize);
- triangleEnemyX = random(width - triangleEnemySize);
- triangleEnemyY = random(height - triangleEnemySize);
- // Load your custom font in the setup (if you have one, replace "YourCustomFont.ttf" with the actual font file)
- // Place the font file in the "data" folder of your Processing sketch
- customFont = createFont("Nougat-ExtraBlack.ttf", customFontSize); // Replace "YourCustomFont.ttf" with the actual font file name
- }
- void draw() {
- if (isUIVisible) {
- if (showMainUI) {
- drawMainUI();
- } else if (showOptionsUI) {
- drawOptionsUI();
- }
- }
- updateEnemies();
- for (Enemy enemy : enemies) {
- if (enemy.isVisible) {
- enemy.drawEnemy();
- }
- }
- if (enemies.size() < maxEnemies && random(100) < 1) {
- spawnEnemy();
- }
- if (enemyHealth <= 0 && !isEnemyDefeated) {
- isEnemyDefeated = true;
- defeatTimer = millis();
- }
- background(255);
- // Draw damage indicators
- for (int i = damageIndicators.size() - 1; i >= 0; i--) {
- DamageIndicator indicator = damageIndicators.get(i);
- indicator.update();
- indicator.display();
- if (indicator.isFinished()) {
- damageIndicators.remove(i);
- }
- }
- // Update and draw player if the player is alive
- if (isPlayerAlive) {
- movePlayer();
- updateAmmoCount();
- checkCollisions();
- drawPlayer();
- drawBullets();
- drawAmmoCount();
- drawPlayerHealthBar();
- }
- // Update and draw enemy if visible
- for (Enemy enemy : enemies) {
- if (enemy.isVisible) {
- enemy.drawEnemy();
- }
- }
- // Randomly generate the purple square once
- if (!isPurpleSquareVisible && !isPlayerPoweredUp && random(100) <= 0.5) {
- purpleSquareX = random(width - purpleSquareSize);
- purpleSquareY = random(height - purpleSquareSize);
- isPurpleSquareVisible = true;
- }
- // Draw the purple square if it's visible
- if (isPurpleSquareVisible) {
- fill(150, 0, 150); // Purple color for the square
- rect(purpleSquareX, purpleSquareY, purpleSquareSize, purpleSquareSize);
- // Check collision between the player and the purple square
- if (isPlayerCollidingWithPurpleSquare()) {
- // Remove the purple square
- isPurpleSquareVisible = false;
- // Do something when the player collides with the square (e.g., activate power-up)
- isPlayerPoweredUp = true;
- powerupStartTime = millis();
- // Store the original player size, color, and damage values for later use
- originalPlayerSize = playerSize;
- originalPlayerColor = get(playerX, playerY); // Store the original fill color
- originalMaxTriangleShotDamage = maxTriangleShotDamage;
- originalMaxSingleTriangleShotDamage = maxSingleTriangleShotDamage;
- // Increase player size
- playerSize *= 1.5;
- // Change player color to purple
- fill(150, 0, 150); // Purple color
- // Multiply the maxTriangleShotDamage and maxSingleTriangleShotDamage by two
- maxTriangleShotDamage *= 2;
- maxSingleTriangleShotDamage *= 2;
- }
- }
- // Check if the power-up has ended
- if (isPlayerPoweredUp && millis() >= powerupStartTime + powerupDuration * 1000) {
- // Deactivate the power-up
- isPlayerPoweredUp = false;
- // Restore the original player size, color, and damage values
- playerSize = originalPlayerSize;
- fill(originalPlayerColor); // Restore the original fill color
- maxTriangleShotDamage = originalMaxTriangleShotDamage;
- maxSingleTriangleShotDamage = originalMaxSingleTriangleShotDamage;
- }
- }
- void drawOptionsUI() {
- int boxWidth = 250; // Increased box width
- int boxHeight = 150; // Increased box height
- int boxX = width / 2 - boxWidth / 2;
- int boxY = height / 2 - boxHeight / 2;
- fill(255);
- stroke(0); // Outline color
- rect(boxX, boxY, boxWidth, boxHeight, 8); // Rounded rectangle
- fill(0);
- textAlign(CENTER, CENTER);
- textSize(24);
- text("Options", width / 2, boxY + 30); // Title
- fill(150);
- rect(boxX + 10, boxY + 10, 30, 30); // Back button
- fill(0);
- textSize(18);
- text("<<", boxX + 25, boxY + 25);
- }
- void drawMainUI() {
- int boxWidth = 250; // Increased box width
- int boxHeight = 150; // Increased box height
- int boxX = width / 2 - boxWidth / 2;
- int boxY = height / 2 - boxHeight / 2;
- fill(255);
- stroke(0); // Outline color
- rect(boxX, boxY, boxWidth, boxHeight, 8); // Rounded rectangle
- fill(0);
- textAlign(CENTER, CENTER);
- textSize(24);
- text("Game Paused", width / 2, boxY + 30); // Title
- textSize(18);
- // Draw hitbox for Button 1
- int button1X = boxX + boxWidth / 2 - 50;
- int button1Y = boxY + 60;
- fill(150);
- rect(button1X, button1Y, 100, 30);
- fill(0);
- text("Continue", boxX + boxWidth / 2, button1Y + 15);
- // Draw hitbox for Button 2
- int button2X = boxX + boxWidth / 2 - 50;
- int button2Y = boxY + 60 + buttonPadding + 30;
- fill(150);
- rect(button2X, button2Y, 100, 30);
- fill(0);
- text("Options", boxX + boxWidth / 2, button2Y + 15);
- }
- void spawnEnemy() {
- float enemySize = 40; // Adjust the size as needed
- float enemyX = random(width - enemySize);
- float enemyY = random(height - enemySize);
- enemies.add(new Enemy(enemyX, enemyY));
- }
- void drawTriangleEnemy() {
- if (isTriangleEnemyVisible && triangleEnemyHealth <= 0 && !isEnemyDefeated) {
- isEnemyDefeated = true;
- defeatTimer = millis();
- }
- // Hide the triangle enemy if defeated
- if (isTriangleEnemyVisible && millis() - defeatTimer > defeatDuration) {
- isTriangleEnemyVisible = false;
- }
- if (isTriangleEnemyVisible && triangleEnemyHealth > 0) {
- fill(255, 165, 0); // Orange color for triangle enemy
- triangle(triangleEnemyX, triangleEnemyY + triangleEnemySize,
- triangleEnemyX + triangleEnemySize / 2, triangleEnemyY,
- triangleEnemyX + triangleEnemySize, triangleEnemyY + triangleEnemySize);
- } else {
- // Shrink the triangle enemy gradually until it disappears
- if (triangleEnemySize > 0) {
- triangleEnemySize -= 2; // Adjust the shrinking speed as needed
- } else {
- // Triangle enemy is destroyed, stop rendering it
- isTriangleEnemyVisible = false;
- }
- }
- // Draw health bar for the triangle enemy
- fill(100); // Gray color for health bar background
- rect(triangleEnemyX, triangleEnemyY - 20, triangleEnemySize, 10);
- // Draw triangle enemy health bar
- fill(255, 0, 0); // Red color for health bar
- float triangleEnemyHealthBarWidth = map(triangleEnemyHealth, 0, maxTriangleEnemyHealth, 0, triangleEnemySize);
- rect(triangleEnemyX, triangleEnemyY - 20, triangleEnemyHealthBarWidth, 10);
- // Display current triangle enemy health amount above the health bar
- fill(0); // Black color for text
- textAlign(CENTER, CENTER); // Align the text to the bottom of the bounding box
- textSize(customFontSize); // Set the font size
- text((int)triangleEnemyHealth, triangleEnemyX + triangleEnemySize / 2, triangleEnemyY - 20 - healthBarPadding); // Display health as an integer
- }
- // Function to check collision between the player and the purple square
- boolean isPlayerCollidingWithPurpleSquare() {
- return (playerX + playerSize / 2 > purpleSquareX &&
- playerX - playerSize / 2 < purpleSquareX + purpleSquareSize &&
- playerY + playerSize / 2 > purpleSquareY &&
- playerY - playerSize / 2 < purpleSquareY + purpleSquareSize);
- }
- // Player functions
- void movePlayer() {
- if (isMoving[0]) playerY -= playerSpeed;
- if (isMoving[1]) playerX -= playerSpeed;
- if (isMoving[2]) playerY += playerSpeed;
- if (isMoving[3]) playerX += playerSpeed;
- }
- // Add a new function to draw the player
- void drawPlayer() {
- if (isPlayerAlive) {
- // Check if the player is not moving and not attacking
- boolean isPlayerIdle = !isMoving[0] && !isMoving[1] && !isMoving[2] && !isMoving[3] && millis() - lastAmmoRespawnTime > healthIncreaseInterval;
- // Increase player health if the player is idle and hasn't reached the maximum health
- if (isPlayerIdle && playerHealth < maxPlayerHealth) {
- playerHealth += 5; // Increase the health by 5 (you can adjust this value as needed)
- if (playerHealth > maxPlayerHealth) {
- playerHealth = maxPlayerHealth;
- }
- lastHealthIncreaseTime = millis(); // Update the time when health was last increased
- }
- // Draw the player as a colored ellipse
- if (isPlayerPoweredUp) {
- fill(150, 0, 150); // Purple color if the player is powered up
- } else {
- fill(0, 255, 0); // Green color if the player is not powered up
- }
- ellipse(playerX, playerY, playerSize, playerSize);
- } else {
- // Player health is 0 or below, make the player object disappear
- return;
- }
- }
- void checkCollisions() {
- // Check collisions between bullets and the enemy base
- for (int i = bullets.size() - 1; i >= 0; i--) {
- PoisonBullet bullet = bullets.get(i);
- if (bullet.hitsEnemy(enemyX, enemyY, enemySize)) {
- bullets.remove(i);
- enemyHealth -= bullet.damage; // Decrease enemy health when hit by a bullet
- if (enemyHealth <= 0) {
- enemyHealth = 0;
- isEnemyVisible = false; // The enemy is destroyed
- }
- // Add a new damage indicator at the enemy's position
- String damageText = Integer.toString(bullet.damage);
- damageIndicators.add(new DamageIndicator(enemyX + enemySize / 2, enemyY, damageText, customFont, customFontSize));
- }
- } // Check collisions between bullets and the triangle enemy
- for (int i = bullets.size() - 1; i >= 0; i--) {
- PoisonBullet bullet = bullets.get(i);
- if (bullet.hitsEnemy(triangleEnemyX, triangleEnemyY, triangleEnemySize)) {
- bullets.remove(i);
- triangleEnemyHealth -= bullet.damage; // Decrease triangle enemy health when hit by a bullet
- if (triangleEnemyHealth <= 0) {
- triangleEnemyHealth = 0;
- isTriangleEnemyVisible = false; // The triangle enemy is destroyed
- }
- // Add a new damage indicator at the triangle enemy's position
- String damageText = Integer.toString(bullet.damage);
- damageIndicators.add(new DamageIndicator(triangleEnemyX + triangleEnemySize / 2, triangleEnemyY, damageText, customFont, customFontSize));
- }
- }
- // Check collisions between enemy and the player
- float dx = enemyX - (playerX + (playerSize / 2));
- float dy = enemyY - (playerY + (playerSize / 2));
- float distance = sqrt(dx * dx + dy * dy);
- if (distance < enemySize / 2 + playerSize / 2) {
- if (millis() - lastAmmoRespawnTime > 1000) {
- playerHealth -= 1004; // Decrease player health when collided with the enemy
- lastAmmoRespawnTime = millis();
- }
- if (playerHealth <= 0) {
- playerHealth = 0;
- isPlayerAlive = false;
- // Handle player destroyed event (e.g., game over)
- }
- }
- // Check collisions between triangle enemy and the player
- dx = triangleEnemyX - (playerX + (playerSize / 2));
- dy = triangleEnemyY - (playerY + (playerSize / 2));
- distance = sqrt(dx * dx + dy * dy);
- if (distance < triangleEnemySize / 2 + playerSize / 2) {
- if (millis() - lastAmmoRespawnTime > 1000) {
- playerHealth -= 1004; // Decrease player health when collided with the triangle enemy
- lastAmmoRespawnTime = millis();
- }
- if (playerHealth <= 0) {
- playerHealth = 0;
- isPlayerAlive = false;
- // Handle player destroyed event (e.g., game over)
- }
- }
- }
- void drawBullets() {
- for (int i = bullets.size() - 1; i >= 0; i--) {
- PoisonBullet bullet = bullets.get(i);
- bullet.update();
- bullet.display();
- }
- }
- // Ammo functions
- void drawAmmoCount() {
- if (isPlayerAlive) {
- float squareWidth = 15;
- float squareHeight = 10;
- float padding = 2;
- float startX = playerX - ((maxAmmoCount * squareWidth) + (padding * (maxAmmoCount - 1))) / 2;
- float startY = playerY - playerSize / 2 - padding - squareHeight;
- for (int i = 0; i < maxAmmoCount; i++) {
- float x = startX + i * (squareWidth + padding);
- float width = squareWidth;
- float height = squareHeight;
- if (i < ammoCount) {
- // Draw a filled orange rectangle for available ammo squares
- fill(255, 165, 0); // Orange color for ammo squares
- } else {
- // Draw empty gray rectangles for the rest of the ammo squares
- fill(200); // Gray color for empty ammo squares
- }
- rect(x, startY, width, height);
- }
- }
- }
- void updateAmmoCount() {
- // Calculate the respawn interval based on the current ammo count
- int ammoRespawnInterval = baseAmmoRespawnInterval + (maxAmmoCount - ammoCount) * 500;
- if (ammoCount < maxAmmoCount && millis() - lastAmmoRespawnTime > ammoRespawnInterval) {
- ammoCount++;
- lastRefilledAmmoIndex = (lastRefilledAmmoIndex + 1) % maxAmmoCount; // Update the last refilled ammo index
- lastAmmoRespawnTime = millis(); // Update the refill start time
- }
- }
- int healthBarPadding = 13; // Padding between health bar and text
- void drawPlayerHealthBar() {
- if (isPlayerAlive) {
- float healthBarWidth = map(playerHealth, 0, maxPlayerHealth, 0, playerSize);
- float healthBarHeight = 10;
- // Adjust the position of the health bar above the player
- float healthBarX = playerX - playerSize / 2;
- float healthBarY = playerY - playerSize / 2 - healthBarHeight - healthBarPadding;
- fill(100); // Gray color for health bar background
- rect(healthBarX, healthBarY, playerSize, healthBarHeight);
- fill(0, 255, 0); // Green color for health bar
- rect(healthBarX, healthBarY, healthBarWidth, healthBarHeight);
- // Display current player health amount above the health bar
- fill(0); // Black color for text
- textAlign(CENTER, CENTER); // Align the text to the bottom of the bounding box
- textSize(customFontSize); // Set the font size
- text((int)playerHealth, playerX, healthBarY - healthBarPadding); // Display health as an integer
- }
- }
- void mouseClicked() {
- if (showMainUI) {
- int boxWidth = 250; // Increased box width
- int boxHeight = 150; // Increased box height
- int boxX = width / 2 - boxWidth / 2;
- int boxY = height / 2 - boxHeight / 2;
- // Check if Button 1 is clicked
- int button1X = boxX + boxWidth / 2 - 50;
- int button1Y = boxY + 60;
- if (mouseX > button1X && mouseX < button1X + 100 &&
- mouseY > button1Y && mouseY < button1Y + 30) {
- // Button 1 clicked
- showMainUI = false;
- }
- // Check if Button 2 is clicked
- int button2X = boxX + boxWidth / 2 - 50;
- int button2Y = boxY + 60 + buttonPadding + 30;
- if (mouseX > button2X && mouseX < button2X + 100 &&
- mouseY > button2Y && mouseY < button2Y + 30) {
- // Button 2 clicked
- showMainUI = false;
- showOptionsUI = true;
- }
- } else if (showOptionsUI) {
- int boxWidth = 250; // Increased box width
- int boxHeight = 150; // Increased box height
- int boxX = width / 2 - boxWidth / 2;
- int boxY = height / 2 - boxHeight / 2;
- if (mouseX > boxX + 10 && mouseX < boxX + 40 &&
- mouseY > boxY + 10 && mouseY < boxY + 40) {
- showOptionsUI = false;
- showMainUI = true;
- }
- }
- }
- void keyPressed() {
- if (key == 'w' || key == 'W') isMoving[0] = true;
- else if (key == 'a' || key == 'A') isMoving[1] = true;
- else if (key == 's' || key == 'S') isMoving[2] = true;
- else if (key == 'd' || key == 'D') isMoving[3] = true;
- else if (key == 'c' || key == 'C') {
- currentAttackMode = (currentAttackMode + 1) % 2;
- }
- if (key == TAB) {
- isUIVisible = !isUIVisible;
- if (isUIVisible) {
- // Reset the UI states when it becomes visible
- showMainUI = false;
- showOptionsUI = false;
- }
- }
- }
- // Function to handle key releases
- void keyReleased() {
- if (key == 'w' || key == 'W') isMoving[0] = false;
- else if (key == 'a' || key == 'A') isMoving[1] = false;
- else if (key == 's' || key == 'S') isMoving[2] = false;
- else if (key == 'd' || key == 'D') isMoving[3] = false;
- }
- // Declare angle, lastMouseX, and lastMouseY as global variables
- float angle;
- float lastMouseX;
- float lastMouseY;
- void mousePressed() {
- if (isPlayerAlive && millis() - lastAmmoRespawnTime > ammoRespawnInterval) {
- // Calculate the angle based on the mouse position
- angle = atan2(mouseY - playerY, mouseX - playerX);
- // Store the last mouse position
- lastMouseX = mouseX;
- lastMouseY = mouseY;
- // Determine the attack mode and add bullets accordingly
- switch (currentAttackMode) {
- case TRIANGLE_SHOT:
- if (ammoCount >= TRIANGLE_SHOT_AMMO) {
- // Create the central bullet
- bullets.add(new PoisonBullet(playerX, playerY, angle, maxTriangleShotDamage));
- // Create the left bullet
- float leftBulletAngle = angle + leftBulletOffset;
- bullets.add(new PoisonBullet(playerX, playerY, leftBulletAngle, maxTriangleShotDamage));
- // Create the right bullet
- float rightBulletAngle = angle + rightBulletOffset;
- bullets.add(new PoisonBullet(playerX, playerY, rightBulletAngle, maxTriangleShotDamage));
- ammoCount -= TRIANGLE_SHOT_AMMO;
- lastAmmoRespawnTime = millis();
- }
- break;
- case SINGLE_TRIANGLE_SHOT:
- if (ammoCount >= SINGLE_TRIANGLE_SHOT_AMMO) {
- // Create a single triangle bullet
- bullets.add(new PoisonBullet(playerX, playerY, angle, maxSingleTriangleShotDamage));
- ammoCount -= SINGLE_TRIANGLE_SHOT_AMMO;
- lastAmmoRespawnTime = millis();
- }
- break;
- }
- }
- }
- // PoisonBullet class
- class PoisonBullet {
- float x, y;
- float speed = 10;
- float angle;
- int damage; // Damage value for the bullet
- PoisonBullet(float x, float y, float angle) {
- this.x = x;
- this.y = y;
- this.angle = angle;
- this.damage = 5000; // Default damage for the triangle shot
- }
- PoisonBullet(float x, float y, float angle, int damage) {
- this.x = x;
- this.y = y;
- this.angle = angle;
- this.damage = damage;
- }
- void update() {
- x += cos(angle) * speed;
- y += sin(angle) * speed;
- }
- void display() {
- fill(0, 90, 255); // Blue color for bullets
- pushMatrix();
- translate(x, y);
- rotate(angle - HALF_PI);
- triangle(0, 0, -5, -15, 5, -15);
- popMatrix();
- }
- boolean isOffScreen() {
- return (x < 0 || x > width || y < 0 || y > height);
- }
- boolean hitsEnemy(float enemyX, float enemyY, float enemySize) {
- float dx = enemyX + enemySize / 2 - x;
- float dy = enemyY + enemySize / 2 - y;
- float distance = sqrt(dx * dx + dy * dy);
- return distance < enemySize / 2 + 15 / 2; // 15 is the width of the bullet triangle
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement