Skip to content

Commit

Permalink
🔨 Added Rigidbodies and Initial Colliders
Browse files Browse the repository at this point in the history
  • Loading branch information
jabo-bernardo authored Apr 11, 2020
1 parent 9c09852 commit 0286c37
Show file tree
Hide file tree
Showing 14 changed files with 133 additions and 3 deletions.
Binary file modified bin/dev/jabo/kree/GameObject.class
Binary file not shown.
Binary file modified bin/dev/jabo/kree/Scene.class
Binary file not shown.
Binary file modified bin/dev/jabo/kree/components/Animator.class
Binary file not shown.
Binary file added bin/dev/jabo/kree/components/BoxCollider.class
Binary file not shown.
Binary file modified bin/dev/jabo/kree/components/MeshRenderer.class
Binary file not shown.
Binary file added bin/dev/jabo/kree/components/RigidBody.class
Binary file not shown.
Binary file modified bin/dev/jabo/kree/components/SpriteRenderer.class
Binary file not shown.
4 changes: 4 additions & 0 deletions src/dev/jabo/kree/GameObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,8 @@ public void Destroy() {
}
}

public Scene GetParentScene() {
return this.parentScene;
}

}
2 changes: 1 addition & 1 deletion src/dev/jabo/kree/Scene.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public abstract class Scene {

protected Game game;

protected GameObject[] gameObjects = new GameObject[0];
public GameObject[] gameObjects = new GameObject[0];

public Scene(Game game) {
this.game = game;
Expand Down
2 changes: 1 addition & 1 deletion src/dev/jabo/kree/components/Animator.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import dev.jabo.kree.Animation;
import dev.jabo.kree.Component;
import dev.jabo.kree.Debug;

public class Animator extends Component {

Expand All @@ -13,6 +12,7 @@ public class Animator extends Component {

public Animator(Animation[] animationData) {
this.allAnimation = animationData;
this.name = "Animator";
}

@Override
Expand Down
29 changes: 29 additions & 0 deletions src/dev/jabo/kree/components/BoxCollider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package dev.jabo.kree.components;

import java.awt.Graphics;
import java.awt.Rectangle;

import dev.jabo.kree.Component;

public class BoxCollider extends Component {

private Rectangle collider;

public BoxCollider() {
this.name = "Box Collider";
}

@Override
public void Update() {
if(collider == null) {
collider = new Rectangle(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.scale.x, gameObject.transform.scale.y);
}

}

@Override
public void Render(Graphics g) {

}

}
2 changes: 1 addition & 1 deletion src/dev/jabo/kree/components/MeshRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
public class MeshRenderer extends Component {

public MeshRenderer() {

this.name = "Mesh Renderer";
}

@Override
Expand Down
96 changes: 96 additions & 0 deletions src/dev/jabo/kree/components/RigidBody.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package dev.jabo.kree.components;

import java.awt.Graphics;

import dev.jabo.kree.Component;
import dev.jabo.kree.Debug;
import dev.jabo.kree.GameObject;
import dev.jabo.kree.Vector2;

public class RigidBody extends Component {

private float acceleration = 0.25f;
private float xVelocity = 0;
private float yVelocity = 0;
public float mass = 1;

public RigidBody() {
this.name = "RigidBody";
}

@Override
public void Update() {
this.yVelocity += acceleration * mass;

for(GameObject obj : gameObject.GetParentScene().gameObjects) {
if(obj.id == gameObject.id)
continue;
if(obj.GetComponent("Box Collider") != null) {
Vector2 gameObjectPos = gameObject.transform.position;
Vector2 colliderObjectPos = obj.transform.position;
Vector2 gameObjectScale = gameObject.transform.scale;
Vector2 colliderObjectScale = obj.transform.scale;

if(gameObjectPos.x + gameObjectScale.x < colliderObjectPos.x) {
continue;
}

if(gameObjectPos.x > colliderObjectPos.x + colliderObjectScale.x) {
continue;
}

if(gameObjectPos.y > colliderObjectPos.y + colliderObjectScale.y) {
continue;
}

if(gameObjectPos.y + gameObjectScale.y < colliderObjectPos.y) {
continue;
}

if(gameObjectPos.y + gameObjectScale.y > colliderObjectPos.y + 16 && gameObjectPos.y < colliderObjectPos.y + colliderObjectScale.y - 16) {
if(gameObjectPos.x < colliderObjectPos.x) {
// Right
Debug.Log("Right");
gameObjectPos.x = colliderObjectPos.x - gameObjectScale.x - 1;
return;
}
if(gameObjectPos.x + gameObjectScale.x > colliderObjectPos.x) {
// Left
Debug.Log("Left");
gameObjectPos.x = colliderObjectPos.x + colliderObjectScale.x + 1;
return;
}
}

if(gameObjectPos.y + gameObjectScale.y >= colliderObjectPos.y) {
// Bottom
if(gameObjectPos.y + gameObjectScale.y > colliderObjectPos.y) {
if(gameObjectPos.y + gameObjectScale.y < colliderObjectPos.y + 16) {
gameObjectPos.y -= 1;
}
}
// Top
if(gameObjectPos.y + gameObjectScale.y > colliderObjectPos.y) {
if(gameObjectPos.y + gameObjectScale.y > colliderObjectPos.y + colliderObjectScale.y - 16) {
gameObjectPos.y += 1;
}
}
this.yVelocity = 0;
}
}
}
gameObject.transform.Translate(new Vector2((int) xVelocity, (int) yVelocity));
xVelocity = 0;

}

public void AddHorizontalForce(int xForce) {
this.xVelocity = xForce;
}

@Override
public void Render(Graphics g) {

}

}
1 change: 1 addition & 0 deletions src/dev/jabo/kree/components/SpriteRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class SpriteRenderer extends Component {
public SpriteRenderer(String path) {

this.sprite = new Sprite(path);
this.name = "Sprite Renderer";

}

Expand Down

0 comments on commit 0286c37

Please sign in to comment.