Skip to content

Commit

Permalink
fixed merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
The-AhmadAA committed Aug 25, 2023
2 parents 7dd76bb + 9c68444 commit 3d2f6cb
Show file tree
Hide file tree
Showing 10 changed files with 217 additions and 5 deletions.
4 changes: 4 additions & 0 deletions source/core/assets/configs/NPCs.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
"health": 100,
"baseAttack": 25,
"spookyFactor": 7
},
"projectile": {
"health": 100,
"baseAttack": 10
}
}

Binary file added source/core/assets/images/projectile.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 added source/core/assets/sounds/projectile firing.mp3
Binary file not shown.
45 changes: 41 additions & 4 deletions source/core/src/main/com/csse3200/game/areas/ForestGameArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.csse3200.game.entities.factories.ProjectileFactory;

/** Forest area for the demo game with trees, a player, and some enemies. */
public class ForestGameArea extends GameArea {
private static final Logger logger = LoggerFactory.getLogger(ForestGameArea.class);
Expand All @@ -27,7 +29,10 @@ public class ForestGameArea extends GameArea {
private static final int NUM_WEAPON_TOWERS = 3;
private static final GridPoint2 PLAYER_SPAWN = new GridPoint2(10, 10);
private static final float WALL_WIDTH = 0.1f;

// Required to load assets before using them
private static final String[] forestTextures = {
"images/projectile.png",
"images/box_boy_leaf.png",
"images/tree.png",
"images/ghost_king.png",
Expand Down Expand Up @@ -59,6 +64,7 @@ public class ForestGameArea extends GameArea {
private final TerrainFactory terrainFactory;

private Entity player;
private Entity ghostking;

/**
* Initialise this ForestGameArea to use the provided TerrainFactory.
Expand All @@ -80,12 +86,17 @@ public void create() {
spawnTerrain();
spawnTrees();
player = spawnPlayer();

spawnGhosts();
spawnGhostKing();
spawnWeaponTower();

ghostking = spawnGhostKing();

// playMusic(); // This music is gonna give me nightmares
playMusic();

spawnProjectile(new Vector2(3f, 3f));
spawnMultiProjectile(new Vector2(3f, 3f));
}

private void displayUI() {
Expand Down Expand Up @@ -152,13 +163,39 @@ private void spawnGhosts() {
}
}

private void spawnGhostKing() {
private Entity spawnGhostKing() {
GridPoint2 minPos = new GridPoint2(0, 0);
GridPoint2 maxPos = terrain.getMapBounds(0).sub(2, 2);

GridPoint2 randomPos = RandomUtils.random(minPos, maxPos);
GridPoint2 randomPos
= RandomUtils.random(minPos, maxPos);
// = new GridPoint2(26, 26);
Entity ghostKing = NPCFactory.createGhostKing(player);
spawnEntityAt(ghostKing, randomPos, true, true);
return ghostKing;
}

/**
* Spawns a projectile currently just in the center of the game
*
* @return a new projectile
*/
private void spawnProjectile(Vector2 speed) {
Entity newProjectile = ProjectileFactory.createProjectile(ghostking, player, new Vector2(100, ghostking.getPosition().x), speed);
newProjectile.setPosition(ghostking.getPosition());
spawnEntity(newProjectile);
}

private void spawnMultiProjectile(Vector2 speed) {
Entity newTopProjectile = ProjectileFactory.createProjectile(ghostking, player, new Vector2(100, player.getPosition().x + 30), speed);
newTopProjectile.setPosition(player.getPosition());
Entity newMiddleProjectile = ProjectileFactory.createProjectile(ghostking, player, new Vector2(100, player.getPosition().x), speed);
newMiddleProjectile.setPosition(player.getPosition());
Entity newBottomProjectile = ProjectileFactory.createProjectile(ghostking, player, new Vector2(100, player.getPosition().x - 30), speed);
newBottomProjectile.setPosition(player.getPosition());

spawnEntity(newTopProjectile);
spawnEntity(newMiddleProjectile);
spawnEntity(newBottomProjectile);
}

private void spawnWeaponTower() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.csse3200.game.components.tasks;

import com.badlogic.gdx.math.Vector2;
import com.csse3200.game.ai.tasks.DefaultTask;
import com.csse3200.game.ai.tasks.PriorityTask;
import com.csse3200.game.entities.Entity;
import com.csse3200.game.physics.PhysicsEngine;
import com.csse3200.game.physics.PhysicsLayer;
import com.csse3200.game.physics.raycast.RaycastHit;
import com.csse3200.game.rendering.DebugRenderer;
import com.csse3200.game.services.ServiceLocator;

/** Chases a target entity until they get too far away or line of sight is lost */
public class TrajectTask extends DefaultTask implements PriorityTask {
private final Entity target;
private final int priority;
private final float viewDistance;
private final PhysicsEngine physics;
private final DebugRenderer debugRenderer;
private final RaycastHit hit = new RaycastHit();
private MovementTask movementTask;
private Vector2 destination;

/**
* @param target The entity to chase.
* @param priority Task priority when chasing (0 when not chasing).
* @param viewDistance Maximum distance from the entity at which chasing can start.
* @param maxChaseDistance Maximum distance from the entity while chasing before giving up.
*/
public TrajectTask(Entity shooter, Entity target, int priority, float viewDistance, Vector2 destination) {
this.target = target;
this.priority = priority;
this.viewDistance = viewDistance;
this.destination = destination;
physics = ServiceLocator.getPhysicsService().getPhysics();
debugRenderer = ServiceLocator.getRenderService().getDebug();
}

@Override
public void start() {
super.start();
movementTask = new MovementTask(destination);
movementTask.create(owner);
movementTask.start();

this.owner.getEntity().getEvents().trigger("trajectStart");
}

@Override
public void update() {
movementTask.setTarget(destination);
movementTask.update();
if (movementTask.getStatus() != Status.ACTIVE) {
movementTask.start();
}
}

@Override
public void stop() {
super.stop();
movementTask.stop();
}

@Override
public int getPriority() {
if (status == Status.ACTIVE) {
return getActivePriority();
}

return getInactivePriority();
}

private float getDistanceToTarget() {
return owner.getEntity().getPosition().dst(destination);
}

private int getActivePriority() {
return priority;
}

private int getInactivePriority() {
float dst = getDistanceToTarget();
if (dst < viewDistance && isTargetVisible()) {
return priority;
}
return -1;
}

private boolean isTargetVisible() {
Vector2 from = owner.getEntity().getCenterPosition();
Vector2 to = target.getCenterPosition();

// If there is an obstacle in the path to the player, not visible.
// if (physics.raycast(from, to, PhysicsLayer.OBSTACLE, hit)) {
// debugRenderer.drawLine(from, hit.point);
// return false;
// }
debugRenderer.drawLine(from, to);
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
*/
public class NPCConfigs {
public BaseEntityConfig ghost = new BaseEntityConfig();
public BaseEntityConfig projectile = new ProjectileConfig();
public GhostKingConfig ghostKing = new GhostKingConfig();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.csse3200.game.entities.configs;

public class ProjectileConfig extends BaseEntityConfig {
public int health = 1;
public int baseAttack = 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.csse3200.game.entities.factories;

import com.csse3200.game.components.TouchAttackComponent;
import com.csse3200.game.components.tasks.TrajectTask;
import com.csse3200.game.ai.tasks.AITaskComponent;
import com.csse3200.game.components.CombatStatsComponent;
import com.csse3200.game.entities.configs.BaseEntityConfig;
import com.csse3200.game.entities.configs.NPCConfigs;
import com.csse3200.game.files.FileLoader;
import com.csse3200.game.entities.Entity;
import com.csse3200.game.rendering.TextureRenderComponent;
import com.csse3200.game.physics.PhysicsLayer;
import com.csse3200.game.physics.components.ColliderComponent;
import com.csse3200.game.physics.components.HitboxComponent;
import com.csse3200.game.physics.components.PhysicsComponent;
import com.csse3200.game.physics.components.PhysicsMovementComponent;
import com.badlogic.gdx.math.Vector2;

/**
* Responsible for creating projectiles within the game
*/
public class ProjectileFactory {

private static final NPCConfigs configs = FileLoader.readClass(NPCConfigs.class, "configs/NPCS.json");

public static Entity createProjectile(Entity shooter, Entity target, Vector2 destination, Vector2 speed) {
BaseEntityConfig config = configs.projectile;

AITaskComponent aiComponent =
new AITaskComponent()
.addTask(new TrajectTask(shooter, target, 10, 100f, destination));

Entity projectile = new Entity()
.addComponent(new TextureRenderComponent("images/projectile.png"))
.addComponent(new PhysicsComponent())
.addComponent(new PhysicsMovementComponent())
.addComponent(new HitboxComponent().setLayer(PhysicsLayer.PROJECTILE))
.addComponent(new ColliderComponent())
.addComponent(new TouchAttackComponent(PhysicsLayer.PLAYER, 1.5f))
.addComponent(new CombatStatsComponent(config.health, config.baseAttack))
.addComponent(aiComponent);

projectile.getComponent(TextureRenderComponent.class).scaleEntity();
projectile.getComponent(PhysicsMovementComponent.class).setSpeed(speed);


// Able to alter the collider component's size in proportion to the Entity's size.
// PhysicsUtils.setScaledCollider(projectile, 0.9f, 0.4f);
return projectile;
}

/**
* Prevents the creation of a Projectile Factory entity being created
*/
private ProjectileFactory() {
throw new IllegalStateException("Instantiating static util class");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class PhysicsLayer {
public static final short OBSTACLE = (1 << 2);
// NPC (Non-Playable Character) colliders
public static final short NPC = (1 << 3);
public static final short PROJECTILE = (1 << 4);
public static final short ALL = ~0;

public static boolean contains(short filterBits, short layer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/** Movement controller for a physics-based entity. */
public class PhysicsMovementComponent extends Component implements MovementController {
private static final Logger logger = LoggerFactory.getLogger(PhysicsMovementComponent.class);
private static final Vector2 maxSpeed = Vector2Utils.ONE;
private static Vector2 maxSpeed = Vector2Utils.ONE;

private PhysicsComponent physicsComponent;
private Vector2 targetPosition;
Expand Down Expand Up @@ -83,4 +83,8 @@ private Vector2 getDirection() {
// Move towards targetPosition based on our current position
return targetPosition.cpy().sub(entity.getPosition()).nor();
}

public void setSpeed(Vector2 speed) {
this.maxSpeed = speed;
}
}

0 comments on commit 3d2f6cb

Please sign in to comment.