Fight window

This commit is contained in:
Makussu 2024-06-05 00:04:52 +02:00
parent d3283cd0f6
commit 5a51e43a4f
7 changed files with 159 additions and 11 deletions

View File

@ -6,7 +6,7 @@ import java.util.List;
/**
* The speech bubble that can be shown by all Talkable characters
*/
public class Bubble {
public class Bubble implements Drawable {
Fenster window;
List<String> text;
boolean opened;
@ -17,6 +17,11 @@ public class Bubble {
this.text = text;
this.opened = false;
}
public Bubble(Fenster window) {
this.window = window;
}
public void open() {
this.opened = true;
}

View File

@ -3,7 +3,7 @@ import processing.core.PApplet;
/**
* Class from which we define all other Entities which are seen in the game.
*/
public class Character {
public class Character implements Drawable {
Fenster window;
String type;
int color;
@ -46,6 +46,7 @@ public class Character {
this.x = x;
this.y = y;
window.fill(color);
window.textSize(20);
window.text(type, x * window.tilewidth/2, y * window.tilewidth/2);
}
}

3
src/Drawable.java Normal file
View File

@ -0,0 +1,3 @@
public interface Drawable {
public void draw(float x, float y);
}

View File

@ -18,6 +18,7 @@ public class Enemy extends Character implements Health, Interactable {
public void damage(int damage) {
health = health - damage;
if (health <= 0) {
health = 0;
window.println("dead");
this.color = window.color(100);
}
@ -30,7 +31,19 @@ public class Enemy extends Character implements Health, Interactable {
}
}
public void draw(float x, float y) {
this.x = x;
this.y = y;
window.fill(color);
window.text(type, x * window.tilewidth/2, y * window.tilewidth/2);
if (window.inFight) {
window.ui.healthBar(x * window.tilewidth/2, y * window.tilewidth/2 - 15, health, maxHealth);
}
}
public void interact() {
window.inGame = false;
window.inFight = true;
window.player.damageEnemy(5, this);
}
}

View File

@ -20,6 +20,8 @@ public class Fenster extends PApplet {
boolean inMenu = false;
boolean inDialog = false;
Fight myfight;
UI ui = new UI(this);
@Override
public void settings() {
@ -28,7 +30,7 @@ public class Fenster extends PApplet {
tiles.settings();
myfight = new Fight(this, player, (Enemy) tiles.get(10, 10));
//this.text("lol", 0, 0);
@ -41,6 +43,8 @@ public class Fenster extends PApplet {
this.textFont(font);
this.textSize(20);
this.textAlign(CENTER, CENTER);
}
@Override
@ -59,6 +63,10 @@ public class Fenster extends PApplet {
ui.draw();
if (inFight) {
myfight.draw();
}
//println(player.x + " " + player.y);
}
@ -70,7 +78,11 @@ public class Fenster extends PApplet {
player.y = player.y - 1;
tiles.set(old_player_x, old_player_y, old_player_cell);
} else if (newTile.character instanceof Interactable) {
((Interactable) newTile.character).interact();
if (newTile.character instanceof Enemy) {
myfight = new Fight(this, player, (Enemy) newTile.character);
} else {
((Interactable) newTile.character).interact();
}
}
}
if (keyCode == this.LEFT) {
@ -100,7 +112,18 @@ public class Fenster extends PApplet {
((Interactable) newTile.character).interact();
}
}
}
if (inFight) {
if (keyCode == 32) {
myfight.attack();
}
if (keyCode == this.ESC) {
if (key == ESC) {
key = 0;
}
inFight = false;
inGame = true;
}
}
}
}

View File

@ -1,10 +1,106 @@
import java.util.List;
public class Fight extends Bubble {
Player player;
Enemy enemy;
Fight(Fenster window, List<String> text) {
super(window, text);
float player_x = 200;
float player_y = 500;
this.opened = false;
float enemy_x = 600;
float enemy_y = 300;
boolean animating = false;
Fight(Fenster window, Player player, Enemy enemy) {
super(window);
this.player = player;
this.enemy = enemy;
this.opened = true;
}
void drawPlayer() {
window.textSize(150);
window.fill(0);
window.text(player.type, player_x, player_y);
window.ui.healthBar(player_x, player_y-60, player.health, player.maxHealth);
}
void drawEnemy() {
window.textSize(150);
window.fill(enemy.color);
window.text(enemy.type, enemy_x, enemy_y);
window.ui.healthBar(enemy_x, enemy_y-60, enemy.health, enemy.maxHealth);
}
public void attack() {
animating = true;
}
int i = 0;
boolean ani_hit = false;
void animating_attack() {
int numPoints = 10;
float startanimate_x;
float startanimate_y;
float stopanimate_x;
float stopanimate_y;
if (i == 5 && ani_hit == false) {
ani_hit = true;
}
if (!ani_hit) {
startanimate_x = 600;
startanimate_y = 300;
stopanimate_x = 200;
stopanimate_y = 500;
} else {
startanimate_x = 200;
startanimate_y = 500;
stopanimate_x = 600;
stopanimate_y = 300;
}
if (i < 10) {
float interpolationAmount = window.map(i, 0, numPoints - 1, 0, 1);
float interpolatedX = window.lerp(startanimate_x, stopanimate_x, interpolationAmount);
float interpolatedY = window.lerp(startanimate_y, stopanimate_y, interpolationAmount);
player_x = interpolatedX;
player_y = interpolatedY;
i++;
}
}
void animation_cleanup() {
if (!ani_hit && player_x == 600 && player_y == 300) {
animating = false;
i = 0;
ani_hit = false;
player_x = 600;
player_y = 300;
}
}
public void draw() {
if (opened) {
window.fill(255);
window.rectMode(window.CENTER);
window.rect(window.width/2, window.height/2, 700, 500);
drawPlayer();
drawEnemy();
window.fill(0);
}
if (animating) {
animating_attack();
}
if (enemy.health <= 0) {
window.inFight = false;
window.inGame = true;
}
}
}

View File

@ -1,4 +1,5 @@
import processing.core.PApplet;
import processing.core.PShape;
public class UI {
PApplet window;
@ -12,12 +13,18 @@ public class UI {
window.rect(x, 775, 300, 50);
}
private void healthBar() {
window.text("", 20, 20);
public void healthBar(float x, float y, int health, int maxHealth) {
float barlength = 100;
float healthLength = barlength / maxHealth * health;
window.rectMode(window.CORNER);
window.fill(0);
window.rect(x -barlength/2, y, barlength, 5);
window.fill(window.color(255, 0, 0));
window.rect(x - barlength/2, y, healthLength, 5);
}
public void draw() {
bottomBar(400, 700);
healthBar();
}
}