-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b88730f
commit e3b8aa5
Showing
9 changed files
with
242 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
core/src/main/java/com/gdx/game/entities/npc/enemy/EnemyPhysicsComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.gdx.game.entities.npc.enemy; | ||
|
||
import com.gdx.game.entities.Entity; | ||
import com.gdx.game.entities.EntityConfig; | ||
import com.gdx.game.entities.npc.NPCPhysicsComponent; | ||
import com.gdx.game.map.MapManager; | ||
|
||
public class EnemyPhysicsComponent extends NPCPhysicsComponent { | ||
|
||
private static final float followRadius = 75.0f; | ||
|
||
@Override | ||
public void update(Entity entity, MapManager mapMgr, float delta) { | ||
updateBoundingBoxPosition(nextEntityPosition); | ||
|
||
if (calculateDistance(mapMgr) <= followRadius) { | ||
followPlayer(mapMgr, entity, delta); | ||
return; | ||
} | ||
|
||
if (state == Entity.State.IMMOBILE) { | ||
return; | ||
} | ||
|
||
if (!isCollisionWithMapLayer(entity, mapMgr) && !isCollisionWithMapEntities(entity, mapMgr) && state == Entity.State.WALKING) { | ||
setNextPositionToCurrent(entity); | ||
} else { | ||
updateBoundingBoxPosition(currentEntityPosition); | ||
} | ||
calculateNextPosition(delta); | ||
} | ||
|
||
private void followPlayer(MapManager mapMgr, Entity entity, float delta) { | ||
float speed = Float.parseFloat(entity.getEntityConfig().getEntityProperties().get(EntityConfig.EntityProperties.ENTITY_SPEED_POINTS.name())); | ||
|
||
float dx = mapMgr.getPlayer().getCurrentPosition().x - currentEntityPosition.x; | ||
float dy = mapMgr.getPlayer().getCurrentPosition().y - currentEntityPosition.y; | ||
|
||
// Check which axis has the greater distance | ||
if (Math.abs(dx) > Math.abs(dy)) { | ||
nextEntityPosition.x += Math.signum(dx) * speed/3 * delta; | ||
} else { | ||
nextEntityPosition.y += Math.signum(dy) * speed/3 * delta; | ||
} | ||
setNextPositionToCurrent(entity); | ||
updateBoundingBoxPosition(currentEntityPosition); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
core/src/test/java/com/gdx/game/entities/npc/enemy/EnemyPhysicsComponentTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.gdx.game.entities.npc.enemy; | ||
|
||
import com.badlogic.gdx.Gdx; | ||
import com.badlogic.gdx.graphics.GL20; | ||
import com.badlogic.gdx.graphics.OrthographicCamera; | ||
import com.badlogic.gdx.graphics.g2d.SpriteBatch; | ||
import com.badlogic.gdx.graphics.glutils.ShapeRenderer; | ||
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer; | ||
import com.badlogic.gdx.math.Vector2; | ||
import com.badlogic.gdx.utils.Json; | ||
import com.gdx.game.GdxRunner; | ||
import com.gdx.game.component.Component; | ||
import com.gdx.game.entities.Entity; | ||
import com.gdx.game.entities.EntityFactory; | ||
import com.gdx.game.map.Map; | ||
import com.gdx.game.map.MapFactory; | ||
import com.gdx.game.map.MapManager; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.MockedConstruction; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.mockConstruction; | ||
|
||
@ExtendWith(GdxRunner.class) | ||
public class EnemyPhysicsComponentTest { | ||
|
||
private MockedConstruction<SpriteBatch> mockSpriteBatch; | ||
private MockedConstruction<ShapeRenderer> mockShapeRenderer; | ||
|
||
@BeforeEach | ||
void init() { | ||
Gdx.gl = mock(GL20.class); | ||
Gdx.gl20 = mock(GL20.class); | ||
mockSpriteBatch = mockConstruction(SpriteBatch.class); | ||
mockShapeRenderer = mockConstruction(ShapeRenderer.class); | ||
} | ||
|
||
@AfterEach | ||
void end() { | ||
mockSpriteBatch.close(); | ||
mockShapeRenderer.close(); | ||
} | ||
|
||
@Test | ||
public void should_follow_player_when_close_enough() { | ||
Json json = new Json(); | ||
Entity player = EntityFactory.getInstance().getEntity(EntityFactory.EntityType.WARRIOR); | ||
MapManager mapManager = new MapManager(); | ||
mapManager.setCamera(new OrthographicCamera()); | ||
mapManager.setPlayer(player); | ||
mapManager.loadMap(MapFactory.MapType.TOPPLE_ROAD_1); | ||
OrthogonalTiledMapRenderer mapRenderer = new OrthogonalTiledMapRenderer(mapManager.getCurrentTiledMap(), Map.UNIT_SCALE); | ||
Entity enemy = mapManager.getCurrentMapEntities().get(1); | ||
|
||
Vector2 currentEntityPosition = new Vector2(13,14); | ||
player.sendMessage(Component.MESSAGE.INIT_START_POSITION, json.toJson(currentEntityPosition)); | ||
player.update(mapManager, mapRenderer.getBatch(), 1.0f); | ||
|
||
float dy = player.getCurrentPosition().y - enemy.getCurrentPosition().y; | ||
|
||
enemy.update(mapManager, mapRenderer.getBatch(), 1.0f); | ||
|
||
float dy2 = player.getCurrentPosition().y - enemy.getCurrentPosition().y; | ||
assertTrue(Math.abs(dy2) < Math.abs(dy)); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
core/src/test/java/com/gdx/game/entities/player/PlayerPhysicsComponentTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.gdx.game.entities.player; | ||
|
||
import com.badlogic.gdx.Gdx; | ||
import com.badlogic.gdx.graphics.GL20; | ||
import com.badlogic.gdx.graphics.OrthographicCamera; | ||
import com.badlogic.gdx.graphics.g2d.SpriteBatch; | ||
import com.badlogic.gdx.graphics.glutils.ShapeRenderer; | ||
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer; | ||
import com.gdx.game.GdxRunner; | ||
import com.gdx.game.component.Component; | ||
import com.gdx.game.entities.Entity; | ||
import com.gdx.game.entities.EntityFactory; | ||
import com.gdx.game.map.Map; | ||
import com.gdx.game.map.MapFactory; | ||
import com.gdx.game.map.MapManager; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import org.mockito.MockedConstruction; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.mockConstruction; | ||
import static org.mockito.Mockito.spy; | ||
import static org.mockito.Mockito.verify; | ||
|
||
@ExtendWith(GdxRunner.class) | ||
public class PlayerPhysicsComponentTest { | ||
|
||
private MockedConstruction<SpriteBatch> mockSpriteBatch; | ||
private MockedConstruction<ShapeRenderer> mockShapeRenderer; | ||
|
||
@BeforeEach | ||
void init() { | ||
Gdx.gl = mock(GL20.class); | ||
Gdx.gl20 = mock(GL20.class); | ||
mockSpriteBatch = mockConstruction(SpriteBatch.class); | ||
mockShapeRenderer = mockConstruction(ShapeRenderer.class); | ||
} | ||
|
||
@AfterEach | ||
void end() { | ||
mockSpriteBatch.close(); | ||
mockShapeRenderer.close(); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("collisionMapData") | ||
public void testCollisionMapEntities(MapFactory.MapType mapType, Component.MESSAGE message, String entityId) { | ||
Entity player = spy(EntityFactory.getInstance().getEntity(EntityFactory.EntityType.WARRIOR)); | ||
MapManager mapManager = new MapManager(); | ||
mapManager.setCamera(new OrthographicCamera()); | ||
mapManager.setPlayer(player); | ||
mapManager.loadMap(mapType); | ||
OrthogonalTiledMapRenderer mapRenderer = new OrthogonalTiledMapRenderer(mapManager.getCurrentTiledMap(), Map.UNIT_SCALE); | ||
|
||
player.update(mapManager, mapRenderer.getBatch(), 1.0f); | ||
|
||
verify(player).sendMessage(message, entityId); | ||
} | ||
|
||
private static Stream<Arguments> collisionMapData() { | ||
return Stream.of( | ||
Arguments.of(MapFactory.MapType.TOPPLE_ROAD_1, Component.MESSAGE.COLLISION_WITH_FOE, "RABITE"), | ||
Arguments.of(MapFactory.MapType.TOPPLE, Component.MESSAGE.COLLISION_WITH_ENTITY, "TOWN_INNKEEPER") | ||
); | ||
} | ||
} |