Skip to content

Commit

Permalink
Sprite rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacLic committed Apr 30, 2021
1 parent 0c2853f commit e7b4048
Show file tree
Hide file tree
Showing 11 changed files with 371 additions and 36 deletions.
Binary file removed android/assets/HexTile.png
Binary file not shown.
Binary file removed android/assets/badlogic.jpg
Binary file not shown.
Binary file added android/assets/sprites.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions android/assets/sprites.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
sprites.png
size: 232, 232
format: RGBA8888
filter: Linear,Linear
repeat: none
swordsman
rotate: false
xy: 0, 0
size: 232, 232
orig: 232, 232
offset: 0, 0
index: -1
44 changes: 32 additions & 12 deletions core/src/main/java/org/strategyGame/MyGdxGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,58 @@

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.ScreenUtils;
import org.strategyGame.ecsStructure.ECSManager;
import org.strategyGame.graphics.GraphicsComponent;
import org.strategyGame.graphics.GraphicsManager;
import org.strategyGame.graphics.TextDisplay;
import org.strategyGame.resources.ResourceType;
import org.strategyGame.resources.TypedResourceAmount;
import org.terasology.gestalt.entitysystem.component.management.ComponentManager;
import org.terasology.gestalt.entitysystem.entity.EntityRef;

public class MyGdxGame extends ApplicationAdapter {
private SpriteBatch batch;
Texture img;

private GraphicsManager graphicsManager;

private final float IDEALIZED_FRAME_LENGTH = 1.0f / 60.0f;
private float elapsedTimeSinceLastUpdate = 0;
private PlayerData playerData;
private ECSManager ecsManager;

private SpriteBatch batch;
private GraphicsManager graphicsManager;

/**
* Sets up the game.
*/
@Override
public void create() {
batch = new SpriteBatch();
img = new Texture("badlogic.jpg");
playerData = new PlayerData();
graphicsManager = new GraphicsManager(batch, playerData);
ecsManager = new ECSManager(new ComponentManager());

batch = new SpriteBatch();
graphicsManager = new GraphicsManager(batch, playerData, ecsManager);

//Entities for testing
GraphicsComponent graphicsComponent = new GraphicsComponent();
graphicsComponent.x = 0;
graphicsComponent.y = 0;
graphicsComponent.spriteName = "swordsman";
ecsManager.createEntity(graphicsComponent);


graphicsComponent = new GraphicsComponent();
graphicsComponent.x = 300;
graphicsComponent.y = 0;
graphicsComponent.spriteName = "swordsman";
graphicsComponent.isFlippedHorizontally = true;
ecsManager.createEntity(graphicsComponent);
}

/**
* Handles the bulk of the gameplay and manages the game loop
* Handles the bulk of the gameplay and manages the game loop.
*/
@Override
public void render() {
ScreenUtils.clear(0.2f, 0.8f, 0.2f, 1);

//Game loop
elapsedTimeSinceLastUpdate += Gdx.graphics.getDeltaTime();
Expand All @@ -45,6 +62,7 @@ public void render() {
update();
elapsedTimeSinceLastUpdate -= IDEALIZED_FRAME_LENGTH;
}

renderUpdatedGame();
}

Expand All @@ -54,6 +72,8 @@ private void update() {
}

private void renderUpdatedGame() {
ScreenUtils.clear(0.2f, 0.8f, 0.2f, 1);

batch.begin();

graphicsManager.render();
Expand All @@ -64,6 +84,6 @@ private void renderUpdatedGame() {
@Override
public void dispose() {
batch.dispose();
img.dispose();
graphicsManager.dispose();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.strategyGame.ecsStructure;

import org.strategyGame.graphics.GraphicalPosition;
import org.strategyGame.graphics.GraphicsComponent;
import org.strategyGame.movement.BoardPosition;
import org.strategyGame.health.Health;
import org.strategyGame.health.DamageSystem;
Expand Down Expand Up @@ -41,7 +41,7 @@ public ECSManager(ComponentManager componentManager) {
//TODO: have this automatically search for Component classes, rather than having to hardcode each
componentStores.add(new ConcurrentComponentStore<>(new ArrayComponentStore<>(componentManager.getType(BoardPosition.class))));
componentStores.add(new ConcurrentComponentStore<>(new ArrayComponentStore<>(componentManager.getType(Health.class))));
componentStores.add(new ConcurrentComponentStore<>(new ArrayComponentStore<>(componentManager.getType(GraphicalPosition.class))));
componentStores.add(new ConcurrentComponentStore<>(new ArrayComponentStore<>(componentManager.getType(GraphicsComponent.class))));

entityManager = new CoreEntityManager(componentStores);

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.strategyGame.graphics;

import org.terasology.gestalt.entitysystem.component.Component;

/**
* The graphics information for an entity. For the position, 0,0 is the bottom-left corner.
*/
public class GraphicsComponent implements Component<GraphicsComponent> {
public int x, y;
public String spriteName;
public boolean isFlippedHorizontally, isFlippedVertically;

@Override
public void copy(GraphicsComponent other) {
this.x = other.x;
this.y = other.y;
this.spriteName = other.spriteName;
this.isFlippedHorizontally = other.isFlippedHorizontally;
this.isFlippedVertically = other.isFlippedVertically;
}
}
83 changes: 77 additions & 6 deletions core/src/main/java/org/strategyGame/graphics/GraphicsManager.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,97 @@
package org.strategyGame.graphics;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import org.strategyGame.PlayerData;
import org.strategyGame.resources.ResourceType;
import org.strategyGame.ecsStructure.ECSManager;
import org.terasology.gestalt.entitysystem.entity.EntityIterator;
import org.terasology.gestalt.entitysystem.entity.EntityRef;

import java.util.HashMap;

/**
* This handles the graphics for the game. Entities with a {@link GraphicsComponent} are automatically rendered, and
* text can be displayed by calling {@code displayString()}.
*/
public class GraphicsManager {

private SpriteBatch batch;
private TextDisplay textDisplay;

private PlayerData playerData;
private ECSManager ecsManager;

public GraphicsManager(SpriteBatch batch, PlayerData playerData) {
private TextureAtlas textureAtlas = new TextureAtlas("sprites.txt");
private HashMap<String, Sprite> sprites;

public GraphicsManager(SpriteBatch batch, PlayerData playerData, ECSManager ecsManager) {
this.batch = batch;
batch.enableBlending();
textDisplay = new TextDisplay(batch);
this.playerData = playerData;
this.ecsManager = ecsManager;

sprites = new HashMap<>();
}

/**
* Renders each entity with a {@link GraphicsComponent}.
*/
public void render() {
textDisplay.displayString(((int) playerData.getStorageAmount(ResourceType.WOOD)) + " wood", Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), 2);
batch.draw(new Texture("HexTile.png"), 100, 10);
EntityIterator iterator = ecsManager.iterate(new GraphicsComponent());
while (iterator.next()) {
renderEntity(iterator.getEntity());
}
}

/**
* Renders a specific entity.
*/
private void renderEntity(EntityRef entity) {
GraphicsComponent graphicsComponent = entity.getComponent(GraphicsComponent.class).get();
Sprite sprite = getSprite(graphicsComponent.spriteName);
if (sprite != null) {
sprite.setPosition(graphicsComponent.x, graphicsComponent.y);
sprite.setFlip(graphicsComponent.isFlippedHorizontally, graphicsComponent.isFlippedVertically);
sprite.draw(batch);
}
}

/**
* Retrieves a sprite instance if one already exists, or creates one if not.
*/
private Sprite getSprite(String spriteName) {
if (!sprites.containsKey(spriteName)) {
Sprite sprite = textureAtlas.createSprite(spriteName);
if (sprite != null) {
sprites.put(spriteName, sprite);
}
return sprite;
}
return sprites.get(spriteName);
}

/**
* Displays a string at the specified horizontal and vertical location.
*/
public void displayString(String string, float horizontalPosition, float verticalPosition) {
textDisplay.displayString(string, horizontalPosition, verticalPosition);
}

/**
* Displays a string at the specified horizontal and vertical location, multiplied in size by the scale.
*
* @param scale the size multiplier
*/
public void displayString(String string, float horizontalPosition, float verticalPosition, float scale) {
textDisplay.displayString(string, horizontalPosition, verticalPosition, scale);
}

/**
* Cleans up assets when the game is closed.
*/
public void dispose() {
textureAtlas.dispose();
}
}
Binary file added raw-assets/swordsman.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit e7b4048

Please sign in to comment.