-
Notifications
You must be signed in to change notification settings - Fork 5
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
base: master
Are you sure you want to change the base?
Add npcs #51
Changes from 6 commits
d7f165a
6a0dd30
da85f7e
de6ad9e
4dcf021
9789bf2
cdf391a
f8f846b
ed20bfe
df5ae08
8849682
d91d15e
53cc97d
e1b6149
c476101
a71d5d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
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 { | ||
protected Sprite SPRITE; | ||
protected Vector2i SPRITE_SIZE; | ||
protected int TILE_SIZE; | ||
protected Vector2f position; | ||
protected Direction direction; | ||
protected Map map; | ||
|
||
Entity() { | ||
SPRITE = new Sprite(); | ||
SPRITE_SIZE = new Vector2i(0,0); | ||
TILE_SIZE = 0; | ||
} | ||
|
||
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 + | ||
'}'; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.*; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
@@ -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 List<Entity> ENTITIES; | ||
private Player player; | ||
|
||
|
||
|
@@ -37,10 +36,16 @@ class Map implements Drawable { | |
// Calculate the total amount of chunks. | ||
TOTAL_CHUNKS = X_CHUNKS * yChunks; | ||
VERTEX_ARRAYS = new VertexArray[TOTAL_CHUNKS]; | ||
ENTITIES = new ArrayList<Entity>(); | ||
// Load the tiles into the map. | ||
load(); | ||
} | ||
|
||
public void setEntity(Entity entity) { | ||
ENTITIES.add(entity); | ||
entity.setMap(this); | ||
} | ||
|
||
public void setPlayer(Player player) { | ||
this.player = player; | ||
player.setMap(this); | ||
|
@@ -87,8 +92,20 @@ 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 && position.equals(player.getPosition())) { | ||
return true; | ||
} | ||
for (Entity e : ENTITIES) { | ||
if (position.equals(e.getPosition())) { | ||
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() { | ||
|
@@ -152,8 +169,14 @@ private void partition() { | |
} | ||
} | ||
|
||
public void update() { | ||
public void updateEntities() { | ||
for (Entity e : ENTITIES) { | ||
e.update(); | ||
} | ||
} | ||
|
||
public void update() { | ||
updateEntities(); | ||
} | ||
|
||
|
||
|
@@ -190,5 +213,8 @@ public void draw(RenderTarget renderTarget, RenderStates renderStates) { | |
} | ||
} | ||
} | ||
for (Entity e : ENTITIES) { | ||
WINDOW.draw(e); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package io.github.twhscs.game; | ||
|
||
import io.github.twhscs.game.util.Direction; | ||
import io.github.twhscs.game.util.Position; | ||
import org.jsfml.graphics.*; | ||
import org.jsfml.system.Vector2f; | ||
import org.jsfml.system.Vector2i; | ||
|
||
class NonPlayableCharacter extends Entity implements Drawable { | ||
private final int ANIMATION_FRAMES; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code is repeated. |
||
private final int ANIMATION_SPEED; | ||
private final float ANIMATION_STEP; | ||
private int animationFrame; | ||
private boolean animating; | ||
|
||
NonPlayableCharacter(Texture npcTexture, int TILE_SIZE, int ANIMATION_FRAMES, int ANIMATION_SPEED) { | ||
super.SPRITE = new Sprite(npcTexture); | ||
super.SPRITE_SIZE = Vector2i.div(npcTexture.getSize(), ANIMATION_FRAMES); | ||
super.TILE_SIZE = TILE_SIZE; | ||
super.position = new Vector2f(0.0f, 0.0f); | ||
super.direction = Direction.NORTH; | ||
this.ANIMATION_FRAMES = ANIMATION_FRAMES; | ||
this.ANIMATION_SPEED = ANIMATION_SPEED; | ||
this.ANIMATION_STEP = 1.0f / (ANIMATION_FRAMES * ANIMATION_SPEED); | ||
animationFrame = 0; | ||
animating = false; | ||
updateSprite(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "NonPlayableCharacter{" + | ||
"SPRITE_SIZE=" + super.SPRITE_SIZE + | ||
", ANIMATION_FRAMES=" + ANIMATION_FRAMES + | ||
", ANIMATION_SPEED=" + ANIMATION_SPEED + | ||
", ANIMATION_STEP=" + ANIMATION_STEP + | ||
", position=" + super.position + | ||
", direction=" + super.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 void randomlyChooseMove() { | ||
if ((int)(Math.random() * 25) == 1) { | ||
this.move(Direction.getRandomCardinalDirection()); | ||
} | ||
} | ||
|
||
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) { | ||
// Calculate the position to move towards. | ||
Vector2f newPosition = Position.getRelativePosition(position, direction, 1.0f); | ||
// Make sure the new position is valid. | ||
if (map.isValidPosition(newPosition)) { | ||
// If it is valid, update the direction and start moving.. | ||
super.direction = direction; | ||
animating = true; | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void update() { | ||
//This randomly chooses when and where to move | ||
//We will hopefully replace this with path finding or something more advanced | ||
randomlyChooseMove(); | ||
// 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; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,7 @@ class Player implements Drawable { | |
this.ANIMATION_FRAMES = ANIMATION_FRAMES; | ||
this.ANIMATION_SPEED = ANIMATION_SPEED; | ||
// Calculate the animation step by taking the reciprocal of the frame count times speed. | ||
ANIMATION_STEP = 1.0f / (ANIMATION_FRAMES * ANIMATION_SPEED); | ||
this.ANIMATION_STEP = 1.0f / (ANIMATION_FRAMES * ANIMATION_SPEED); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this? |
||
position = new Vector2f(0.0f, 0.0f); | ||
direction = Direction.NORTH; | ||
animationFrame = 0; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
package io.github.twhscs.game.util; | ||
|
||
import java.util.ArrayList; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are these imported? |
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* Enumeration of the eight cardinal and ordinal directions. | ||
*/ | ||
|
@@ -34,4 +38,20 @@ public static Direction getOppositeDirection(Direction direction) { | |
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Gets a random direction. | ||
* @return a random direction. | ||
*/ | ||
public static Direction getRandomDirection() { | ||
return Direction.values()[(int)(Math.random() * 8)]; | ||
} | ||
|
||
/** | ||
* Gets a random cardinal direction. | ||
* @return a random cardinal direction. | ||
*/ | ||
public static Direction getRandomCardinalDirection() { | ||
return Direction.values()[(int)(Math.random() * 4)]; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Entities should be stored in the map
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also do you think we could use a set instead of a list?