mirror of
https://github.com/Noratrieb/Java2DGame.git
synced 2026-01-14 14:05:01 +01:00
basic trigger
This commit is contained in:
parent
e82b5fe02f
commit
df4932adb3
7 changed files with 31 additions and 10 deletions
|
|
@ -206,8 +206,7 @@ public class Master extends JPanel {
|
||||||
* @return True if it collides with something, false if it doesn't - Should return a Collision
|
* @return True if it collides with something, false if it doesn't - Should return a Collision
|
||||||
*/
|
*/
|
||||||
public Collision doesCollide(Collidable collidable) {
|
public Collision doesCollide(Collidable collidable) {
|
||||||
long time = System.nanoTime();
|
Collision collision = null;
|
||||||
Collision collides = null;
|
|
||||||
|
|
||||||
for (Collidable other : collidables) {
|
for (Collidable other : collidables) {
|
||||||
|
|
||||||
|
|
@ -217,13 +216,17 @@ public class Master extends JPanel {
|
||||||
if (other != collidable && (distance < other.getHitbox().getSize() + collidable.getHitbox().getSize())) {
|
if (other != collidable && (distance < other.getHitbox().getSize() + collidable.getHitbox().getSize())) {
|
||||||
|
|
||||||
if (other.getHitbox().collidesWith(collidable.getHitbox())) {
|
if (other.getHitbox().collidesWith(collidable.getHitbox())) {
|
||||||
collides = new Collision(collidable, other);
|
collision = new Collision(collidable, other);
|
||||||
|
if(collision.isTrigger()){
|
||||||
|
collidable.onTrigger();
|
||||||
|
} else {
|
||||||
collidable.onCollision();
|
collidable.onCollision();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return collides;
|
}
|
||||||
|
return collision;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getW() {
|
public int getW() {
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ import core.objects.core.GameObject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class provides everything about the local coordinate system the game uses
|
* This class provides everything about the local coordinate system the game uses
|
||||||
* <p>In this system, the screen is always 100 high and 100 * the screen ration wide.
|
* <p>In this system, the screen is always 100 high and 100 * the screen ratio wide.
|
||||||
* This class is the used to convert these coordinates into the true Java 2D coordinates for the drawing</p>
|
* This class is the used to convert these coordinates into the true Java 2D coordinates for the drawing</p>
|
||||||
*/
|
*/
|
||||||
public class Coordinates {
|
public class Coordinates {
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package core.objects.core;
|
||||||
import core.math.Vector2D;
|
import core.math.Vector2D;
|
||||||
import core.objects.core.GameObject;
|
import core.objects.core.GameObject;
|
||||||
import core.physics.Collidable;
|
import core.physics.Collidable;
|
||||||
|
import core.physics.Collision;
|
||||||
import core.physics.hitboxes.Hitbox;
|
import core.physics.hitboxes.Hitbox;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
@ -38,7 +39,8 @@ public abstract class CollGameObject extends GameObject implements Collidable {
|
||||||
this.position = target;
|
this.position = target;
|
||||||
|
|
||||||
((Collidable) this).getHitbox().moveTo(position, size);
|
((Collidable) this).getHitbox().moveTo(position, size);
|
||||||
if (master.doesCollide(this) != null) {
|
Collision coll = master.doesCollide(this);
|
||||||
|
if (coll != null && !coll.isTrigger()) {
|
||||||
this.position = oldPos;
|
this.position = oldPos;
|
||||||
((Collidable) this).getHitbox().moveTo(oldPos, size);
|
((Collidable) this).getHitbox().moveTo(oldPos, size);
|
||||||
}
|
}
|
||||||
|
|
@ -63,6 +65,10 @@ public abstract class CollGameObject extends GameObject implements Collidable {
|
||||||
public void onCollision() {
|
public void onCollision() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onTrigger() {
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ArrayList<Class<?>> getIgnores() {
|
public ArrayList<Class<?>> getIgnores() {
|
||||||
return ignores;
|
return ignores;
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ public interface Collidable {
|
||||||
Vector2D getCenterPos();
|
Vector2D getCenterPos();
|
||||||
Vector2D getSize();
|
Vector2D getSize();
|
||||||
void onCollision();
|
void onCollision();
|
||||||
|
void onTrigger();
|
||||||
ArrayList<Class<?>> getIgnores();
|
ArrayList<Class<?>> getIgnores();
|
||||||
boolean isTrigger();
|
boolean isTrigger();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,9 +8,21 @@ public class Collision {
|
||||||
private final Collidable a;
|
private final Collidable a;
|
||||||
private final Collidable b;
|
private final Collidable b;
|
||||||
|
|
||||||
|
private final boolean isTrigger;
|
||||||
|
|
||||||
public Collision(Collidable a, Collidable b) {
|
public Collision(Collidable a, Collidable b) {
|
||||||
this.a = a;
|
this.a = a;
|
||||||
this.b = b;
|
this.b = b;
|
||||||
|
|
||||||
|
this.isTrigger = a.isTrigger() || b.isTrigger();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether the collision is a trigger and shouldn't impact movement
|
||||||
|
* @return True if it's a trigger, false when it isn't
|
||||||
|
*/
|
||||||
|
public boolean isTrigger() {
|
||||||
|
return isTrigger;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ import java.awt.*;
|
||||||
*/
|
*/
|
||||||
public class Init {
|
public class Init {
|
||||||
|
|
||||||
public static final boolean DEBUG_MODE = true;
|
public static final boolean DEBUG_MODE = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new GameObject
|
* Create a new GameObject
|
||||||
|
|
|
||||||
|
|
@ -32,8 +32,7 @@ public class Shell extends CollGameObject {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCollision() {
|
public void onTrigger() {
|
||||||
destroy();
|
destroy();
|
||||||
//master.debugPos(position);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue