Skip to content

Commit

Permalink
Changed Shooting Mechanism
Browse files Browse the repository at this point in the history
Shooting now uses mouse pointer location, instead of firing from 'E'.
  • Loading branch information
r0b-ert committed Feb 21, 2022
1 parent 0cb3bed commit 041dcde
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 15 deletions.
42 changes: 42 additions & 0 deletions core/src/com/mygdx/pirategame/PirateGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.mygdx.pirategame.screen.*;


Expand Down Expand Up @@ -112,6 +116,44 @@ public void changeScreen(int screen) {
}//
}

/**
* Used to scale a location on the screen to a location within game coordinates
*
* @param point The point on the screen
* @param camera The camera that is in charge of scaling
* @return The point translated into in-game coordinates
*/
public static Vector2 getScaledLocation(Vector2 point, OrthographicCamera camera) {
return new Vector2(point.x * (camera.viewportWidth / Gdx.graphics.getWidth()), point.y * (camera.viewportHeight / Gdx.graphics.getHeight()));
}

/**
* Used to get the in game coordinates of the mouse
*
* @param camera The camera that is in charge of scaling
* @return The location of the mouse pointer in in-game coordinates
*/
public static Vector2 getScaledMouseLocation(OrthographicCamera camera) {
int mouseX = Gdx.input.getX();
int mouseY = Gdx.graphics.getHeight() - Gdx.input.getY();
return getScaledLocation(new Vector2(mouseX, mouseY), camera);
}

public static Vector3 mousePositionInWorld(Vector3 v, OrthographicCamera camera) {

v.set(Gdx.input.getX(), Gdx.input.getY(), 0f);
camera.unproject(v);

return v;
}

public static Vector3 worldToScreenPosition(Vector3 v, OrthographicCamera camera) {

camera.project(v);

return v;
}

/**
* Allows the user to interact with the audio options
*
Expand Down
102 changes: 90 additions & 12 deletions core/src/com/mygdx/pirategame/gameobjects/CannonFire.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.MathUtils;
Expand All @@ -26,32 +27,64 @@ public class CannonFire extends Sprite {
private boolean destroyed;
private boolean setToDestroy;
private Body b2body;
private float angle;
private double angle;
private float velocity;
private Vector2 bodyVel;
private Sound fireNoise;
private final Texture texture;
private final Sprite sprite;
private float sourceX, sourceY;
private float x, y;
private Vector2 mouse;

/**
* Instantiates cannon fire
* Determines general cannonball data
* Determines firing sound
*
* @param screen visual data
* @param x x value of origin
* @param y y value of origin
* @param sourceX x value of origin
* @param sourceY y value of origin
* @param targetX x value of the target
* @param targetY y value of the target
* @param body body of origin
* @param velocity velocity of the cannon ball
* @param velocity velocity of the cannonball
*/
public CannonFire(GameScreen screen, float x, float y, Body body, float velocity) {
public CannonFire(GameScreen screen, float targetX, float targetY, Vector2 position, Texture ship, Body body, OrthographicCamera camera, float velocity, Vector2 middleScaled) {
this.velocity = velocity;
this.world = screen.getWorld();
//sets the angle and velocity
bodyVel = body.getLinearVelocity();
angle = body.getAngle();
//angle = body.getAngle();

//sourceX = body.getPosition().x;
//sourceY = body.getPosition().y;

mouse = PirateGame.getScaledMouseLocation(camera);
targetX = mouse.x;
targetY = mouse.y;

sourceX = Gdx.graphics.getWidth() / 2;
sourceY = Gdx.graphics.getHeight() / 2;

x = body.getPosition().x;
y = body.getPosition().y;

System.out.println(sourceX);
System.out.println(sourceY);
System.out.println(targetX);
System.out.println(targetY);
//System.out.println(x);
//System.out.println(y);

// Uses a triangle to calculate the new trajectory
angle = Math.atan2(targetY - sourceY, targetX - sourceX);

//set cannonBall dimensions for the texture
cannonBall = new Texture("cannonBall.png");
setRegion(cannonBall);
this.texture = new Texture("cannonBall.png");
this.sprite = new Sprite(texture);

setRegion(texture);
setBounds(x, y, 10 / PirateGame.PPM, 10 / PirateGame.PPM);
//set collision bounds
defineCannonBall();
Expand All @@ -60,10 +93,15 @@ public CannonFire(GameScreen screen, float x, float y, Body body, float velocity
if (screen.game.getPreferences().isEffectsEnabled()) {
fireNoise.play(screen.game.getPreferences().getEffectsVolume());
}
/*
// sets center of sprite at source coordinates
b2body.getPosition().x = sourceX - (getWidth() / 2);
b2body.getPosition().y = sourceY - (getHeight() / 2);
*/
}

/**
* Defines the existance, direction, shape and size of a cannonball
* Defines the existence, direction, shape and size of a cannonball
*/
public void defineCannonBall() {
//sets the body definitions
Expand All @@ -86,19 +124,35 @@ public void defineCannonBall() {
b2body.createFixture(fDef).setUserData(this);

//Velocity maths
//System.out.println(velocity);
//System.out.println(angle);
/*
float velX = MathUtils.cos(angle) * velocity + bodyVel.x;
float velY = MathUtils.sin(angle) * velocity + bodyVel.y;
*/

//float velX = (float) (Math.cos(angle) * velocity + bodyVel.x);
//float velY = (float) (Math.sin(angle) * velocity + bodyVel.y);
float velX = (float) (Math.cos(angle) * velocity);
float velY = (float) (Math.sin(angle) * velocity);
System.out.println(Math.toDegrees(angle));
/*
System.out.println(velX);
System.out.println(velY);
*/
b2body.applyLinearImpulse(new Vector2(velX, velY), b2body.getWorldCenter(), true);
}

/**
* Updates state with delta time
* Defines range of cannon fire
*
* @param dt Delta time (elapsed time since last game tick)
* @param delta Delta time (elapsed time since last game tick)
*/
public void update(float dt){
stateTime += dt;
public void update(float delta){
stateTime += delta;
//Update position of ball
setPosition(b2body.getPosition().x - getWidth() / 2, b2body.getPosition().y - getHeight() / 2);

Expand All @@ -111,6 +165,30 @@ public void update(float dt){
if(stateTime > 0.98f) {
setToDestroy();
}

/*
//float velocity = 200 * delta;
x += Math.cos(angle) * velocity;
y += Math.sin(angle) * velocity;
setPosition(x, y);
*/

/*
// destroy cannonball if it goes off the screen
// limiting x
if (b2body.getPosition().x <= -getWidth() / 2 || b2body.getPosition().x >= camera.viewportWidth - getWidth() / 2) {
dispose();
}
// limiting y
if (b2body.getPosition().y <= -getHeight() / 2 || b2body.getPosition().y >= camera.viewportHeight - height / 2) {
dispose();
}
*/

}

/**
Expand Down
30 changes: 27 additions & 3 deletions core/src/com/mygdx/pirategame/gameobjects/Player.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.mygdx.pirategame.gameobjects;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.box2d.*;
import com.badlogic.gdx.utils.Array;
import com.mygdx.pirategame.PirateGame;
Expand Down Expand Up @@ -49,7 +53,7 @@ public Player(GameScreen screen) {
}

/**
* Update the position of the player. Also updates any cannon balls the player generates
* Update the position of the player. Also updates any cannonballs the player generates
*
* @param dt Delta Time
*/
Expand Down Expand Up @@ -107,18 +111,38 @@ private void definePlayer() {
/**
* Called when E is pushed. Causes 1 cannon ball to spawn on both sides of the ships wih their relative velocity
*/
public void fire() {
public void fire(Vector2 mouse, OrthographicCamera camera) {
// Fires cannons
/*
cannonBalls.add(new CannonFire(screen, b2body.getPosition().x, b2body.getPosition().y, b2body, 5));
cannonBalls.add(new CannonFire(screen, b2body.getPosition().x, b2body.getPosition().y, b2body, -5));

*/
// Cone fire below
/*cannonBalls.add(new CannonFire(screen, b2body.getPosition().x, b2body.getPosition().y, (float) (b2body.getAngle() - Math.PI / 6), -5, b2body.getLinearVelocity()));
cannonBalls.add(new CannonFire(screen, b2body.getPosition().x, b2body.getPosition().y, (float) (b2body.getAngle() - Math.PI / 6), 5, b2body.getLinearVelocity()));
cannonBalls.add(new CannonFire(screen, b2body.getPosition().x, b2body.getPosition().y, (float) (b2body.getAngle() + Math.PI / 6), -5, b2body.getLinearVelocity()));
cannonBalls.add(new CannonFire(screen, b2body.getPosition().x, b2body.getPosition().y, (float) (b2body.getAngle() + Math.PI / 6), 5, b2body.getLinearVelocity()));
}
*/
/*
// Center of boat sprite
float playerCenterX = b2body.getPosition().x + getTexture().getWidth() / 2;
float playerCenterY = b2body.getPosition().y + getTexture().getHeight() / 2;
*/

//cannonBalls.add(new CannonFire(screen, b2body.getPosition().x, b2body.getPosition().y, b2body, 5));
//cannonBalls.add(new CannonFire(screen, playerCenterX, playerCenterY, scaledMouse.x, scaledMouse.y, b2body, 5));
//cannonBalls.add(new CannonFire(screen, mouse.x, mouse.y, ship, b2body, 5));
Vector3 position = new Vector3(b2body.getPosition().x, b2body.getPosition().y, 0);
Vector3 scaledPosition = PirateGame.worldToScreenPosition(position, camera);
Vector2 positionFire = new Vector2(scaledPosition.x, scaledPosition.y);

Vector3 middle = new Vector3(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2, 0);
Vector3 middleScaled = PirateGame.worldToScreenPosition(middle, camera);
Vector2 middleFire = new Vector2(middleScaled.x, middleScaled.y);

cannonBalls.add(new CannonFire(screen, mouse.x, mouse.y, positionFire, ship, b2body, camera, 5, middleFire));
}

/**
Expand Down
38 changes: 38 additions & 0 deletions core/src/com/mygdx/pirategame/screen/GameScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.box2d.*;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
Expand Down Expand Up @@ -276,10 +277,12 @@ public void handleInput(float dt) {
if (Gdx.input.isKeyPressed(Input.Keys.S)) {
player.b2body.applyLinearImpulse(new Vector2(0, -accel), player.b2body.getWorldCenter(), true);
}
/*
// Cannon fire on 'E'
if (Gdx.input.isKeyJustPressed(Input.Keys.E)) {
player.fire();
}
*/
// Checking if player at max velocity, and keeping them below max
if (player.b2body.getLinearVelocity().x >= maxSpeed) {
player.b2body.applyLinearImpulse(new Vector2(-accel, 0), player.b2body.getWorldCenter(), true);
Expand All @@ -293,6 +296,41 @@ public void handleInput(float dt) {
if (player.b2body.getLinearVelocity().y <= -maxSpeed) {
player.b2body.applyLinearImpulse(new Vector2(0, accel), player.b2body.getWorldCenter(), true);
}
// Firing Code
if (Gdx.input.isButtonJustPressed(Input.Buttons.LEFT)) {

//Vector2 scaledMouse = PirateGame.getScaledMouseLocation(camera);
/*
int mouseX = Gdx.input.getX();
int mouseY = Gdx.graphics.getHeight() - Gdx.input.getY();
Vector3 mouse = new Vector3(mouseX, mouseY, 0);
Vector3 scaledMouse = PirateGame.worldToScreenPosition(mouse, camera);
// Center of boat sprite
//float playerCenterX = getPlayerPos().x + player.getWidth() / 2;
//float playerCenterY = getPlayerPos().y + player.getHeight() / 2;
Vector2 mouseFire = new Vector2(scaledMouse.x, scaledMouse.y);
player.fire(mouseFire);
*/

int mouseX = Gdx.input.getX();
int mouseY = Gdx.graphics.getHeight() - Gdx.input.getY();
Vector2 mouse = new Vector2(mouseX, mouseY);

player.fire(mouse, camera);

/*
int mouseX = Gdx.input.getX();
int mouseY = Gdx.graphics.getHeight() - Gdx.input.getY();
Vector3 mouse = new Vector3(mouseX, mouseY, 0);
Vector3 scaledMouse = PirateGame.mousePositionInWorld(mouse, camera);
Vector2 mouseFire = new Vector2(scaledMouse.x, scaledMouse.y);
player.fire(mouseFire, camera);
*/


}
}
if (Gdx.input.isKeyJustPressed(Input.Keys.ESCAPE)) {
if(gameStatus == GAME_PAUSED) {
Expand Down

0 comments on commit 041dcde

Please sign in to comment.