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

Add npcs #51

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Game.iml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
<root url="jar://$MODULE_DIR$/lib/jsfml.jar!/" />
<root url="jar://$MODULE_DIR$/lib/jsfml.jar!/" />
<root url="jar://$MODULE_DIR$/lib/jsfml.jar!/" />
<root url="jar://$MODULE_DIR$/lib/jsfml.jar!/" />
<root url="jar://$MODULE_DIR$/lib/jsfml.jar!/" />
</CLASSES>
<JAVADOC>
<root url="http://jsfml.org/javadoc/" />
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/io/github/twhscs/game/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.jsfml.graphics.RenderWindow;
import org.jsfml.graphics.View;
import org.jsfml.system.Clock;
import org.jsfml.system.Vector2f;
import org.jsfml.system.Vector2i;
import org.jsfml.window.Keyboard;
import org.jsfml.window.VideoMode;
Expand Down Expand Up @@ -42,7 +43,13 @@ private App() {
PLAYER = new Player(RESOURCE_MANAGER.getTexture("ryuk"), GAME_VIEW, TILE_SIZE, 4, 2);
MAP = new Map(100, 100, TILE_SIZE, ZOOM, 25, RESOURCE_MANAGER.getTexture("tiles"), WINDOW);
MAP.setPlayer(PLAYER);

for (int i = 0; i < 10; i++) {
MAP.setEntity(
"test" + i,
new NonPlayableCharacter(RESOURCE_MANAGER.getTexture("ryuk"), TILE_SIZE, 4, 2)
);
MAP.getEntity("test" + i).setPosition(new Vector2f(20,20));
}
// Start the main loop.
run();
}
Expand Down Expand Up @@ -118,7 +125,7 @@ private void processInput() {
Vector2i size = event.asSizeEvent().size;
GAME_VIEW.reset(new FloatRect(0.0f, 0.0f, size.x, size.y));
GAME_VIEW.zoom(ZOOM);
PLAYER.updateSprite();
MAP.updateSprites();
break;
case KEY_PRESSED:
switch (event.asKeyEvent().key) {
Expand All @@ -139,15 +146,13 @@ private void processInput() {

private void update() {
MAP.update();
PLAYER.update();
}

private void render(float positionBetweenUpdates) {
WINDOW.setView(GAME_VIEW);
WINDOW.clear();
WINDOW.draw(MAP);
PLAYER.interpolate(positionBetweenUpdates);
WINDOW.draw(PLAYER);
WINDOW.draw(MAP);
WINDOW.display();
}
}
126 changes: 126 additions & 0 deletions src/main/java/io/github/twhscs/game/Character.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package io.github.twhscs.game;

import io.github.twhscs.game.util.Direction;
import io.github.twhscs.game.util.Position;
import org.jsfml.graphics.IntRect;
import org.jsfml.graphics.Texture;
import org.jsfml.system.Vector2f;
import org.jsfml.system.Vector2i;

class Character extends Entity {
protected int ANIMATION_FRAMES;
protected int ANIMATION_SPEED;
protected float ANIMATION_STEP;
protected int animationFrame;
protected boolean animating;

Character(Texture charTexture, int TILE_SIZE, int ANIMATION_FRAMES, int ANIMATION_SPEED) {
super(charTexture, TILE_SIZE);
super.SPRITE_SIZE = Vector2i.div(charTexture.getSize(), ANIMATION_FRAMES);
this.ANIMATION_FRAMES = ANIMATION_FRAMES;
this.ANIMATION_SPEED = ANIMATION_SPEED;
this.ANIMATION_STEP = 1.0f / (ANIMATION_FRAMES * ANIMATION_SPEED);
animationFrame = 0;
animating = false;
}

@Override
public String toString() {
return "Character{" +
"SPRITE_SIZE=" + SPRITE_SIZE +
", ANIMATION_FRAMES=" + ANIMATION_FRAMES +
", ANIMATION_SPEED=" + ANIMATION_SPEED +
", ANIMATION_STEP=" + ANIMATION_STEP +
", position=" + position +
", direction=" + direction +
", animationFrame=" + animationFrame +
", animating=" + animating +
'}';
}

@Override
public void updateSprite() {
// Calculate the sprite position by multiplying the map position by the tile size.
// Subtract half of the sprite width minus the tile size to center it horizontally.
// Subtract the sprite height minus the tile size to center it vertically.
Vector2f spritePosition = new Vector2f(position.x * TILE_SIZE - ((SPRITE_SIZE.x - TILE_SIZE) / 2.0f), position.y * TILE_SIZE - (SPRITE_SIZE.y - TILE_SIZE));
// Round the position to prevent graphical errors.
spritePosition = Position.round(spritePosition);
// Update the sprite's position.
SPRITE.setPosition(spritePosition);
// Apply the appropriate texture based on direction and animation.
SPRITE.setTextureRect(getTextureRect());
}

public boolean nextPositionIsValid(Direction direction) {
// Calculate the position to move towards.
Vector2f newPosition = Position.getRelativePosition(position, direction, 1.0f);
return map.isValidPosition(newPosition);
}

public void move(Direction direction) {
// TODO: Allow for faster movement. (Do not have to wait until current move is finished before initiating next move.)
// Only move the player if they are not already moving.
if (!animating) {
// Make sure the new position is valid.
if (nextPositionIsValid(direction)) {
// If it is valid, update the direction and start moving..
super.direction = direction;
animating = true;
}
}
}

@Override
public void update() {
// Check if the player is moving.
if (animating) {
// Move the player by the animation step.
super.position = Position.getRelativePosition(position, direction, ANIMATION_STEP);
// Check if it is time to stop moving.
if (animationFrame + 1 >= ANIMATION_FRAMES * ANIMATION_SPEED) {
// Reset the animation frame and stop moving.
animationFrame = 0;
animating = false;
// Round the position to prevent float rounding errors.
super.position = Position.round(position);
} else {
// If it is not time to stop, keep going.
animationFrame++;
}
// Update the sprite.
updateSprite();
}
}

private IntRect getTextureRect() {
// Normalize the current frame based on the amount of actual frames.
int adjustedFrame = Math.round((animationFrame * ANIMATION_FRAMES) / (ANIMATION_FRAMES * ANIMATION_SPEED));
// Use math to calculate the player's current texture.
switch (super.direction) {
case NORTH:
return new IntRect(adjustedFrame * super.SPRITE_SIZE.x, 3 * super.SPRITE_SIZE.y, super.SPRITE_SIZE.x, super.SPRITE_SIZE.y);
case SOUTH:
return new IntRect(adjustedFrame * super.SPRITE_SIZE.x, 0, super.SPRITE_SIZE.x, super.SPRITE_SIZE.y);
case WEST:
return new IntRect(adjustedFrame * super.SPRITE_SIZE.x, super.SPRITE_SIZE.y, super.SPRITE_SIZE.x, super.SPRITE_SIZE.y);
case EAST:
return new IntRect(adjustedFrame * super.SPRITE_SIZE.x, 2 * super.SPRITE_SIZE.y, super.SPRITE_SIZE.x, super.SPRITE_SIZE.y);
default:
return new IntRect(0, 0, 0, 0);
}
}

public void interpolate(float positionBetweenUpdates) {
if (animating) {
// Multiply the animation step by the position between frames (0.0f - 1.0f).
float interpolationStep = ANIMATION_STEP * positionBetweenUpdates;
// Get the current position.
Vector2f currentPosition = super.position;
// Temporarily update the position with the interpolation step applied, update the sprite, then revert the position.
super.position = Position.getRelativePosition(super.position, super.direction, interpolationStep);
updateSprite();
super.position = currentPosition;
}
}
}
56 changes: 56 additions & 0 deletions src/main/java/io/github/twhscs/game/Entity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.github.twhscs.game;

import io.github.twhscs.game.util.Direction;
import org.jsfml.graphics.*;
import org.jsfml.system.Vector2f;
import org.jsfml.system.Vector2i;

abstract class Entity implements Drawable, Comparable<Entity> {
protected Sprite SPRITE;
protected Vector2i SPRITE_SIZE;
protected int TILE_SIZE;
protected Vector2f position;
protected Direction direction;
protected Map map;

Entity(Texture entityTexture, int TILE_SIZE) {
this.TILE_SIZE = TILE_SIZE;
SPRITE = new Sprite(entityTexture);
SPRITE_SIZE = new Vector2i(0,0);
position = new Vector2f(0.0f, 0.0f);
direction = Direction.NORTH;
}

public Vector2f getPosition() {
return position;
}

public void setPosition(Vector2f position) {
this.position = position;
}

public void setMap(Map map) {
this.map = map;
}

public void update() {}

public void updateSprite() {}

public void draw(RenderTarget renderTarget, RenderStates renderStates) {
renderTarget.draw(SPRITE);
}

public String toString() {
return "Entity{" +
"SPRITE_SIZE=" + SPRITE_SIZE +
", position=" + position +
", direction=" + direction +
'}';
}

public int compareTo(Entity compareEntity) {
int comparePosition = (int)((Entity) compareEntity).getPosition().y;
return (int)this.getPosition().y - comparePosition;
}
}
53 changes: 48 additions & 5 deletions src/main/java/io/github/twhscs/game/Map.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import org.jsfml.system.Vector2f;
import org.jsfml.system.Vector2i;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.*;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you importing this?


class Map implements Drawable {
private final Vector2i DIMENSIONS;
Expand All @@ -19,6 +17,7 @@ class Map implements Drawable {
private final int TOTAL_CHUNKS;
private final int X_CHUNKS;
private final VertexArray[] VERTEX_ARRAYS;
private final HashMap<String, Entity> ENTITIES;
private Player player;


Expand All @@ -37,12 +36,23 @@ class Map implements Drawable {
// Calculate the total amount of chunks.
TOTAL_CHUNKS = X_CHUNKS * yChunks;
VERTEX_ARRAYS = new VertexArray[TOTAL_CHUNKS];
ENTITIES = new HashMap<String, Entity>();
// Load the tiles into the map.
load();
}

public void setEntity(String name, Entity entity) {
ENTITIES.put(name, entity);
entity.setMap(this);
}

public Entity getEntity(String name) {
return ENTITIES.get(name);
}

public void setPlayer(Player player) {
this.player = player;
ENTITIES.put("player", player);
player.setMap(this);
}

Expand Down Expand Up @@ -87,8 +97,24 @@ private int positionToChunkID(Vector2f position) {
return ((int) position.x / CHUNK_SIZE) + (((int) position.y / CHUNK_SIZE) * X_CHUNKS);
}

public boolean isOccupiedPosition(Vector2f position) {
if (player != null) {
Vector2f playerDifference = Vector2f.sub(position, player.getPosition());
if ((Math.abs(playerDifference.x) < 1.0f && Math.abs(playerDifference.y) < 1.0f)) {
return true;
}
}
for (Entity e : ENTITIES.values()) {
Vector2f entityDifference = Vector2f.sub(position, e.getPosition());
if ((Math.abs(entityDifference.x) < 1.0f && Math.abs(entityDifference.y) < 1.0f)) {
return true;
}
}
return false;
}

public boolean isValidPosition(Vector2f position) {
return position.x >= 0.0f && position.y >= 0.0f && position.x < DIMENSIONS.x && position.y < DIMENSIONS.y;
return position.x >= 0.0f && position.y >= 0.0f && position.x < DIMENSIONS.x && position.y < DIMENSIONS.y && !isOccupiedPosition(position);
}

private void partition() {
Expand Down Expand Up @@ -152,8 +178,20 @@ private void partition() {
}
}

public void update() {
public void updateEntities() {
for (Entity e : ENTITIES.values()) {
e.update();
}
}

public void updateSprites() {
for (Entity e : ENTITIES.values()) {
e.updateSprite();
}
}

public void update() {
updateEntities();
}


Expand Down Expand Up @@ -190,5 +228,10 @@ public void draw(RenderTarget renderTarget, RenderStates renderStates) {
}
}
}
List<Entity> drawList = new ArrayList<Entity>(ENTITIES.values());
Collections.sort(drawList);
for (Entity e : drawList) {
WINDOW.draw(e);
}
}
}
Loading