-
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
Open
Bassics
wants to merge
16
commits into
master
Choose a base branch
from
add-npcs
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+363
−138
Open
Add npcs #51
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
d7f165a
Add NonPlayableCharacter class and Entity class
Bassics 6a0dd30
Add Entity functionality with Map class
Bassics da85f7e
Add collision detection
Bassics de6ad9e
Add dumb AI
Bassics 4dcf021
Comment code
Bassics 9789bf2
Eliminate unneeded List
Bassics cdf391a
Implement HashMap instead of a List
Bassics f8f846b
Cut out some repetition
Bassics ed20bfe
Add Character class, which extends Entity, and extend it by Player an…
Bassics df5ae08
Take out "implements Drawable" (Unneeded)
Bassics 8849682
Sort rendering
Bassics d91d15e
Make movement "less retarded"
Bassics 53cc97d
Fix zoom and number of entities
Bassics e1b6149
Fix number of entities
Bassics c476101
Fix collision detection bugs
Bassics a71d5d2
Add Path Finding
Bassics File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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; | ||
} | ||
} | ||
} |
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,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; | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why are you importing this?