Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code smells #264

Merged
merged 2 commits into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
* shows the 'ground' in the game. Enabling/disabling this component will show/hide the terrain.
*/
public class TerrainComponent extends RenderComponent {
private static final Logger logger = LoggerFactory.getLogger(TerrainComponent.class);
private static final int TERRAIN_LAYER = 0;

private final TiledMap tiledMap;
Expand All @@ -33,7 +32,6 @@ public class TerrainComponent extends RenderComponent {
private final TerrainOrientation orientation;
private final float tileSize;
private TiledMapTileLayer.Cell lastHoveredCell = null;
private TiledMapTile originalTile = null;
private TextureRegion originalRegion = null;


Expand Down Expand Up @@ -107,41 +105,6 @@ public int getLayer() {
return TERRAIN_LAYER;
}

// TODO : This is just a visual effect that we might not need in the end but just keeping it here for now
public void colorTile(final int x, final int y) {
final TiledMapTileLayer tileLayer = (TiledMapTileLayer) tiledMap.getLayers().get(0);
final TiledMapTile originalTile = tileLayer.getCell(x, y).getTile();

ResourceService resourceService = ServiceLocator.getResourceService();

// Load all the tiles into an array
final TerrainTile[] terrainTiles = new TerrainTile[7];
for (int i = 0; i < 7; i++) {
Texture texture = resourceService.getAsset("images/GrassTile/grass_tile_" + (i + 1) + ".png", Texture.class);
terrainTiles[i] = new TerrainTile(new TextureRegion(texture));
}

final float interval = 0.2f; // Switch every 0.2 seconds
final float duration = 1.4f; // 7 images * 0.2 seconds each

Timer.schedule(new Timer.Task() {
float timeElapsed = 0.0f;

@Override
public void run() {
timeElapsed += interval;

if (timeElapsed >= duration) {
tileLayer.getCell(x, y).setTile(originalTile); // Reset to original tile after the total duration
this.cancel(); // End the timer task
} else {
int index = (int) (timeElapsed / interval);
tileLayer.getCell(x, y).setTile(terrainTiles[index]);
}
}
}, 0, interval, (int) (duration / interval) - 1); // Scheduling the task
}

/**
* Highlights the tile under the mouse cursor by changing its texture region.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package com.csse3200.game.areas.terrain;

import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapRenderer;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile;
import com.badlogic.gdx.math.GridPoint2;
import com.csse3200.game.components.CameraComponent;
import com.csse3200.game.services.ResourceService;
Expand Down Expand Up @@ -52,14 +50,12 @@ public TerrainComponent createTerrain(TerrainType terrainType) {
ResourceService resourceService = ServiceLocator.getResourceService();
resourceService.loadTextures(new String[]{"images/terrain_use.png"});
resourceService.loadAll();
switch (terrainType) {
case ALL_DEMO:
TextureRegion orthogonal =
new TextureRegion(resourceService.getAsset("images/terrain_use.png", Texture.class));
return createTerrain(1f, orthogonal);
default:
return null;
if (terrainType == TerrainType.ALL_DEMO) {
TextureRegion orthogonal =
new TextureRegion(resourceService.getAsset("images/terrain_use.png", Texture.class));
return createTerrain(1f, orthogonal);
}
return null;
}

/**
Expand All @@ -86,12 +82,10 @@ private TerrainComponent createTerrain(float tileWorldSize, TextureRegion terrai
*/

public TiledMapRenderer createRenderer(TiledMap tiledMap, float tileScale) {
switch (orientation) {
case ORTHOGONAL:
return new OrthogonalTiledMapRenderer(tiledMap, tileScale);
default:
return null;
if (orientation == TerrainComponent.TerrainOrientation.ORTHOGONAL) {
return new OrthogonalTiledMapRenderer(tiledMap, tileScale);
}
return null;
}

/**
Expand All @@ -104,9 +98,9 @@ public TiledMapRenderer createRenderer(TiledMap tiledMap, float tileScale) {
private TiledMap createTiles(GridPoint2 tileSize, TextureRegion terrain) {
TiledMap tiledMap = new TiledMap();

TiledMapTileLayer Layer = new TiledMapTileLayer(20, 6, tileSize.x, tileSize.y);
fillInvisibleTiles(Layer, new GridPoint2(20, 6), terrain);
tiledMap.getLayers().add(Layer);
TiledMapTileLayer layer = new TiledMapTileLayer(20, 6, tileSize.x, tileSize.y);
fillInvisibleTiles(layer, new GridPoint2(20, 6), terrain);
tiledMap.getLayers().add(layer);

return tiledMap;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class DroidCombatTask extends DefaultTask implements PriorityTask {
public enum STATE {
IDLE, UP, DOWN, SHOOT_UP, SHOOT_DOWN, WALK, DIE
}
public STATE towerState = STATE.WALK;
private STATE towerState = STATE.WALK;

/**
* @param priority Task priority when targets are detected (0 when nothing detected). Must be a positive integer.
Expand Down Expand Up @@ -156,7 +156,6 @@ public void updateTowerState() {
owner.getEntity().getEvents().trigger(GO_UP);
towerState = STATE.IDLE;


}
}
case DIE -> {
Expand All @@ -166,13 +165,6 @@ public void updateTowerState() {
}
}
}
/**
* For stopping the running task
*/
@Override
public void stop() {
super.stop();
}

/**
* Returns the current state of the tower.
Expand All @@ -183,6 +175,10 @@ public STATE getState() {
return this.towerState;
}

public void setState(STATE state) {
this.towerState = state;
}

/**
* Returns the current priority of the task.
* @return active priority value if targets detected, inactive priority otherwise
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class TNTTowerCombatTask extends DefaultTask implements PriorityTask {
private final GameTime timeSource;
private long endTime;
private final RaycastHit hit = new RaycastHit();
public boolean readToDelete = false;
private boolean readToDelete = false;

public enum STATE {
IDLE, EXPLODE, REMOVE
Expand Down Expand Up @@ -103,19 +103,8 @@ public void updateTowerState() {
owner.getEntity().getEvents().trigger(DAMAGE);
towerState = STATE.REMOVE;
}
case REMOVE -> {
readToDelete = true;
}
case REMOVE -> readToDelete = true;
}


}
/**
* For stopping the running task
*/
@Override
public void stop() {
super.stop();
}

/**
Expand All @@ -124,7 +113,6 @@ public void stop() {
*/
@Override
public int getPriority() {

if (isReadyToDelete()) {
owner.getEntity().setFlagForDelete(true);
return -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,30 +92,28 @@ void animateDeath() {
void animateDefault() { animator.startAnimation("idle");}


//TODO: For the time being, these items will be positioned here. Next, we should create a component that enables an entity to fire projectiles.

/**
* Fires a projectile upwards from the entity's current position.
*/
void shootUp() {
Entity Projectile = ProjectileFactory.createEffectProjectile(PhysicsLayer.NPC, new Vector2(100,
Entity projectile = ProjectileFactory.createEffectProjectile(PhysicsLayer.NPC, new Vector2(100,
entity.getPosition().y), new Vector2(2,2), ProjectileEffects.SLOW, false);
Projectile.setScale(new Vector2(0.5f,0.5f));
Projectile.setPosition((float) (entity.getPosition().x + 0.2),
projectile.setScale(new Vector2(0.5f,0.5f));
projectile.setPosition((float) (entity.getPosition().x + 0.2),
(float) (entity.getPosition().y + 0.5));
ServiceLocator.getEntityService().register(Projectile);
ServiceLocator.getEntityService().register(projectile);
}

/**
* Fires a projectile downwards from the entity's current position.
*/
void shootDown() {
Entity Projectile = ProjectileFactory.createEffectProjectile(PhysicsLayer.NPC, new Vector2(100,
Entity projectile = ProjectileFactory.createEffectProjectile(PhysicsLayer.NPC, new Vector2(100,
entity.getPosition().y), new Vector2(2,2), ProjectileEffects.SLOW, false);
Projectile.setScale(new Vector2(0.5f,0.5f));
Projectile.setPosition((float) (entity.getPosition().x + 0.2),
(float) (entity.getPosition().y));
ServiceLocator.getEntityService().register(Projectile);
projectile.setScale(new Vector2(0.5f,0.5f));
projectile.setPosition((float) (entity.getPosition().x + 0.2),
(entity.getPosition().y));
ServiceLocator.getEntityService().register(projectile);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ private void applyTNTDamage() {
for (int i = 0; i < allEntities.size; i++) {
Entity otherEntity = allEntities.get(i);

if (entity == otherEntity) continue; // Skip the source entity

Vector2 positionSource = entity.getPosition();
Vector2 positionOther = otherEntity.getPosition();

Expand All @@ -77,7 +75,7 @@ private void applyTNTDamage() {
HitboxComponent otherHitbox = otherEntity.getComponent(HitboxComponent.class);

// Check for null components and log specifics
if (sourceHitbox == null || otherHitbox == null) {
if (sourceHitbox == null || otherHitbox == null || entity == otherEntity) {

continue;
}
Expand Down
Loading