Skip to content

Commit

Permalink
Added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Aarone K authored and Aarone K committed Apr 28, 2019
1 parent e19176e commit 90afa51
Show file tree
Hide file tree
Showing 25 changed files with 141 additions and 21 deletions.
10 changes: 10 additions & 0 deletions src/crypt_looter/BossController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

import java.awt.*;

// Class controls boss attack and movement
public class BossController extends EnemyController {
private ProjectileManager projectileManager;
public BossController(Point spawnPos, BoxManager _boxManager, SoundController _soundController, CharacterModel playerModel, ProjectileManager projectileManager){
super(spawnPos, _boxManager, _soundController, playerModel);
this.projectileManager = projectileManager;

//Increases the health and reduces the speed of the boss
model.maxHealth = 6;
model.health = 6;
speed = 0.03;

//Boss will attack back if you provoke him
aiController.attackDistance = 200;
}

Expand All @@ -20,18 +25,22 @@ public void initView(Point spawnPos){

public void update(){
super.update();
//If the boss has taken damage chase the player
if(model.health != model.maxHealth){
aiController.attackDistance = 10000;
}
}

public void attackDetection(){
//Retrieve direction of attack
Point aiVector = aiController.attackDir();

int attackX = aiVector.x;
int attackY = aiVector.y;

setDirection(attackX, -attackY);

//If the boss can attack then spawn projectiles at radial locations
if(model.attackTimer <= 0 && aiController.canAttack(aiController.attackDistance, model.height)) {
double segmentAngle = 2 * Math.PI / 8;
for(int i =0; i < 8; i++){
Expand All @@ -44,6 +53,7 @@ public void attackDetection(){
model.attackTimer = attackTime;
aiController.attackDistance = 10000;
}else {
//Set hit box to inactive and the attacking bow to fox when not attacking
model.attackBow = false;
attackController.active = false;
model.attackTimer -= deltaTime / 1000;
Expand Down
2 changes: 1 addition & 1 deletion src/crypt_looter/BossView.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public BossView(Rectangle transform, CharacterModel model) {

charTexNormalFilepath = "textures/boss_walkcycle.png";
charTexSlashFilepath = "textures/boss_slash.png";
// No enemy bow textures, but CharacterView requires one so we will reuse the boss slash texture
// No boss bow textures, but CharacterView requires one so we will reuse the boss slash texture
charTexBowFilepath = "textures/boss_slash.png";
charTexHurtFilepath = "textures/boss_hurt.png";

Expand Down
2 changes: 2 additions & 0 deletions src/crypt_looter/BoxController.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public BoxController(CharacterModel model, CharacterView view){
useView = false;
}

//This constructor is used for debugging by drawing the boxController
public BoxController(Rectangle rect, WorldController worldController, int height, boolean addView) {
this.rect = rect;
model = new CharacterModel(rect, height);
Expand Down Expand Up @@ -55,6 +56,7 @@ public void setDeath(boolean death){
model.dead = death;
}

//Object position is set at top left corner, getCenter gets the center of the sprite
public Rectangle getCenter(){
Rectangle center = model.getTransform();
center.setLocation((int) center.getCenterX(), (int) center.getCenterY());
Expand Down
45 changes: 39 additions & 6 deletions src/crypt_looter/BoxManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class BoxManager {
int skinWidth = 2;
int playerHeightOffset = 20;

//Create box manager and create a boxCollider for each tile
public BoxManager(World world, WorldController worldController){
colliders = new BoxController[world.mapSize.width][world.mapSize.height];
this.world = world;
Expand All @@ -28,6 +29,7 @@ public BoxManager(World world, WorldController worldController){
}
}

//Update the box controller of each tile
public void update(){
for(int x = 0; x < world.mapSize.width; x++){
for (int y = 0; y < world.mapSize.height; y++) {
Expand All @@ -36,28 +38,39 @@ public void update(){
}
}

//Used to detect the projectile hitting the environment
public boolean projectileMove(Rectangle arrow, int height){
int tileY = Math.floorMod(((arrow.y + arrow.width / 2) / world.tileSize) , world.mapSize.width);
int tileX = Math.floorMod(((arrow.x + arrow.height / 2) / world.tileSize), world.mapSize.height);
//Get the x and y of the closet tile of the projectile
int tileX = Math.floorMod(((arrow.x + arrow.width / 2) / world.tileSize) , world.mapSize.width);
int tileY = Math.floorMod(((arrow.y + arrow.height / 2) / world.tileSize), world.mapSize.height);

int minX = (tileX - arrow.height / world.tileSize * 2) % world.mapSize.height;
int maxX = (tileX + arrow.height / world.tileSize * 2 + 1) % world.mapSize.height;
int minY = (tileY - arrow.width / world.tileSize * 2 % world.mapSize.width);
int maxY = (tileY + arrow.width / world.tileSize * 2 + 1) % world.mapSize.width;

//Work out the surrounding tiles of the closest tile
int minX = (tileX - arrow.width / world.tileSize * 2) % world.mapSize.width;
int maxX = (tileX + arrow.width / world.tileSize * 2 + 1) % world.mapSize.width;
int minY = (tileY - arrow.height / world.tileSize * 2 % world.mapSize.height);
int maxY = (tileY + arrow.height / world.tileSize * 2 + 1) % world.mapSize.height;

//Make sure the minimum x and minimum y tile
minX = minX < 0 ? 0 : minX;
minY = minY < 0 ? 0 : minY;

//Only check the surrounding tiles
for(int x = minX; x < maxX; x++) {
for (int y = minY; y < maxY; y++) {
//Check if the tile height is the same and has no collisions
//if so ignore this tile
if(height != -1){
if (!world.heightCollisions[height][y][x] && !world.collisions[y][x]) {
continue;
}
}
//Don't collide if it is a ladder
if(height == -1 && !world.collisions[y][x]){
continue;
}

//Return true if the tile and projectile collide
BoxController box = colliders[y][x];
if(box.getRect().intersects(arrow)){
return true;
Expand All @@ -68,11 +81,14 @@ public boolean projectileMove(Rectangle arrow, int height){
}

public Vector2 move(Vector2 velocity, Rectangle character, BoxController entity){
//Get the x and y of the closest tile
int tileY = Math.floorMod(((character.y + character.width / 2) / world.tileSize) , world.mapSize.width);
int tileX = Math.floorMod(((character.x + character.height / 2) / world.tileSize), world.mapSize.height);

//Get the height of a tile and check if it is a death tile
int boxHeight = world.heightMap[tileY][tileX];
entity.setDeath(world.death[tileY][tileX]);
//Set the height of the player to be the height of the tile
entity.setHeight(boxHeight);

int minX = (tileX - character.height / world.tileSize * 2) % world.mapSize.height;
Expand All @@ -90,22 +106,28 @@ public Vector2 move(Vector2 velocity, Rectangle character, BoxController entity)
maxY = world.mapSize.width;
}

//Get the calculate the vertex positions of the player with height offset and skin width
Origins origins = new Origins(character, playerHeightOffset, skinWidth);
for(int x = minX; x < maxX; x++) {
for (int y = minY; y < maxY; y++) {
if(boxHeight != -1){
//Check if the tile height is the same and has no collisions
//if so ignore this tile
if (!world.heightCollisions[boxHeight][y][x] && !world.collisions[y][x]) {
continue;
}
}
//Don't collide if it is a ladder
if(boxHeight == -1 && !world.collisions[y][x]){
continue;
}
BoxController box = colliders[y][x];
//Collide the entity and tile with the given velocity
velocity = collideBoxes(box, velocity, origins);
}
}

//Collide with other entities without a height offset
Origins characterOrigins = new Origins(character, 0, skinWidth);
for(int i= 0; i < entities.size(); i++){
if(entities.get(i) != entity){
Expand All @@ -118,13 +140,15 @@ public Vector2 move(Vector2 velocity, Rectangle character, BoxController entity)


private Vector2 collideBoxes(BoxController box, Vector2 velocity, Origins origins){
//Calculate horizontal collisions
Vector2 horizontalOriginBot = (velocity.x < 0) ? origins.botLeft : origins.botRight;
Vector2 horizontalOriginTop = (velocity.x < 0) ? origins.topLeft : origins.topRight;

if (contains(horizontalOriginBot.x + velocity.x + Math.signum(velocity.x) * skinWidth, horizontalOriginBot.y, box.getRect()) || contains(horizontalOriginTop.x + velocity.x + Math.signum(velocity.x) * skinWidth, horizontalOriginTop.y, box.getRect())) {
velocity.x = 0;
}

//Calculate vertical collisions
Vector2 verticalOriginLeft = (velocity.y < 0) ? origins.topLeft : origins.botLeft;
Vector2 verticalOriginRight = (velocity.y < 0) ? origins.topRight : origins.botRight;

Expand All @@ -135,6 +159,7 @@ private Vector2 collideBoxes(BoxController box, Vector2 velocity, Origins origin
}

public void detectItemCollision(BoxController player){
//Detect if the player has hit any items if so check if it can be triggered then trigger and use item
for(int i = 0; i < itemManager.items.size(); i++) {
if (player.getRect().intersects(itemManager.items.get(i).getRect())) {
if(itemManager.items.get(i).canTrigger()) {
Expand All @@ -145,6 +170,7 @@ public void detectItemCollision(BoxController player){
}
}

//Detect if the player attack attackControllers are overlapping and is active
public boolean detectPlayerAttackCollision(BoxController entity){
if((entity.getHeight() != playerAttacks[0].attackHeight) && !(playerAttacks[0].attackHeight == -1 || entity.getHeight() == -1)){
return false;
Expand All @@ -163,6 +189,7 @@ public boolean detectPlayerAttackCollision(BoxController entity){
return false;
}

// Detect if the enemies hitboxes are overlapping and active
public boolean detectEnemyAttackCollision(BoxController player){
for(int j = 0; j < enemyAttacks.size(); j++) {
AttackController enemyAttack = enemyAttacks.get(j);
Expand All @@ -182,11 +209,14 @@ public boolean detectEnemyAttackCollision(BoxController player){
return false;
}

//Detect if the enemy projectiles are colliding with the player
public boolean detectEnemyProjectileCollision(BoxController boxController){
for (int i= 0; i < projectileManager.projectiles.size(); i++){
//Ignore the original archer for collisions
if(projectileManager.projectiles.get(i).archer == boxController){
continue;
}
//if projectile hit player return true
if(projectileManager.projectiles.get(i).view.getBounds().intersects(boxController.getRect())){
projectileManager.destroyProjectile(projectileManager.projectiles.get(i));
return true;
Expand All @@ -197,9 +227,11 @@ public boolean detectEnemyProjectileCollision(BoxController boxController){

public boolean detectPlayerProjectileCollision(BoxController enemy){
for (int i= 0; i < projectileManager.projectiles.size(); i++){
//Ignore the player for collisions
if(projectileManager.projectiles.get(i).archer != player){
continue;
}
//if projectile hit enemy return true
if (projectileManager.projectiles.get(i).view.getBounds().intersects(enemy.getRect())) {
projectileManager.destroyProjectile(projectileManager.projectiles.get(i));
return true;
Expand All @@ -209,6 +241,7 @@ public boolean detectPlayerProjectileCollision(BoxController enemy){
}


//Return true if a point is inside a rectangle bound
private boolean contains(double x, double y, Rectangle rectangle){
return rectangle.x <= x && x <= rectangle.x + rectangle.width &&
rectangle.y <= y && y <= rectangle.y + rectangle.height;
Expand Down
1 change: 1 addition & 0 deletions src/crypt_looter/CharacterController.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public CharacterController(Point spawnPos, SoundController soundController, BoxM
this.boxManager = boxManager;
}

//Sets model direction based on the attackX and attackY
protected void setDirection(int attackX, int attackY){
if(attackX == 0 && attackY == 1){
model.direction = 0;
Expand Down
3 changes: 3 additions & 0 deletions src/crypt_looter/CharacterModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ public Rectangle getTransform(){
return new Rectangle((int) x, (int) y, baseTransform.width, baseTransform.height);
}

//Moves the baseTransform , x and y based on the x and y movement
public void moveWorld(double deltaX, double deltaY){
x += deltaX;
y += deltaY;
baseTransform.setLocation((int) x, (int)y);
}

//Sets the baseTransform, x and y location
public void setWorld(int setX, int setY){
x = setX;
y = setY;
Expand All @@ -72,6 +74,7 @@ public int getY(){
}


//Decrease health and set dead if less than or equal to 0
public void decreaseHealth(int decreaseAmount) {
if((health - decreaseAmount) <= 0) {
health = 0;
Expand Down
2 changes: 2 additions & 0 deletions src/crypt_looter/Coin.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ public void run() {
this.playerModel = playerModel;
}

//Increase the players score
@Override
public void triggerItem(){
playerModel.increaseScore(1000);
}

//Will always activate when picked up
@Override
public boolean canTrigger(){
return true;
Expand Down
2 changes: 2 additions & 0 deletions src/crypt_looter/Dagger.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ public void run() {
this.playerModel = playerModel;
}

//Set dagger equipped to true to enable to attack
@Override
public void triggerItem(){
playerModel.daggerEquipped = true;
}

//Only trigger if the dagger is not already equipped
@Override
public boolean canTrigger(){
return !playerModel.daggerEquipped;
Expand Down
5 changes: 5 additions & 0 deletions src/crypt_looter/EnemyAIController.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public EnemyAIController(CharacterModel enemy, CharacterModel player){
this.player = player;
}

//Get move vector for the enemy
//returns a vector that points towards the player snapped to the 8 directions
public Vector2 move (int height){
if(!canAttack(attackDistance, height)){
return new Vector2(0, 0);
Expand All @@ -24,6 +26,8 @@ public Vector2 move (int height){
return new Vector2(x, y);
}

//Get direction to face when attacking
//Faces player when attacking
public Point attackDir(){
float x = (player.getX() - enemy.getX());
float y = (player.getY() - enemy.getY());
Expand All @@ -35,6 +39,7 @@ public Point attackDir(){
}
}

//Check if the enemy is within a distance and on the same height as the player
public boolean canAttack(int attackDistance, int height){

if(height == player.height || (height == -1 || player.health == -1)) {
Expand Down
Loading

0 comments on commit 90afa51

Please sign in to comment.