Finished the collision detection.
This commit is contained in:
@@ -109,7 +109,8 @@ public class PongEntityInitializer extends EntityInitializerBase {
|
||||
if(!entitiesCreated)
|
||||
throw new IllegalStateException("Entities have not been created before setting assets.");
|
||||
|
||||
Vector2 randomVector = new Vector2().set(Vector2.X).setAngle(MathUtils.random(0, 360));
|
||||
Vector2 randomVector = new Vector2().set(Vector2.X).setAngle(MathUtils.random(-60, 60));
|
||||
int randomSign = MathUtils.random(-1, 1) >= 0 ? 1 : -1;
|
||||
TextureAtlas atlas = loader.getAsset("data/gfx/textures/pong_atlas.atlas", TextureAtlas.class);
|
||||
Texture bckg = loader.getAsset("data/gfx/textures/bckg.png", Texture.class);
|
||||
|
||||
@@ -121,7 +122,7 @@ public class PongEntityInitializer extends EntityInitializerBase {
|
||||
|
||||
Mappers.spriteMapper.get(ball).sprite = atlas.createSprite("ball");
|
||||
Mappers.positionMapper.get(ball).setXY(-(Mappers.spriteMapper.get(ball).sprite.getWidth() / 2), -(Mappers.spriteMapper.get(ball).sprite.getHeight() / 2));
|
||||
Mappers.velocityMapper.get(ball).setXY(randomVector.x * 475.0f, randomVector.y * 475.0f);
|
||||
Mappers.velocityMapper.get(ball).setXY(randomVector.x * 475.0f * randomSign, randomVector.y * 475.0f * randomSign);
|
||||
Mappers.bboxMapper.get(ball).bbox.set(Mappers.spriteMapper.get(ball).sprite.getBoundingRectangle());
|
||||
Mappers.soundMapper.get(ball).path = "data/sfx/BounceYoFrankie.ogg";
|
||||
|
||||
|
@@ -22,6 +22,7 @@ import com.badlogic.ashley.core.Family;
|
||||
import com.badlogic.ashley.systems.IteratingSystem;
|
||||
import com.badlogic.ashley.utils.ImmutableArray;
|
||||
import com.badlogic.gdx.math.MathUtils;
|
||||
import com.badlogic.gdx.math.Rectangle;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import com.gamejolt.mikykr5.poukemon.ProjectConstants;
|
||||
import com.gamejolt.mikykr5.poukemon.ecs.components.BoundingBoxComponent;
|
||||
@@ -42,7 +43,6 @@ public class CollisionDetectionSystem extends IteratingSystem {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public CollisionDetectionSystem(Engine engine){
|
||||
//super(Family.getFor(PositionComponent.class, BoundingBoxComponent.class, VelocityComponent.class));
|
||||
super(Family.getFor(ComponentType.getBitsFor(PositionComponent.class, BoundingBoxComponent.class, VelocityComponent.class), ComponentType.getBitsFor(), ComponentType.getBitsFor(PlayerComponent.class)));
|
||||
|
||||
collidables = engine.getEntitiesFor(Family.getFor(BoundingBoxComponent.class));
|
||||
@@ -63,8 +63,6 @@ public class CollisionDetectionSystem extends IteratingSystem {
|
||||
// Check if this entity is within the screen.
|
||||
// If the entity collides with any of the borders then bounce or score as needed.
|
||||
if(position.x < screenLeftBorder){
|
||||
position.x = screenLeftBorder;
|
||||
velocity.vx = velocity.vx < 0.0f ? -velocity.vx : velocity.vx;
|
||||
resetEntity(entity);
|
||||
|
||||
message = new InterSystemMessage(ScoringSystem.class.getCanonicalName());
|
||||
@@ -77,8 +75,6 @@ public class CollisionDetectionSystem extends IteratingSystem {
|
||||
}
|
||||
|
||||
if(position.x + bounds.bbox.getWidth() >= screenRightBorder){
|
||||
position.x = screenRightBorder - bounds.bbox.getWidth();
|
||||
velocity.vx = velocity.vx > 0.0f ? -velocity.vx : velocity.vx;
|
||||
resetEntity(entity);
|
||||
|
||||
message = new InterSystemMessage(ScoringSystem.class.getCanonicalName());
|
||||
@@ -113,9 +109,39 @@ public class CollisionDetectionSystem extends IteratingSystem {
|
||||
}
|
||||
|
||||
for(int i = 0; i < collidables.size(); i++){
|
||||
BoundingBoxComponent collidable;
|
||||
PositionComponent colPosition;
|
||||
|
||||
if(collidables.get(i).getIndex() == entity.getIndex()){
|
||||
continue;
|
||||
}else{
|
||||
collidable = Mappers.bboxMapper.get(collidables.get(i));
|
||||
colPosition = Mappers.positionMapper.get(collidables.get(i));
|
||||
if(colPosition == null)
|
||||
continue;
|
||||
|
||||
bounds.bbox.setPosition(position.x, position.y);
|
||||
collidable.bbox.setPosition(colPosition.x, colPosition.y);
|
||||
|
||||
if(collidesLeft(bounds.bbox, collidable.bbox)){
|
||||
velocity.vx = velocity.vx < 0.0f ? -velocity.vx : velocity.vx;
|
||||
accelerate(velocity);
|
||||
|
||||
if(sound != null){
|
||||
message = new InterSystemMessage(SoundSystem.class.getCanonicalName());
|
||||
message.data.put("PLAY", sound.path);
|
||||
InterSystemMessagingQueue.pushMessage(message);
|
||||
}
|
||||
}else if(collidesRight(bounds.bbox, collidable.bbox)){
|
||||
velocity.vx = velocity.vx > 0.0f ? -velocity.vx : velocity.vx;
|
||||
accelerate(velocity);
|
||||
|
||||
if(sound != null){
|
||||
message = new InterSystemMessage(SoundSystem.class.getCanonicalName());
|
||||
message.data.put("PLAY", sound.path);
|
||||
InterSystemMessagingQueue.pushMessage(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,13 +155,35 @@ public class CollisionDetectionSystem extends IteratingSystem {
|
||||
velocity.vy *= 1.03f;
|
||||
}
|
||||
|
||||
private void resetEntity(Entity entity){
|
||||
PositionComponent position = Mappers.positionMapper.get(entity);
|
||||
SpriteComponent sprite = Mappers.spriteMapper.get(entity);
|
||||
VelocityComponent velocity = Mappers.velocityMapper.get(entity);
|
||||
private boolean collidesLeft(Rectangle a, Rectangle b){
|
||||
float leftBottomCornerY, leftTopCornerY, leftCenterY;
|
||||
|
||||
randomVector.set(Vector2.X).setAngle(MathUtils.random(0, 360));
|
||||
velocity.setXY(randomVector.x * -475, randomVector.y * 475);
|
||||
leftBottomCornerY = a.y;
|
||||
leftTopCornerY = a.y + a.height;
|
||||
leftCenterY = a.y + (a.height / 2);
|
||||
|
||||
return b.contains(a.x, leftBottomCornerY) || b.contains(a.x, leftTopCornerY) || b.contains(a.x, leftCenterY);
|
||||
}
|
||||
|
||||
private boolean collidesRight(Rectangle a, Rectangle b){
|
||||
float x, rightBottomCornerY, rightTopCornerY, rightCenterY;
|
||||
|
||||
x = a.x + a.width;
|
||||
rightBottomCornerY = a.y;
|
||||
rightTopCornerY = a.y + a.height;
|
||||
rightCenterY = a.y + (a.height / 2);
|
||||
|
||||
return b.contains(x, rightBottomCornerY) || b.contains(x, rightTopCornerY) || b.contains(x, rightCenterY);
|
||||
}
|
||||
|
||||
private void resetEntity(Entity entity){
|
||||
PositionComponent position = Mappers.positionMapper.get(entity);
|
||||
SpriteComponent sprite = Mappers.spriteMapper.get(entity);
|
||||
VelocityComponent velocity = Mappers.velocityMapper.get(entity);
|
||||
int randomSign = MathUtils.random(-1, 1) >= 0 ? 1 : -1;
|
||||
|
||||
randomVector.set(Vector2.X).setAngle(MathUtils.random(-60, 60));
|
||||
velocity.setXY(randomVector.x * -475 * randomSign, randomVector.y * 475 * randomSign);
|
||||
|
||||
if(position != null){
|
||||
if(sprite != null){
|
||||
|
@@ -39,11 +39,11 @@ public class ComputerPlayerPositioningSystem extends IteratingSystem {
|
||||
|
||||
@Override
|
||||
public void processEntity(Entity entity, float deltaTime) {
|
||||
InterSystemMessage message;
|
||||
VelocityComponent velocity = Mappers.velocityMapper.get(entity);
|
||||
PositionComponent position = Mappers.positionMapper.get(entity);
|
||||
PlayerComponent player = Mappers.playerMapper.get(entity);
|
||||
BoundingBoxComponent bounds = Mappers.bboxMapper.get(entity);
|
||||
InterSystemMessage message;
|
||||
VelocityComponent velocity = Mappers.velocityMapper.get(entity);
|
||||
PositionComponent position = Mappers.positionMapper.get(entity);
|
||||
PlayerComponent player = Mappers.playerMapper.get(entity);
|
||||
BoundingBoxComponent bounds = Mappers.bboxMapper.get(entity);
|
||||
|
||||
if(player.id == PlayerComponent.COMPUTER_PLAYER){
|
||||
while((message = InterSystemMessagingQueue.popMessage(ComputerPlayerPositioningSystem.class.getCanonicalName())) != null){
|
||||
|
@@ -189,12 +189,7 @@ public class InGameState extends BaseState implements AssetsLoadedListener{
|
||||
|
||||
private float convertWorldYToFrameBufferY(float y){
|
||||
Vector3 vec3 = new Vector3(0, y - Math.abs(fbBounds.y - (-(h/2.0f))), 0);
|
||||
|
||||
Gdx.app.log("IN_GAME", "Y before: " + Float.toString(vec3.y));
|
||||
|
||||
fbCamera.unproject(vec3, 0, 0, w, h / oldRatio);
|
||||
|
||||
Gdx.app.log("IN_GAME", "Y after: " + Float.toString(vec3.y));
|
||||
fbCamera.unproject(vec3, -(w / 2.0f), -((h / oldRatio) / 2.0f), w, h / oldRatio);
|
||||
|
||||
return vec3.y;
|
||||
}
|
||||
|
Reference in New Issue
Block a user