Skip to content

Commit

Permalink
feat: entity-based resource system
Browse files Browse the repository at this point in the history
  • Loading branch information
Veradictus committed Sep 18, 2023
1 parent 889687b commit 63c3f45
Show file tree
Hide file tree
Showing 33 changed files with 491 additions and 83 deletions.
27 changes: 24 additions & 3 deletions packages/client/data/sprites.json
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@
{ "id": "player/weapon/bowwillowtwine", "offsetX": -8, "offsetY": -32 },

{ "id": "player/shield/lantern", "offsetY": -20 },
{ "id": "player/shield/lantern2", "offsetY": -20 },
{ "id": "player/shield/lantern2", "offsetY": -21 },
{ "id": "player/shield/lanternarcan", "offsetY": -24 },
{ "id": "player/shield/coppershield", "offsetY": -24 },
{ "id": "player/shield/tinshield", "offsetY": -24 },
Expand Down Expand Up @@ -1166,7 +1166,7 @@
"offsetY": 0
},
{
"id": "objects/sprucetree",
"id": "objects/spruce",
"width": 64,
"height": 80,
"animations": {
Expand All @@ -1178,14 +1178,35 @@
"length": 5,
"row": 1
},
"stump": {
"exhausted": {
"length": 1,
"row": 2
}
},
"offsetX": -24,
"offsetY": -56
},
{
"id": "objects/oak",
"width": 64,
"height": 96,
"animations": {
"idle": {
"length": 1,
"row": 0
},
"shake": {
"length": 1,
"row": 1
},
"exhausted": {
"length": 1,
"row": 2
}
},
"offsetX": -24,
"offsetY": -64
},
{
"id": "mobs/clam",
"width": 32,
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
14 changes: 10 additions & 4 deletions packages/client/src/controllers/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Pet from '../entity/character/pet/pet';
import Player from '../entity/character/player/player';
import Projectile from '../entity/objects/projectile';
import Effect from '../entity/objects/effect';
import Tree from '../entity/objects/tree';
import Tree from '../entity/objects/resource/impl/tree';

import { Modules } from '@kaetram/common/network';

Expand All @@ -19,6 +19,7 @@ import type Character from '../entity/character/character';
import type { PetData } from '@kaetram/common/types/pet';
import type { PlayerData } from '@kaetram/common/network/impl/player';
import type { EntityData, EntityDisplayInfo } from '@kaetram/common/types/entity';
import type { ResourceEntityData } from '@kaetram/common/types/resource';

interface EntitiesCollection {
[instance: string]: Entity;
Expand Down Expand Up @@ -130,7 +131,7 @@ export default class EntitiesController {
}

case Modules.EntityType.Tree: {
entity = this.createTree(info as EntityData);
entity = this.createTree(info as ResourceEntityData);

prefix = 'objects';
break;
Expand Down Expand Up @@ -347,8 +348,13 @@ export default class EntitiesController {
* @returns A new tree object.
*/

private createTree(info: EntityData): Entity {
return new Tree(info.instance);
private createTree(info: ResourceEntityData): Entity {
let tree = new Tree(info.instance);

// Update the state of the tree.
tree.exhausted = info.state === Modules.ResourceState.Depleted;

return tree;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions packages/client/src/controllers/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type Map from '../map/map';
import type Trade from '../menu/trade';
import type Leaderboards from '../menu/leaderboards';
import type Guilds from '../menu/guilds';
import type Tree from '../entity/objects/resource/impl/tree';

interface TargetData {
sprite: Sprite;
Expand Down Expand Up @@ -561,6 +562,8 @@ export default class InputController {
}

case Modules.EntityType.Tree: {
if ((entity as Tree).exhausted) return;

this.setCursor(this.cursors.axe);
this.hovering = Modules.Hovering.Tree;
break;
Expand Down
16 changes: 15 additions & 1 deletion packages/client/src/entity/animation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
interface Frame {
export interface Frame {
index: number;
x: number;
y: number;
Expand Down Expand Up @@ -119,6 +119,20 @@ export default class Animation {
return Date.now() - this.lastTime > this.speed && !this.stopped;
}

/**
* Used by tree objects to select the correct animation frame when trying to use the
* base of the tree or the cut stump.
* @returns The second animation in the row.
*/

public getSecondFrame(): Frame {
return {
index: 1,
x: this.width,
y: this.row * this.height
};
}

/**
* Resets the animation to the first frame.
*/
Expand Down
2 changes: 2 additions & 0 deletions packages/client/src/entity/character/character.ts
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,8 @@ export default class Character extends Entity {
}

public moved(): void {
if (!this.isPlayer()) return;

this.moveCallback?.();
}

Expand Down
15 changes: 10 additions & 5 deletions packages/client/src/entity/character/player/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,14 @@ export default class Handler extends CharacterHandler {
instance: this.character.target?.instance
});

this.game.socket.send(Packets.Target, [
this.getTargetType(),
this.character.target?.instance || ''
]);
let targetType = this.getTargetType();

// Send target packet only if we have a target.
if (targetType !== Opcodes.Target.None)
this.game.socket.send(Packets.Target, [
this.getTargetType(),
this.character.target?.instance || ''
]);

// ---------------------------------------------------------

Expand Down Expand Up @@ -238,7 +242,8 @@ export default class Handler extends CharacterHandler {
return Opcodes.Target.Talk;

// Interaction type for objects.
if (this.character.target.isObject()) return Opcodes.Target.Object;
if (this.character.target.isObject() || this.character.target.isTree())
return Opcodes.Target.Object;

// Interaction for attacking
if (this.character.target.isMob() || (this.character.target.isPlayer() && this.game.pvp))
Expand Down
10 changes: 10 additions & 0 deletions packages/client/src/entity/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,16 @@ export default abstract class Entity {
return this.type === Modules.EntityType.Tree;
}

/**
* Used for unifying multiple resources into one function. Things
* like trees, rocks, and bushes are all considered resources.
* @returns Whether or not the entity is a tree, a rock, or a bush.
*/

public isResource(): boolean {
return this.isTree();
}

/**
* Default implementation for `isModerator()`
* @returns False by default.
Expand Down
27 changes: 27 additions & 0 deletions packages/client/src/entity/objects/resource/impl/tree.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Resource from '../resource';

import { Modules } from '@kaetram/common/network';

import type { Frame } from '../../../animation';
import type Sprite from '../../../sprite';

export default class Tree extends Resource {
public baseFrame!: Frame;
public exhaustedFrame!: Frame;

public constructor(instance: string) {
super(instance, Modules.EntityType.Tree);
}

/**
* Override for the set sprite that computes the base frame and the stump frame.
* @param sprite The sprite that we are setting.
*/

public override setSprite(sprite: Sprite): void {
super.setSprite(sprite);

this.baseFrame = this.sprite.animations.exhausted.frame;
this.exhaustedFrame = this.sprite.animations.exhausted.getSecondFrame();
}
}
58 changes: 58 additions & 0 deletions packages/client/src/entity/objects/resource/resource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import Entity from '../../entity';

import type { Modules } from '@kaetram/common/network';

export default abstract class Resource extends Entity {
public exhausted = false;

public constructor(instance: string, type: Modules.EntityType) {
super(instance, type);
}

/**
* Plays the shake animation for the resource.
*/

public shake(): void {
this.setAnimation('shake', 150, 1, () => {
this.idle();
});
}

/**
* Override for the animation frame to use for the tree.
*/

public override idle(): void {
this.setAnimation(this.exhausted ? 'exhausted' : 'idle', 150, 1, () => {
//
});
}

/**
* Override for the `updateSilhouette` function to bypass drawing any silhouette
* around the resource if it is exhausted.
* @param state The state of the silhouette.
*/

public override updateSilhouette(state?: boolean): void {
if (this.exhausted) {
this.silhouette = false;
return;
}

super.updateSilhouette(state);
}

/**
* Used for a resource to alternate between an exhausted state (just the empty
* resource) or its default idle state.
* @param state The new state of the resource.
*/

public setExhausted(state = false): void {
this.exhausted = state;

this.idle();
}
}
17 changes: 0 additions & 17 deletions packages/client/src/entity/objects/tree.ts

This file was deleted.

14 changes: 14 additions & 0 deletions packages/client/src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import type Entity from './entity/entity';
import type Storage from './utils/storage';
import type Character from './entity/character/character';
import type { TileIgnore } from './utils/pathfinder';
import type Resource from './entity/objects/resource/resource';

export default class Game {
public player: Player;
Expand Down Expand Up @@ -360,6 +361,8 @@ export default class Game {
// Skip pets from the search.
if (entity.isPet()) continue;

if (entity.isResource() && (entity as Resource).exhausted) continue;

let { x, y, sprite } = entity,
{ width, height, offsetX, offsetY } = sprite,
largest = width > height ? width : height,
Expand Down Expand Up @@ -430,4 +433,15 @@ export default class Game {
public isLowPowerMode(): boolean {
return !this.camera.isCentered() && !this.renderer.animateTiles;
}

/**
* Function to consolidate all calls to check whether or not the current
* entity we're dealing with is the main player.
* @param instance The instance we want to check.
* @returns Whether the main player's instance matches the instance provided.
*/

public isMainPlayer(instance: string): boolean {
return this.player.instance === instance;
}
}
5 changes: 3 additions & 2 deletions packages/client/src/map/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,14 +387,15 @@ export default class Map {
* marked as a collision.
* @param x The x grid coordinate.
* @param y The y grid coordinate.
* @param all Whether to include additional collisions.
* @returns Whether the x and y coordinates in the 2D grid are colliding.
*/

public isColliding(x: number, y: number): boolean {
public isColliding(x: number, y: number, all = false): boolean {
if (this.isOutOfBounds(x, y)) return true;
if ((this.data[this.coordToIndex(x, y)] as number) < 1) return true;

return this.grid[y][x] === 1;
return this.grid[y][x] === 1 || (all && this.grid[y][x] === 2);
}

/**
Expand Down
Loading

0 comments on commit 63c3f45

Please sign in to comment.