Skip to content

Commit 08bb093

Browse files
author
Picoseconds
committed
feat: added using of items
added item data and using of items, including healing and placing.
1 parent 59ebe23 commit 08bb093

File tree

8 files changed

+255
-32
lines changed

8 files changed

+255
-32
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package-lock.json
22
node_modules/
33
.vscode/
4-
lib/
4+
lib/
5+
.data/

src/items/Item.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { ItemType } from './UpgradeItems';
2+
3+
enum Resources {
4+
Food,
5+
Wood,
6+
Stone,
7+
Points
8+
}
9+
10+
export default class Item {
11+
constructor(
12+
public type: ItemType,
13+
public requires?: [Resources, number]
14+
) { }
15+
}

src/items/StartingItems.ts

-10
This file was deleted.

src/items/UpgradeItems.ts

+35-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
1+
/* enum StartingItems {
2+
Axe = 1,
3+
Sword = 3,
4+
Polearm = 5,
5+
Bat = 6,
6+
Daggers = 7,
7+
Stick = 8
8+
} */
9+
10+
enum StartingItems {
11+
Apple = 0,
12+
WoodWall = 3,
13+
Spikes = 6,
14+
Windmill = 10
15+
}
16+
117
enum Age3Items {
2-
Cookie = 17,
18+
Cookie = 1,
319
StoneWall = 20
420
}
521

622
enum Age4Items {
7-
PitTrap = 31,
23+
PitTrap = 15,
824
BoostPad = 32
925
}
1026

1127
enum Age5Items {
12-
GreaterSpikes = 23,
28+
GreaterSpikes = 7,
1329
FasterWindmill = 27,
1430
Mine = 29,
1531
Sapling = 30
@@ -43,4 +59,19 @@ enum Age9Items {
4359
PoisonSpikes = 24,
4460
SpinningSpikes = 25,
4561
SpawnPad = 36
46-
}
62+
}
63+
64+
const ItemType = {
65+
...StartingItems,
66+
...Age3Items,
67+
...Age4Items,
68+
...Age5Items,
69+
...Age6Items,
70+
...Age7Items,
71+
...Age8Items,
72+
...Age9Items
73+
};
74+
75+
type ItemType = StartingItems | Age3Items | Age4Items | Age5Items | Age6Items | Age7Items | Age8Items | Age9Items;
76+
77+
export { Age3Items, Age4Items, Age5Items, Age6Items, Age7Items, Age8Items, Age9Items, ItemType }

src/items/items.ts

+28-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import weapons from './weapons.json';
2+
import items from './items.json';
3+
import { ItemType } from './UpgradeItems';
24

35
/**
46
* An enum containing the names of all the items. Saves you the effort of differentiating weapon items and other items
@@ -91,5 +93,29 @@ function getWeaponDamage(item: Weapons) {
9193
return weapon?.dmg || 0;
9294
}
9395

94-
export { StartingItems, StartingItems as Age2Items } from "./StartingItems";
95-
export { PrimaryWeapons, SecondaryWeapons, getHitTime, Weapons, getWeaponAttackDetails, getWeaponDamage };
96+
function getItemCost(item: ItemType) {
97+
return items[item].req;
98+
}
99+
100+
function getPlaceable(item: ItemType) {
101+
return !!items[item].group.place;
102+
}
103+
104+
function getPlaceOffset(item: ItemType) {
105+
return items[item].placeOffset;
106+
}
107+
108+
function getScale(item: ItemType) {
109+
return items[item].scale;
110+
}
111+
112+
function getGameObjID(item: ItemType) {
113+
switch (item) {
114+
case ItemType.WoodWall:
115+
return 3;
116+
}
117+
118+
return 0;
119+
}
120+
121+
export { PrimaryWeapons, SecondaryWeapons, getHitTime, Weapons, getWeaponAttackDetails, getWeaponDamage, getItemCost, getPlaceable, getPlaceOffset, getScale, getGameObjID };

src/moomoo/Game.ts

+88-14
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import WebSocket from "ws";
44
import Client from "./Client";
55
import Player from "./Player";
66
import * as lowDb from 'lowdb';
7-
import { randomPos } from "./util";
7+
import { randomPos, chunk } from "./util";
88
import msgpack from "msgpack-lite";
99
import GameState from "./GameState";
1010
import * as Physics from "./Physics";
@@ -15,7 +15,7 @@ import GameObject from "../gameobjects/GameObject";
1515
import { PacketType } from "../packets/PacketType";
1616
import FileAsync from 'lowdb/adapters/FileAsync';
1717
import { PacketFactory } from "../packets/PacketFactory";
18-
import { getWeaponDamage, getWeaponAttackDetails } from "../items/items";
18+
import { getWeaponDamage, getWeaponAttackDetails, getItemCost, getPlaceable } from "../items/items";
1919
import { gameObjectSizes, GameObjectType } from "../gameobjects/gameobjects";
2020

2121
let currentGame: Game | null = null;
@@ -58,6 +58,12 @@ export default class Game {
5858
process.nextTick(this.update);
5959
}
6060

61+
getNextGameObjectID() {
62+
return this.state.gameObjects.length > 0
63+
? Math.max(...this.state.gameObjects.map((gameObj) => gameObj.id)) + 1
64+
: 0;
65+
}
66+
6167
generateStructures() {
6268
const gameObjectTypes = [GameObjectType.Tree];
6369

@@ -68,13 +74,11 @@ export default class Game {
6874

6975
if (sizes) {
7076
let newGameObject = new GameObject(
71-
this.state.gameObjects.length > 0
72-
? Math.max(...this.state.gameObjects.map((gameObj) => gameObj.id)) + 1
73-
: 0,
77+
this.getNextGameObjectID(),
7478
randomPos(12e3, 12e3),
75-
0,
76-
sizes[Math.floor(Math.random() * sizes.length)],
77-
gameObjectType
79+
0,
80+
sizes[Math.floor(Math.random() * sizes.length)],
81+
gameObjectType
7882
);
7983

8084
for (let gameObject of this.state.gameObjects) {
@@ -166,8 +170,8 @@ export default class Game {
166170
}
167171

168172
kickClient(client: Client, reason: string = "kicked") {
169-
console.log(`Kicked ${client.id}: ${reason}`);
170173
this.clients.splice(this.clients.indexOf(client), 1);
174+
console.log(`Kicked ${client.id}: ${reason}`);
171175

172176
// nothing sketchy, just keeps the reason there using a glitch that allows script execution
173177
client.socket.send(msgpack.encode(["d", [
@@ -176,7 +180,7 @@ export default class Game {
176180

177181
setTimeout(() => {
178182
client.socket.close();
179-
}, 10);
183+
}, 1);
180184
}
181185

182186
async banClient(client: Client) {
@@ -185,6 +189,7 @@ export default class Game {
185189
await (await (await this.db.get("bannedIPs")).push(client.ip)).write();
186190
}
187191

192+
console.log(`Banned ${client.id} with ip ${client.ip}`);
188193
this.kickClient(client, "Banned by a Moderator");
189194
}
190195
}
@@ -350,7 +355,7 @@ export default class Game {
350355
this.state.players.forEach((player) => {
351356
Physics.movePlayer(player, 33);
352357

353-
if (player.isAttacking) {
358+
if (player.isAttacking && player.buildItem == -1) {
354359
if (Date.now() - player.lastHitTime >= player.getWeaponHitTime()) {
355360
let nearbyPlayers = player.getNearbyPlayers(this.state);
356361

@@ -441,11 +446,35 @@ export default class Game {
441446
* @param player the player doing the attacking
442447
*/
443448
normalAttack(player: Player) {
444-
player.isAttacking = true;
445-
446449
if (player.buildItem != -1) {
447-
// TODO: use the item
450+
let item = player.buildItem;
451+
if (player.useItem(item, this.state, this.getNextGameObjectID())) {
452+
if (getPlaceable(item)) {
453+
player.getNearbyPlayers(this.state).forEach(nearbyPlayer => this.sendGameObjects(nearbyPlayer))
454+
this.sendGameObjects(player);
455+
}
456+
457+
let itemCost = getItemCost(item);
458+
let costs = chunk(itemCost, 2);
459+
460+
for (let cost of costs) {
461+
switch (cost[0]) {
462+
case "food":
463+
player.food -= cost[1] as number;
464+
break;
465+
case "wood":
466+
player.wood -= cost[1] as number;
467+
break;
468+
case "stone":
469+
player.stone -= cost[1] as number;
470+
break;
471+
}
472+
}
473+
474+
player.buildItem = -1;
475+
}
448476
} else {
477+
player.isAttacking = true;
449478
}
450479
}
451480

@@ -530,6 +559,11 @@ export default class Game {
530559
newPlayer.dead = false;
531560
newPlayer.health = 100;
532561

562+
newPlayer.food = packet.data[0].moofoll ? 100 : 0;
563+
newPlayer.points = packet.data[0].moofoll ? 100 : 0;
564+
newPlayer.stone = packet.data[0].moofoll ? 100 : 0;
565+
newPlayer.wood = packet.data[0].moofoll ? 100 : 0;
566+
533567
client.socket.send(
534568
packetFactory.serializePacket(
535569
new Packet(PacketType.PLAYER_START, [newPlayer.id])
@@ -672,6 +706,46 @@ export default class Game {
672706
}
673707
}
674708
break;
709+
case PacketType.SELECT_ITEM:
710+
if (client.player) {
711+
let isWeapon = packet.data[1];
712+
713+
if (isWeapon) {
714+
client.player.buildItem = -1;
715+
if (client.player.weapon == packet.data[0]) {
716+
client.player.selectedWeapon = client.player.weapon;
717+
} else if (client.player.secondaryWeapon == packet.data[0]) {
718+
client.player.selectedWeapon = client.player.secondaryWeapon;
719+
}
720+
} else {
721+
let itemCost = getItemCost(packet.data[0]);
722+
let costs = chunk(itemCost, 2);
723+
724+
for (let cost of costs) {
725+
switch (cost[0]) {
726+
case "food":
727+
if (client.player.food < cost[1])
728+
return;
729+
break;
730+
case "wood":
731+
if (client.player.wood < cost[1])
732+
return;
733+
break;
734+
case "stone":
735+
if (client.player.stone < cost[1])
736+
return;
737+
break;
738+
}
739+
}
740+
741+
if (client.player.buildItem == packet.data[0]) {
742+
client.player.buildItem = -1;
743+
} else {
744+
client.player.buildItem = packet.data[0];
745+
}
746+
}
747+
}
748+
break;
675749
}
676750
}
677751
}

0 commit comments

Comments
 (0)