Skip to content

Instantly share code, notes, and snippets.

@ajweeks
Last active January 31, 2016 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajweeks/2d8d6bdb15fbd9ad9e19 to your computer and use it in GitHub Desktop.
Save ajweeks/2d8d6bdb15fbd9ad9e19 to your computer and use it in GitHub Desktop.
Devember Twenty-Fourth

Devember Day 24

So much for taking a day off implementing new features I guess. I added a slight improvement to the collision detection algorithm, which results in the player getting nudged only if they are near the edge of an object. I realize that not everyone wants to see the code, and since the collision detection code is getting quite long I'll upload it for those who want to see it (here), and for those who don't, read on!

I also allowed the player to swing their axe even when not near a tree, because it felt like the game was trying to control you too much otherwise. I also made wider trees take more hits to chop down, although I think all trees have the same width at the moment.

After that I spent a while implementing an entity system. When a tree is chopped down, it now drops a log entity which has a certain life span and a position somewhere around the tree. Upon pickup, a little sound is played and your wood count increases. The sound gets a little annoying, but it's alright I suppose. I'm not sure what the use of wood will be in the game, or paper - which I basically just randomly decided to add to test my CSS. But I definitely like having to pick up the wood yourself rather than just seeing the tree magically disappear. The log entites bounce up and down along a sine wave, mostly so the player realizes they aren't stumps. One further thing I could add to make the animation even better would be to drop each log from a specific height, as if it was popping out of the tree.

Here's what the entities look like currently:

Log entities

I realize that they're a little bit ugly, and they don't quite match the stumps. But I'll call it programmer art, so it's good to stay for now.

That's it for today though, thanks for reading, I'll see you in the next one!

Previous Entry | All Entries | Next Entry

var px = this.pivot.position.x;
var py = this.pivot.position.y;
var xv = 0;
var yv = 0;
var input = false;
if (Keyboard.contains(Keyboard.KEYS.W) || Keyboard.contains(Keyboard.KEYS.UP)) {
yv = this.maxVel * deltaTime;
input = true;
} else if (Keyboard.contains(Keyboard.KEYS.S) || Keyboard.contains(Keyboard.KEYS.DOWN)) {
yv = -this.maxVel * deltaTime;
input = true;
}
if (Keyboard.contains(Keyboard.KEYS.A) || Keyboard.contains(Keyboard.KEYS.LEFT)) {
xv = -this.maxVel * deltaTime;
input = true;
} else if (Keyboard.contains(Keyboard.KEYS.D) || Keyboard.contains(Keyboard.KEYS.RIGHT)) {
xv = this.maxVel * deltaTime;
input = true;
}
if (input === false) {
this.animator.switchAnimation(0);
return;
}
this.pivot.position.x += xv;
this.pivot.position.y += yv;
if (px == this.pivot.position.x && py == this.pivot.position.y) { // we didn't move, possibly walking against a wall or something
this.animator.switchAnimation(0); // should be playing the idle animation
}
this.collideWithEntities();
var nudgeMultiplyer = this.maxVel * 2;
var nudgeThreshold = 3; // How close to the edge of the object does the player have to be to get nudged
var tree = this.level.collides(this.pivot.position.x, this.pivot.position.y, this.width, this.height);
if (tree !== null) { // Collision!
if (this.level.collides(this.pivot.position.x, py, this.width, this.height) === null) { // the new y value is the problem
if (this.pivot.position.x < tree.pivot.position.x) { // check what half of the object we're on
var dist = Math.abs((this.pivot.position.x + this.width / 2) - (tree.pivot.position.x - tree.width / 2));
if (dist < nudgeThreshold) { // only nudge the player if they're near the edge of the object
if (xv <= 0) { // prevent the player from being stationary when holding UP and LEFT at the corner of a tree
this.pivot.position.x -= this.maxVel * deltaTime;
}
}
} else {
var dist = Math.abs((this.pivot.position.x - this.width / 2) - (tree.pivot.position.x + tree.width / 2));
if (dist < nudgeThreshold) { // only nudge the player if they're near the edge of the object
if (xv >= 0) {
this.pivot.position.x += this.maxVel * deltaTime;
}
}
}
this.pivot.position.y = py;
} else if (this.level.collides(px, this.pivot.position.y, this.width, this.height) === null) { // the new x value is the problem
if (this.pivot.position.y < tree.pivot.position.y) {
// NOTE(AJ): At the current time, dist will almost certainly be *always* smaller than nudeDistance,
// since tree trunks have small radiai, but we'll leave the checks in for now
var dist = Math.abs((this.pivot.position.y) - (tree.pivot.position.y + tree.trunkRadius));
if (dist < nudgeThreshold) { // only nudge the player if they're near the edge of the object
if (yv <= 0) { // ensure the player isn't walking the opposite direction
this.pivot.position.y -= this.maxVel * deltaTime;
}
}
} else {
if (this.pivot.position.y > tree.pivot.position.y) {
var dist = Math.abs((this.pivot.position.y) - (tree.pivot.position.y - tree.trunkRadius));
if (dist < nudgeThreshold) { // only nudge the player if they're near the edge of the object
if (yv >= 0) {
this.pivot.position.y += this.maxVel * deltaTime;
}
}
}
}
this.pivot.position.x = px;
}
}
if (this.pivot.position.x - this.width / 2 < -this.level.width / 2) {
this.pivot.position.x = -this.level.width / 2 + this.width / 2;
}
if (this.pivot.position.x + this.width / 2 > this.level.width / 2) {
this.pivot.position.x = this.level.width / 2 - this.width / 2;
}
if (this.pivot.position.y - this.height / 2 > this.level.height) {
this.pivot.position.y = this.level.height + this.height / 2;
}
if (this.pivot.position.y - this.height < 0) { // TODO(AJ): This isn't quite perfect yet, but it'll work for now
this.pivot.position.y = this.height;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment