Skip to content

Commit

Permalink
hitBoxEdit1
Browse files Browse the repository at this point in the history
  • Loading branch information
PacharaphonP committed May 18, 2023
1 parent b7e018d commit 79eaf1b
Show file tree
Hide file tree
Showing 24 changed files with 461 additions and 221 deletions.
Binary file added bin/Object/Projectile.class
Binary file not shown.
Binary file added bin/ShadowPot/ball.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified bin/logic/entity/Chicknight.class
Binary file not shown.
Binary file modified bin/logic/entity/Enemy.class
Binary file not shown.
Binary file modified bin/logic/entity/Entity.class
Binary file not shown.
Binary file modified bin/logic/entity/EyeOfQwifot.class
Binary file not shown.
Binary file modified bin/logic/entity/GriszlyEye.class
Binary file not shown.
Binary file modified bin/logic/entity/MagicalTortoise.class
Binary file not shown.
Binary file modified bin/logic/entity/Player.class
Binary file not shown.
Binary file modified bin/logic/entity/ShadowPot.class
Binary file not shown.
Binary file modified bin/logic/game/GameLogic.class
Binary file not shown.
Binary file modified bin/sharedObject/RenderableHolder.class
Binary file not shown.
Binary file added res/ShadowPot/ball.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
108 changes: 108 additions & 0 deletions src/Object/Projectile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package Object;

import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import logic.entity.Player;
import logic.game.GameLogic;
import sharedObject.IRenderable;
import sharedObject.RenderableHolder;

public class Projectile implements IRenderable {

protected double worldX, worldY;
public double screenX, screenY;
private Rectangle solidArea,solidScreen;
private double angle;
private int dmg, speed;
protected int z, radius;
protected boolean visible, destroyed;
public GameLogic gameLogic;
private double xspeed, yspeed;

public Projectile(double worldX, double worldY, double angle, GameLogic gameLogic) {
this.worldX = worldX;
this.worldY = worldY;
this.gameLogic = gameLogic;
this.angle = angle;
this.speed = 3;
this.z = 10;
this.visible = true;
this.destroyed = false;
this.xspeed = Math.cos(angle) * speed;
this.yspeed = Math.sin(angle) * speed;
this.dmg = 5;
solidArea = new Rectangle(0, 0, 8, 8);
solidScreen = new Rectangle(screenX,screenY,8,8);
}

public void update() {
worldX += xspeed;
worldY += yspeed;
screenX = worldX - gameLogic.getPlayer().getWorldX() + gameLogic.getPlayer().screenX;
screenY = worldY - gameLogic.getPlayer().getWorldY() + gameLogic.getPlayer().screenY;
solidArea.setX(screenX);
solidArea.setY(screenY);
// System.out.println("X =" + xspeed + " Y = " + yspeed);
boolean isOut = screenX < 0 || screenX > 1280 || screenY < 0 || screenY > 720;
if (isOut) {
destroyed = true;
}
checkEnemyHit();
}

public void checkEnemyHit() {
Player p =gameLogic.getPlayer();
int x = (int) p.getScreenX();
int y = (int) p.getScreenY();
int width = (int) p.getSolidArea().getWidth();
int height = (int) p.getSolidArea().getHeight();
boolean overlap = solidArea.intersects(x,y,width,height);
System.out.println("Overlap = " + overlap);
System.out.println("X = " + x + " Y = " + y);
if (overlap) {
p.changeHealthTo(p.getCurrentHealth() - dmg);
destroyed = true;
}
solidArea.setX(screenX);
solidArea.setY(screenY);
}

public Rectangle getSolidArea() {
return solidArea;
}

@Override
public int getZ() {
// TODO Auto-generated method stub
return 0;
}

@Override
public void draw(GraphicsContext gc) {
// TODO Auto-generated method stub
gc.drawImage(RenderableHolder.ball, screenX, screenY);
drawHitbox(gc);
}

@Override
public boolean isDestroyed() {
// TODO Auto-generated method stub

return destroyed;
}

@Override
public boolean isVisible() {
// TODO Auto-generated method stub
return visible;
}

// Debugger
public void drawHitbox(GraphicsContext gc) {
gc.setLineWidth(2);
gc.setFill(Color.RED);
gc.strokeRect(solidArea.getX(), solidArea.getY(), solidArea.getWidth(),
solidArea.getHeight());
}
}
74 changes: 12 additions & 62 deletions src/logic/entity/Chicknight.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,17 @@
import logic.game.GameLogic;
import sharedObject.RenderableHolder;

public class Chicknight extends Entity {
private double angle = 0;
public class Chicknight extends Enemy {
private Image image = RenderableHolder.CKRight;
private String currentState = "default";

// AttackBlock
private Rectangle attackBlock;
private int attackOffset;

public Chicknight(int x, int y, GameLogic gameLogic) {
super(x,y,gameLogic);
this.maxHp = 10;
this.currentHealth = maxHp;
this.dmg = 5;
this.z = -100;
this.speed = 1;
image = RenderableHolder.CKRight;
initSolidArea();
initAttackBlock();
}
Expand Down Expand Up @@ -70,20 +66,14 @@ public void draw(GraphicsContext gc) {
drawAttackBlock(gc);
}

@Override
public void attack(Entity p) {
// TODO Auto-generated method stub
System.out.println(currentState);
((Player) p).changeHealthTo(gameLogic.getPlayer().getCurrentHealth()-dmg);
System.out.println("SKDASDKLASMDKANDKLDKLMLWQD");

}


@Override
public void update() {
// TODO Auto-generated method stub
screenX = worldX - gameLogic.getPlayer().worldX + gameLogic.getPlayer().screenX;
screenY = worldY - gameLogic.getPlayer().worldY + gameLogic.getPlayer().screenY;
super.update();


Player player = gameLogic.getPlayer();
if (!canAttack(player.worldX, player.worldY, worldX, worldY, (int)(attackBlock.getWidth()+solidArea.getWidth()))) {
angle = Math.atan2(player.worldY - worldY, player.worldX - worldX);
Expand Down Expand Up @@ -117,7 +107,6 @@ public void update() {
currentState = "attack";
attack(gameLogic.getPlayer());
}

updateAttackBlock();
}

Expand All @@ -126,54 +115,15 @@ public void initSolidArea() {
}

public void initAttackBlock() {
// screenX = worldX-gameLogic.getPlayer().worldX+gameLogic.getPlayer().screenX;
// screenY = worldY-gameLogic.getPlayer().worldY+gameLogic.getPlayer().screenY;
attackBlock = new Rectangle(screenX, screenY, 10 * 2, 7 * 2);
// attackOffset = (int)(*2);
}

public void checkEnemyHit(Rectangle attackBlock) {
int x = (int) getSolidArea().getX();
int y = (int) getSolidArea().getY();
int width = (int) getSolidArea().getWidth();
int height = (int) getSolidArea().getHeight();
boolean overlap = attackBlock.intersects(x,y,width,height);
if (overlap) this.attack(gameLogic.getPlayer());
}

public void updateAttackBlock() {
attackBlock.setX(screenX + solidArea.getX());
attackBlock.setY(screenY + solidArea.getY());
}

public void changeHealthTo(int health) {
if (health>=maxHp) {
currentHealth = maxHp;
}
else if (health<=0) {
currentHealth = 0;
setDestroyed(true);
}
else {
currentHealth = health;
// System.out.println("Plathong" + currentHealth);
}
attackBlock = new Rectangle(screenX+solidArea.getWidth(), screenY, 10 * 2, 7 * 2);
}

public int getCurrentHealth() {
return currentHealth;
}

// for Debugging
public void drawHitbox(GraphicsContext gc) {
gc.setLineWidth(2);
gc.setFill(Color.PINK);
gc.strokeRect(screenX, screenY, 32, 64);
}

// Debug Chick
public void drawAttackBlock(GraphicsContext gc) {
gc.setFill(Color.BLACK);
gc.strokeRect(attackBlock.getX()+solidArea.getWidth(), attackBlock.getY(), attackBlock.getWidth(), attackBlock.getHeight());
gc.strokeRect(attackBlock.getX()+solidArea.getWidth(), attackBlock.getY(), attackBlock.getWidth(),
attackBlock.getHeight());
}


}
68 changes: 67 additions & 1 deletion src/logic/entity/Enemy.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,78 @@
package logic.entity;

import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import logic.game.GameLogic;

public abstract class Enemy extends Entity{

protected double angle = 0;
protected String currentState;

public Enemy(int x, int y, GameLogic gameLogic) {
super(x, y, gameLogic);
// TODO Auto-generated constructor stub
}

public void changeHealthTo(int health) {
if (health>=maxHp) {
currentHealth = maxHp;
}
else if (health<=0) {
currentHealth = 0;
setDestroyed(true);
}
else {
currentHealth = health;
// System.out.println("Plathong" + currentHealth);
}
}
public boolean checkEnemyHit() {
Player p =gameLogic.getPlayer();
int x = (int) p.getScreenX();
int y = (int) p.getScreenY();
int width = (int) p.getSolidArea().getWidth();
int height = (int) p.getSolidArea().getHeight();
boolean overlap = solidScreen.intersects(x,y,width,height);
// System.out.println("Overlap = " + overlap);
// System.out.println("X = " + x + " Y = " + y);
return overlap;

}

public int getCurrentHealth() {
return currentHealth;
}

public void attack(Entity p) {
// TODO Auto-generated method stub
System.out.println(this.getClass().getSimpleName()+"Attack");
if (checkEnemyHit()) ((Player) p).changeHealthTo(gameLogic.getPlayer().getCurrentHealth()-dmg);
}

//Debugger
public void drawHitbox(GraphicsContext gc) {
gc.setLineWidth(2);
gc.setFill(Color.PINK);
gc.strokeRect(solidScreen.getX(), solidScreen.getY(), solidScreen.getWidth(), solidScreen.getHeight());
}

protected String currentState = "alive";
public void drawAttackBlock(GraphicsContext gc) {
gc.setFill(Color.BLACK);
gc.strokeRect(solidArea.getX(), attackBlock.getY(), attackBlock.getWidth(),
attackBlock.getHeight());
}

public void updateAttackBlock() {
attackBlock.setX(screenX);
attackBlock.setY(screenY);
}

public void update() {
super.update();
solidScreen = new Rectangle(screenX+solidArea.getX(),screenY+solidArea.getY(),solidArea.getWidth(),solidArea.getHeight());
}
public abstract void initSolidArea();
public abstract void initAttackBlock();
}
47 changes: 28 additions & 19 deletions src/logic/entity/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,46 @@
import logic.game.GameLogic;
import sharedObject.IRenderable;

public abstract class Entity implements IRenderable{
protected double worldX,worldY;
public double screenX,screenY;
protected int z,radius;
protected boolean visible,destroyed;
public abstract class Entity implements IRenderable {
protected double worldX, worldY;
public double screenX, screenY;
protected int z, radius;
protected boolean visible, destroyed;
protected int speed;
protected String direction;
public Rectangle solidArea;
protected Image image;
public Rectangle solidArea,solidScreen;
public boolean collisionOn = false;
public GameLogic gameLogic;

// Status
protected int maxHp;
protected int currentHealth;
protected int dmg;
protected Image image;

public Entity(int x, int y,GameLogic gameLogic){

// AttackBlock
protected Rectangle attackBlock;

public Entity(int x, int y, GameLogic gameLogic) {
visible = true;
destroyed = false;
worldX = x;
worldY = y;
this.gameLogic = gameLogic;
this.direction = "right";
}

public abstract void attack(Entity t);
public abstract void update();

public boolean canAttack(double x1,double y1,double x2,double y2,int attackRange) {
return (Math.abs(x1-x2) < attackRange && Math.abs(y1-y2) < attackRange);

public void update() {
screenX = worldX - gameLogic.getPlayer().worldX + gameLogic.getPlayer().screenX;
screenY = worldY - gameLogic.getPlayer().worldY + gameLogic.getPlayer().screenY;
}


public boolean canAttack(double x1, double y1, double x2, double y2, int attackRange) {
return (Math.abs(x1 - x2) < attackRange && Math.abs(y1 - y2) < attackRange);
}

@Override
public int getZ() {
// TODO Auto-generated method stub
Expand All @@ -60,8 +69,8 @@ public double getWorldX() {

public double getWorldY() {
return worldY;
}
}

public String getDirection() {
return direction;
}
Expand Down Expand Up @@ -89,13 +98,13 @@ public Rectangle getSolidArea() {
public boolean isCollisionOn() {
return collisionOn;
}

public void setCollisionOn(boolean collisionOn) {
this.collisionOn = collisionOn;
}

public void setDestroyed(boolean destroyed) {
this.destroyed = destroyed;
}

}
Loading

0 comments on commit 79eaf1b

Please sign in to comment.