Skip to content

Commit 91d3f1e

Browse files
committed
deltas
1 parent c594e0f commit 91d3f1e

22 files changed

+45
-24
lines changed

src/main/java/com/game/engine/Manager.java

+4-7
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,15 @@ public static Manager getInstance() {
2525
public void init(){
2626
Manager._collisionSystem = CollisionSystem.getInstance();
2727
}
28-
29-
30-
31-
// Move To renderer system
3228
public void updateCurrentEntities( Batch batch, float delta){
29+
_collisionSystem.checkCollisions();
3330
_player.update(batch, delta);
3431
_rocks.forEach(ent -> ent.update(batch, delta));
3532
}
3633

37-
public void printEntityIds(){
38-
_rocks.forEach(ent -> System.out.println(ent.getEntityId()));
39-
System.out.println(_player.getEntityId());
34+
public void registerComponents(){
35+
_collisionSystem.registerComponent(_player.getPhysicsComponent());
36+
_rocks.forEach(ent -> _collisionSystem.registerComponent(ent.getPhysicsComponent()));
4037
}
4138

4239
/*

src/main/java/com/game/engine/components/GraphicsComponent.java

+1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ public void initDimensions(){
2020
public void rotateBy(float d){
2121
this._sprite.rotate(d);
2222
}
23+
2324
}
2425

src/main/java/com/game/engine/components/MovementComponent.java

-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ public <T extends MovementComponent> void shareCurrentPosition(T mc){
1616
mc.setCurrentPosition(_currentPosition);
1717
}
1818

19-
public <T extends MovementComponent> void shareRotation(T mc){
20-
mc.setCurrentPosition(_currentPosition);
21-
}
22-
2319

2420
/*
2521
* SETTERS

src/main/java/com/game/engine/components/PhysicsComponent.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.badlogic.gdx.math.MathUtils;
44
import com.badlogic.gdx.math.Polygon;
5-
import com.badlogic.gdx.math.Rectangle;
65
import com.badlogic.gdx.math.Vector2;
76
import com.game.engine.entities.Entity;
87

@@ -122,5 +121,7 @@ public void setBoundaryPolygon(int sides){
122121
vertices[2*i+1] = h/2 * MathUtils.sin(angle) + h/2;
123122
}
124123
_boundaryPolygon = new Polygon(vertices);
124+
_boundaryPolygon.setOrigin(0, 0);
125125
}
126+
126127
}

src/main/java/com/game/engine/components/player/PlayerGraphicsComponent.java

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class PlayerGraphicsComponent extends GraphicsComponent {
1515
public PlayerGraphicsComponent(){
1616
this._texture = new Texture(Gdx.files.internal("assets/spaceship.png"));
1717
this._sprite = new Sprite(this._texture);
18+
this._sprite.setOrigin(0, 0);
1819
this._dimensions = new Vector2(this._sprite.getWidth(), this._sprite.getHeight());
1920
}
2021

src/main/java/com/game/engine/components/player/PlayerPhysicsComponent.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ public PlayerPhysicsComponent(){
1717
public void dispose() {
1818

1919
}
20-
20+
2121
@Override
2222
public void update(Entity entity, float delta) {
2323
applyPhysics(delta);
2424
wrapAroundWorld();
2525
shareCurrentPosition(entity.getGraphicsComponent());
26+
_boundaryPolygon.setPosition(_currentPosition.x, _currentPosition.y );
27+
28+
2629
}
2730

2831
}

src/main/java/com/game/engine/components/rock/RockGraphicsComponent.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class RockGraphicsComponent extends GraphicsComponent {
1515
public RockGraphicsComponent(){
1616
this._texture = new Texture(Gdx.files.internal("assets/rock.png"));
1717
this._sprite = new Sprite(this._texture);
18+
this._sprite.setOrigin(0, 0);
1819
}
1920

2021
@Override
@@ -25,7 +26,10 @@ public void dispose() {
2526

2627
@Override
2728
public void update( Entity entity, Batch batch, float delta) {
28-
// TODO Auto-generated method stub
29+
// System.out.println(String.format("""
30+
// Entity ID: %s,
31+
// GraphicsPosition: (%.2f, %.2f)
32+
// """, entity.getEntityId(), _currentPosition.x, _currentPosition.y));
2933
_sprite.setPosition(this._currentPosition.x, this._currentPosition.y);
3034
_sprite.draw(batch);
3135
}

src/main/java/com/game/engine/components/rock/RockPhysicsComponent.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ public void update(Entity entity, float delta) {
2626
applyPhysics(delta);
2727
wrapAroundWorld();
2828
shareCurrentPosition(entity.getGraphicsComponent());
29-
30-
}
29+
_boundaryPolygon.setPosition(_currentPosition.x, _currentPosition.y );
30+
31+
}
3132

3233
}
3334

Original file line numberDiff line numberDiff line change
@@ -1,27 +1,44 @@
11
package com.game.engine.systems;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
5+
36
import com.game.engine.components.Component;
7+
import com.game.engine.components.player.PlayerPhysicsComponent;
8+
import com.game.engine.components.rock.RockPhysicsComponent;
49

5-
public class CollisionSystem implements System{
10+
public class CollisionSystem implements GameSystem{
611

712
public static CollisionSystem collisionSystem;
813

14+
private static List<RockPhysicsComponent> _components = new ArrayList<>();
15+
private static PlayerPhysicsComponent _player;
916
public static CollisionSystem getInstance(){
1017
return new CollisionSystem();
1118
}
1219
@Override
1320
public void registerComponent(Component component) {
14-
// TODO Auto-generated method stub
21+
if(component instanceof PlayerPhysicsComponent)
22+
_player = (PlayerPhysicsComponent)component;
23+
else
24+
_components.add((RockPhysicsComponent)component);
1525
}
1626

1727
@Override
1828
public void removeComponent(Component component) {
19-
// TODO Auto-generated method stub
29+
if(component instanceof PlayerPhysicsComponent)
30+
_player = null;
31+
else
32+
_components.remove(component);
2033
}
2134

22-
@Override
23-
public void notifyComponents(String message) {
24-
// TODO Auto-generated method stub
35+
public void checkCollisions(){
36+
for(RockPhysicsComponent rock : _components){
37+
if(_player._boundaryPolygon.getBoundingRectangle().overlaps(rock._boundaryPolygon.getBoundingRectangle())){
38+
_player.setMotionAngle(rock.getMotionAngle());
39+
}
40+
41+
}
2542
}
2643

2744
}

src/main/java/com/game/engine/systems/System.java src/main/java/com/game/engine/systems/GameSystem.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
import com.game.engine.components.Component;
66

7-
public interface System {
7+
public interface GameSystem {
88
void registerComponent(Component component);
99
void removeComponent(Component component);
10-
void notifyComponents(String message);
1110
}

src/main/java/com/game/screens/MainGameScreen.java

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public void show() {
4747
_manager.setEntityBounds();
4848
_manager.setPlayerPosition(new Vector2(400,300));
4949
_manager.setRocksPositions();
50+
_manager.registerComponents();
5051
_batch = new SpriteBatch();
5152
_space = new Texture(Gdx.files.internal("assets/space.png"));
5253

-62 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
-266 Bytes
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)