From bed82c4bfa77dc2a4cf7a19e4285e93a3dfc4d24 Mon Sep 17 00:00:00 2001 From: ALGLAVE Ivan <ivan.alglave8@etu.univ-lorraine.fr> Date: Mon, 28 Dec 2020 19:31:50 +0100 Subject: [PATCH] Added a very rudimentary AI that only goes towards the ball, the further away the fastest --- .../com/mygdx/game/GameScreens/GamePlay.java | 3 ++- core/src/com/mygdx/game/bodies/Ball.java | 6 +++++ core/src/com/mygdx/game/bodies/Player.java | 25 +++++++++++-------- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/core/src/com/mygdx/game/GameScreens/GamePlay.java b/core/src/com/mygdx/game/GameScreens/GamePlay.java index 245d121..0386f60 100644 --- a/core/src/com/mygdx/game/GameScreens/GamePlay.java +++ b/core/src/com/mygdx/game/GameScreens/GamePlay.java @@ -268,7 +268,8 @@ public class GamePlay extends GameScreen } if(pleft.getContactStatus().equals("2")) pleft.setContactStatus(0); if(pright.getContactStatus().equals("2")) pright.setContactStatus(0); - pleft.interact(pressed(Input.Keys.Q), pressed(Input.Keys.D), pressed(Input.Keys.Z), pressed(Input.Keys.S)); + if(isAI) pleft.interact(ball.getPos()); + else pleft.interact(pressed(Input.Keys.Q), pressed(Input.Keys.D), pressed(Input.Keys.Z), pressed(Input.Keys.S)); pright.interact(pressed(Input.Keys.J), pressed(Input.Keys.L), pressed(Input.Keys.I), pressed(Input.Keys.K)); } diff --git a/core/src/com/mygdx/game/bodies/Ball.java b/core/src/com/mygdx/game/bodies/Ball.java index 37a463d..781f5a1 100644 --- a/core/src/com/mygdx/game/bodies/Ball.java +++ b/core/src/com/mygdx/game/bodies/Ball.java @@ -1,5 +1,6 @@ package com.mygdx.game.bodies; +import com.badlogic.gdx.Game; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.SpriteBatch; @@ -55,4 +56,9 @@ public class Ball { tb.setPosition(new Vector2(GameScreen.Width()/2f, GameScreen.Height()/2f)); } + + public Vector2 getPos() + { + return new Vector2(tb.getX(), tb.getY()); + } } diff --git a/core/src/com/mygdx/game/bodies/Player.java b/core/src/com/mygdx/game/bodies/Player.java index 2c8fb5d..5ae06c1 100644 --- a/core/src/com/mygdx/game/bodies/Player.java +++ b/core/src/com/mygdx/game/bodies/Player.java @@ -57,22 +57,25 @@ public class Player public void interact(boolean left, boolean right, boolean up, boolean down) { - if(type == TYPE.AI) - { + int hor = (left ? -250 : 0) + (right ? 250 : 0); + int ver = (down ? -250 : 0) + (up ? 250 : 0); + Vector2 impulse = new Vector2(hor, ver); + tb.applyForce(impulse.scl(Gdx.graphics.getDeltaTime()), true); + } + public void interact(Vector2 delta) + { + if(type != TYPE.AI) + { + tb.applyForce(delta.scl(Gdx.graphics.getDeltaTime()), true); } else { - int hor = (left ? -250 : 0) + (right ? 250 : 0); - int ver = (down ? -250 : 0) + (up ? 250 : 0); - Vector2 impulse = new Vector2(hor, ver); - tb.applyForce(impulse.scl(Gdx.graphics.getDeltaTime()), true); - } - } + delta.x -= tb.getX(); + delta.y -= tb.getY(); - public void interact(Vector2 delta) - { - tb.applyForce(delta.scl(Gdx.graphics.getDeltaTime()), true); + tb.applyForce(delta.scl(Gdx.graphics.getDeltaTime()), true); + } } public void reset(boolean hasScored) -- GitLab