Skip to content

Commit

Permalink
Merge pull request #32 from ENG1-Team-29/SaveManagerAttempt3
Browse files Browse the repository at this point in the history
Save manager
  • Loading branch information
uoy-jb2501 authored May 2, 2022
2 parents 311290a + 0e58148 commit 686e023
Show file tree
Hide file tree
Showing 57 changed files with 1,959 additions and 364 deletions.
Binary file removed core/assets/img/entity/boat1.aseprite
Binary file not shown.
Binary file removed core/assets/img/entity/boat1.png
Binary file not shown.
Binary file removed core/assets/img/entity/boat2.aseprite
Binary file not shown.
File renamed without changes
Binary file added core/assets/img/entity/boat_friendly.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified core/assets/img/entity/boat_neutral.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed core/assets/img/entity/cannonball.aseprite
Binary file not shown.
43 changes: 26 additions & 17 deletions core/src/main/java/io/github/annabeths/Boats/AIBoat.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

/**
* @since Assessment 2
* @author James Burnell
* Abstract class that represents Boats with AI-behaviour.
*/
public abstract class AIBoat extends Boat {

Expand All @@ -22,33 +24,31 @@ public abstract class AIBoat extends Boat {

/** How close should the boat be to its destination before setting a new one */
float destinationThreshold = 50f;

/**
* If the boat's rotation is greater than the target angle by this much, start
* rotating
* Constructor for AIBoat
* @param controller an instance of GameController that this boat belongs to
* @param initialPosition the initial position for the boat
* @param texLoc file location of the boat's texture
*/
float angleThreshold = 0.25f;

public AIBoat(GameController controller, Vector2 initialPosition, String texLoc) {
super(controller, initialPosition, texLoc);

this.initialPosition = initialPosition.cpy();
this.state = AIState.IDLE;
}

/**
* Enum describing state of the AI
*/
public enum AIState {
ATTACK, APPROACH, IDLE
}

/** Updates the AI state */
public void updateAIState() {
}

/**
* The steps to take when in {@link AIState#IDLE idle} state
*
* @param delta the time since last update
*/
public void idle(float delta) {
public void idle() {
if (destination == null) {
SetDestination(getNewRandomValidTarget());
} else {
Expand All @@ -66,15 +66,13 @@ public void approach(float delta) {

/**
* The steps to take when in {@link AIState#ATTACK attack} state
*
* @param delta the time since last update
*/
public void attack(float delta) {
}

/**
* Moves the boat towards its current destination
*
* @param delta time since last frame
*/
public void MoveToDestination(float delta) {
Expand All @@ -83,13 +81,17 @@ public void MoveToDestination(float delta) {

/**
* Figure out the angle between the boat and the destination
*
* @return The angle to the destination
*/
public float getAngleToDest() {
return destination == null ? -1 : destination.cpy().sub(getCenter()).angleDeg();
}


/**
* Called once per-frame, updates state of the boat.
* @param delta time since last frame
*/
@Override
public void Update(float delta) {
MoveToDestination(delta);
Expand All @@ -98,6 +100,9 @@ public void Update(float delta) {
updateDestination();
}

/**
* If the AIBoat has reached its target, choose a new random destination.
*/
public void updateDestination() {
if (destination == null || getCenter().dst(destination) <= destinationThreshold) {
Vector2 target = getNewRandomValidTarget();
Expand Down Expand Up @@ -134,9 +139,6 @@ public boolean isDestValid(Vector2 target) {
for (College college : controller.colleges) {
if (Intersector.intersectSegmentPolygon(getCenter(), target,
college.collisionPolygon)) {
// the line has hit a college, return false and set a new destination
// System.out.println(
// "hit: " + college.getCenter() + " | " + getCenter() + " <->" + target);
return false;
}
}
Expand All @@ -153,6 +155,10 @@ void SetDestination(Vector2 target) {
this.destination = target;
}

/**
* Getter method for the AIBoat's destination
* @return the AIBoat's destination - Vector2.
*/
public Vector2 GetDestination() {
return destination;
}
Expand All @@ -164,5 +170,8 @@ public float getDestinationThreshold() {
return destinationThreshold;
}

/**
* abstract method for when the AIBoat shoots a projectile.
*/
abstract void Shoot();
}
36 changes: 31 additions & 5 deletions core/src/main/java/io/github/annabeths/Boats/AttackBoat.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,22 @@ public abstract class AttackBoat extends AIBoat {
*/
public Boat target;

/**
* Constructor for AttackBoat.
* @param controller an instance of GameController that this boat belongs to
* @param initialPosition the initial position for the boat
* @param texLoc file location of the boat's texture
*/
public AttackBoat(GameController controller, Vector2 initialPosition, String texLoc) {
super(controller, initialPosition, texLoc);

this.speed = 75;
this.turnSpeed = 150;
}

/**
* Creates projectiles on either side of the boat, with opposite trajectories.
*/
@Override
public void Shoot() {

Expand All @@ -44,17 +53,23 @@ public void Shoot() {
Projectile projLeft = createProjectile(projectileType, -90, damageMul, 1);
Projectile projRight = createProjectile(projectileType, 90, damageMul, 1);

// Add the projectile to the GameController's physics objects list so it
// Add the projectile to the GameController's physics objects list, so it
// receives updates
controller.NewPhysicsObject(projLeft);
controller.NewPhysicsObject(projRight);
}

/**
* Set the AttackBoat to be destroyed on the start of the next frame.
*/
@Override
void Destroy() {
killOnNextTick = true;
}

/**
* Update the AttackBoat's AI-state based on its distance from its target.
*/
public void updateAIState() {
target = getNearestTarget();
if (target == null) {
Expand All @@ -73,6 +88,10 @@ public void updateAIState() {
}
}

/**
* Called once per-frame, updates state of the boat.
* @param delta time since last frame
*/
@Override
public void Update(float delta) {
if (isDead()) Destroy();
Expand All @@ -88,7 +107,7 @@ public void Update(float delta) {
attack(delta);
break;
case IDLE:
idle(delta);
idle();
break;
default:
break;
Expand All @@ -97,18 +116,26 @@ public void Update(float delta) {
if (destination != null) MoveToDestination(delta);
}

/**
* Approach the Boat's target. Called when the Boat's AIState is {@link AIState#APPROACH approach}
* @param delta the time since last update
*/
public void approach(float delta) {
if (isDestValid(target.getCenter())) {
destination = target.getCenter();
} else {
idle(delta);
idle();
}
}

/**
* Attack the Boat's target. Called when the Boat's AIState is {@link AIState#ATTACK attack}
* @param delta the time since last update
*/
public void attack(float delta) {
float angToTarget = getCenter().sub(target.getCenter()).angleDeg();

// Move at a 90 degree angle to the play to align the boat to shoot
// Move at a 90-degree angle to the play to align the boat to shoot
destination = null;
float adjustedAng = angToTarget - rotation;
adjustedAng = MathHelper.normalizeAngle(adjustedAng);
Expand All @@ -133,7 +160,6 @@ public void attack(float delta) {

/**
* Locates the nearest target the boat should follow/attack
*
* @return The boat to target
*/
public abstract Boat getNearestTarget();
Expand Down
55 changes: 42 additions & 13 deletions core/src/main/java/io/github/annabeths/Boats/Boat.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public abstract class Boat extends PhysicsObject implements IHealth {
protected float maxHP;
protected float speed;
protected float turnSpeed;

protected float shotDelay = 0.5f;
protected float timeSinceLastShot = 0f;

Expand Down Expand Up @@ -51,15 +50,13 @@ public Boat(GameController controller, Vector2 position, String texLoc) {
/**
* Generic move method for boats to move forward by their set speed, and a
* multiplier
*
* @param delta time since last frame
* @param multiplier multiplier to set forward or reverse motion (1 or -1)
*
* @since Assessment 1
*/
void Move(float delta, int multiplier) {
// Convention: 0 degrees means the object is pointing right, positive angles are
// counter clockwise
// counterclockwise
Vector2 oldPos = position.cpy();
position.x += Math.cos(Math.toRadians(rotation)) * speed * delta * multiplier;
position.y += Math.sin(Math.toRadians(rotation)) * speed * delta * multiplier;
Expand Down Expand Up @@ -103,12 +100,6 @@ void Turn(float delta, float multiplier) {
* @author Hector Woods
*/
public void moveTowardsDesiredAngle(float desiredAngle, float delta) {

// Manipulate angle to compensate for [0-360] limitations
// if (rotation <= 90 && desiredAngle >= 270) desiredAngle -= 360;
// if (rotation >= 270 && desiredAngle <= 90) desiredAngle += 360;
// if (rotation > 180 && desiredAngle < 90) desiredAngle += 360;

float angDiff = MathHelper.getAbsDiff2Angles(rotation, desiredAngle);
boolean turnLeft = Math.abs((rotation + angDiff) % 360 - desiredAngle) < 0.05f;

Expand All @@ -119,16 +110,22 @@ public void moveTowardsDesiredAngle(float desiredAngle, float delta) {
Move(delta, 1);
}

/**
* abstract method for when the Boat shoots a projectile.
*/
abstract void Shoot();

/**
* abstract method for when the Boat is destroyed.
*/
abstract void Destroy();

/**
* Place the boat somewhere in global space, use this when spawning boats
*
* @param x the x position
* @param y the y position
*
*
* @since Assessment 1
*/
void SetPosition(float x, float y) {
Expand Down Expand Up @@ -158,26 +155,58 @@ public void Draw(SpriteBatch batch) {
*/
protected Projectile createProjectile(ProjectileData type, float rotationOffset, float dmgMul,
float spdMul) {
boolean isPlayer = this instanceof PlayerBoat || this instanceof FriendlyBoat;
return new Projectile(getCenter(), rotation + rotationOffset, type, isPlayer, dmgMul,
boolean isPlayer = this instanceof PlayerBoat;
boolean isFriendly = this instanceof FriendlyBoat || this instanceof PlayerBoat;
return new Projectile(getCenter(), rotation + rotationOffset, type, isPlayer, isFriendly, dmgMul,
spdMul);
}

/**
* getter method for the Boat's HP
* @return the Boat's HP
*/
@Override
public float getHealth() {
return HP;
}

/**
* setter method for the Boat's HP
* @param health the new HP for the boat
*/
public void setHealth(float health){
this.HP = health;
}
/**
* setter method for the Boat's maximum HP
* @param maxHealth the new max HP for the boat
*/
public void setMaxHealth(float maxHealth){
this.maxHP = maxHealth;
}

/**
* getter method for the Boat's maximum HP
* @return the Boat's MaxHP
*/
@Override
public float getMaxHealth() {
return maxHP;
}

/**
* Deal damage to the boat.
* @param dmg The amount of damage to be dealt.
*/
@Override
public void damage(float dmg) {
HP = MathUtils.clamp(HP - dmg, 0, maxHP);
}

/**
* Whether the boat is dead or not.
* @return Boolean - true if dead
*/
@Override
public boolean isDead() {
return killOnNextTick || IHealth.super.isDead();
Expand Down
Loading

0 comments on commit 686e023

Please sign in to comment.