This commit is contained in:
2024-05-26 17:24:02 +02:00
commit d0bf6c6066
14 changed files with 330 additions and 0 deletions

24
src/Cell.java Normal file
View File

@@ -0,0 +1,24 @@
public class Cell {
float width;
float x, y;
Character character;
Cell(float x, float y, float width, Character character) {
this.width = width;
this.x = x * width/2;
this.y = y * width/2;
this.character = character;
}
public boolean hasChar() {
if (this.character != null) {
return true;
}
return false;
}
public void draw() {
if (this.hasChar()) {
character.draw(this.x, this.y);
}
}
}

31
src/Character.java Normal file
View File

@@ -0,0 +1,31 @@
import processing.core.PApplet;
public class Character {
PApplet window;
String type;
int color;
boolean collidable;
Character (PApplet window, String type) {
this.window = window;
this.type = type;
this.collidable = false;
this.color = 0;
}
Character (PApplet window, String type, int color) {
this.window = window;
this.type = type;
this.color = color;
this.collidable = false;
}
public Character(PApplet window) {
this.window = window;
}
public void draw(float x, float y) {
window.fill(color);
window.text(type, x, y);
}
}

29
src/Enemy.java Normal file
View File

@@ -0,0 +1,29 @@
import processing.core.PApplet;
public class Enemy extends Character implements Health {
int health;
int maxHealth = 20;
Enemy(PApplet window) {
super(window);
this.type = "A";
this.health = maxHealth;
this.collidable = true;
this.color = window.color(200, 0, 0);
}
public void damage(int damage) {
health = health - damage;
if (health <= 0) {
window.println("dead");
this.color = window.color(100);
}
}
public void heal(int healing) {
health = health + healing;
if (health > maxHealth) {
health = maxHealth;
}
}
}

108
src/Fenster.java Normal file
View File

@@ -0,0 +1,108 @@
import processing.core.PApplet;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
public class Fenster extends PApplet {
Cell[][] tileMap;
Character spot = new Character(this, ".");
Player player = new Player(this);
Character old_player_cell;
int old_player_x;
int old_player_y;
UI ui = new UI(this);
@Override
public void settings() {
size(800, 800);
tileMap = new Cell[100][100];
for(int i = 0; i < tileMap.length; i++) {
for(int j = 0; j < tileMap[0].length; j++) {
tileMap[i][j] = new Cell(j, i, 40.0f, new Character(this, "."));//put whatever number you want in
//here and it will insert it instead of 0's
}
}
tileMap[10][10].character = new Enemy(this);
//this.text("lol", 0, 0);
//this.rect(4.0f, 4.0f, 4.0f, 4.0f);
}
@Override
public void setup() {
this.textSize(20);
this.textAlign(CENTER, CENTER);
}
@Override
public void draw() {
background(255);
// Handle the Cell which the player was on last round
if (tileMap[player.y][player.x].character != player) {
old_player_cell = tileMap[player.y][player.x].character;
old_player_x = player.x;
old_player_y = player.y;
}
tileMap[player.y][player.x].character = player;
for(int i = 0; i < tileMap.length; i++) {
for(int j = 0; j < tileMap[0].length; j++) {
tileMap[i][j].draw();//put whatever number you want in
//here and it will insert it instead of 0's
}
}
ui.draw();
//println(player.x + " " + player.y);
}
public void keyPressed(processing.event.KeyEvent event) {
this.keyPressed();
if (keyCode == this.UP) {
println("pressed");
Cell newTile = tileMap[player.y -1][player.x];
if (!newTile.character.collidable) {
player.y = player.y - 1;
tileMap[old_player_y][old_player_x].character = old_player_cell;
} else if (newTile.character instanceof Enemy) {
player.damageEnemy(5, (Enemy) newTile.character);
}
}
if (keyCode == this.LEFT) {
Cell newTile = tileMap[player.y][player.x - 1];
println("pressed");
if (!newTile.character.collidable) {
player.x = player.x - 1;
tileMap[old_player_y][old_player_x].character = old_player_cell;
} else if (newTile.character instanceof Enemy) {
player.damageEnemy(5, (Enemy) newTile.character);
}
}
if (keyCode == this.DOWN) {
Cell newTile = tileMap[player.y +1][player.x];
println("pressed");
if (!newTile.character.collidable) {
player.y = player.y + 1;
tileMap[old_player_y][old_player_x].character = old_player_cell;
} else if (newTile.character instanceof Enemy) {
player.damageEnemy(5, (Enemy) newTile.character);
}
}
if (keyCode == this.RIGHT) {
Cell newTile = tileMap[player.y][player.x + 1];
println("pressed");
if (!newTile.character.collidable) {
player.x = player.x + 1;
tileMap[old_player_y][old_player_x].character = old_player_cell;
} else if (newTile.character instanceof Enemy) {
player.damageEnemy(5, (Enemy) newTile.character);
}
}
}
}

7
src/Health.java Normal file
View File

@@ -0,0 +1,7 @@
public interface Health {
public void damage(int damage);
public void heal(int healing);
}

9
src/Main.java Normal file
View File

@@ -0,0 +1,9 @@
import processing.core.PApplet;
// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
public class Main {
public static void main(String[] args) {
PApplet.main("Fenster");
}
}

33
src/Player.java Normal file
View File

@@ -0,0 +1,33 @@
import processing.core.PApplet;
public class Player extends Character implements Health {
int x, y;
int health;
int maxHealth = 20;
Player(PApplet window) {
super(window);
this.type = "@";
this.x = 4;
this.y = 4;
this.health = maxHealth;
}
public void damageEnemy(int damage, Enemy enemy) {
enemy.damage(damage);
}
public void damage(int damage) {
health = health - damage;
if (health <= 0) {
window.println("dead");
}
}
public void heal(int healing) {
health = health + healing;
if (health > maxHealth) {
health = maxHealth;
}
}
}

18
src/UI.java Normal file
View File

@@ -0,0 +1,18 @@
import processing.core.PApplet;
public class UI {
PApplet window;
UI(PApplet window) {
this.window = window;
}
private void bottomBar(float x, float y) {
window.rectMode(window.CENTER);
window.rect(x, 775, 300, 50);
}
public void draw() {
bottomBar(400, 700);
}
}