Skip to content

Commit 2c2762a

Browse files
committed
Merge branch 'inventory'
2 parents 4f94ab8 + e6f8677 commit 2c2762a

24 files changed

+654
-194
lines changed

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<head>
33
<meta charset="utf-8"/>
44
<title> ASCII </title>
5+
<link rel="stylesheet" type="text/css" href="styles.css">
56
</head>
67
<body>
78
<script src="bundle.js"></script>

src/Actions/Action.ts

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ export enum ActionDirection {
55
Up,
66
Down,
77
Left,
8-
Right
8+
Right,
9+
10+
UpLeft,
11+
UpRight,
12+
DownLeft,
13+
DownRight
914
}
1015

1116
export abstract class Action {
@@ -14,4 +19,49 @@ export abstract class Action {
1419
this.actor = actor;
1520
}
1621
abstract perform(world: World): void;
22+
23+
public static DirectionToCoords(actorX: number, actorY: number, dir: ActionDirection) {
24+
let posX = actorX;
25+
let posY = actorY;
26+
27+
switch (dir) {
28+
case ActionDirection.Up:
29+
posY -= 1;
30+
break;
31+
32+
case ActionDirection.Down:
33+
posY += 1;
34+
break;
35+
36+
case ActionDirection.Left:
37+
posX -= 1;
38+
break;
39+
40+
case ActionDirection.Right:
41+
posX += 1;
42+
break;
43+
44+
case ActionDirection.UpLeft:
45+
posX -= 1;
46+
posY -= 1;
47+
break;
48+
49+
case ActionDirection.UpRight:
50+
posX += 1;
51+
posY -= 1;
52+
break;
53+
54+
case ActionDirection.DownLeft:
55+
posX -= 1;
56+
posY += 1;
57+
break;
58+
59+
case ActionDirection.DownRight:
60+
posX += 1;
61+
posY += 1;
62+
break;
63+
}
64+
65+
return [posX, posY];
66+
}
1767
}

src/Actions/PickupItemAction.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Actor } from "../Actors/Actor";
22
import { Action } from "./Action";
33
import { World } from "../world";
4+
import { Floor } from "../Rooms/Environment";
45
import { Item } from "../Items/Item";
56
import { GameObject } from "../GameObject";
67

@@ -10,12 +11,13 @@ export class PickupItemAction extends Action {
1011
}
1112

1213
perform(world: World) {
13-
if (world.getActiveRoom().getObject(this.actor.x, this.actor.y) instanceof Item) {
14-
15-
16-
let item = (<Item>world.getActiveRoom().getObject(this.actor.x, this.actor.y));
14+
let onObject = world.getActiveRoom().getObject(this.actor.x, this.actor.y);
15+
if (onObject instanceof Floor && (<Floor>onObject).getObjects().length > 0) {
16+
17+
let item = <Item>(<Floor>onObject).getObjects().shift();
1718

1819
this.actor.inventory.push(item);
20+
world.appendMessage("You pick up the " + item.name + ".");
1921

2022
// If the actor had nothing else in their inventory, equip this item
2123
if (this.actor.inventory.length == 1) this.actor.equipt = item;

src/Actions/WalkAction.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ export class WalkAction extends Action {
2929
if (this.dir == ActionDirection.Left) toObject = room.getObject(this.actor.x - 1, this.actor.y);
3030
if (this.dir == ActionDirection.Right) toObject = room.getObject(this.actor.x + 1, this.actor.y);
3131

32+
if (this.dir == ActionDirection.UpLeft) toObject = room.getObject(this.actor.x - 1, this.actor. y - 1);
33+
if (this.dir == ActionDirection.UpRight) toObject = room.getObject(this.actor.x + 1, this.actor. y - 1);
34+
if (this.dir == ActionDirection.DownLeft) toObject = room.getObject(this.actor.x - 1, this.actor. y + 1);
35+
if (this.dir == ActionDirection.DownRight) toObject = room.getObject(this.actor.x + 1, this.actor. y + 1);
36+
3237

3338
if (!toObject.collides) {
3439
if (fromObject instanceof Floor) {
@@ -40,6 +45,25 @@ export class WalkAction extends Action {
4045
if (this.dir == ActionDirection.Down) this.actor.y = this.actor.y + 1;
4146
if (this.dir == ActionDirection.Left) this.actor.x = this.actor.x - 1;
4247
if (this.dir == ActionDirection.Right) this.actor.x = this.actor.x + 1;
48+
49+
// diagonals:
50+
if (this.dir == ActionDirection.UpLeft) {
51+
this.actor.x -= 1
52+
this.actor.y -= 1
53+
}
54+
if (this.dir == ActionDirection.UpRight) {
55+
this.actor.x += 1;
56+
this.actor.y -= 1;
57+
}
58+
59+
if (this.dir == ActionDirection.DownLeft){
60+
this.actor.x -= 1;
61+
this.actor.y +=1;
62+
}
63+
if (this.dir == ActionDirection.DownRight) {
64+
this.actor.x += 1;
65+
this.actor.y += 1;
66+
}
4367

4468
if (toObject instanceof Floor) {
4569
(<Floor>room.objects[this.actor.x][this.actor.y]).setOccupation(this.actor);

src/Actors/Actor.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ import { Item } from '../Items/Item';
66

77
// essentially, "Actors" are GameObjects that are allowed to takeTurns and have names.
88

9+
export class Corpse extends GameObject {
10+
11+
constructor(actor: Actor) {
12+
super(actor.x, actor.y, new Tile('%', actor.getTile().fg, actor.getTile().bg));
13+
this.name = actor.name + ' corpse';
14+
}
15+
16+
}
17+
918
export abstract class Actor extends GameObject {
1019

1120
public nextAction: Action;
@@ -24,4 +33,6 @@ export abstract class Actor extends GameObject {
2433
}
2534

2635
abstract takeTurn(world: World): void;
36+
37+
abstract death(world: World): GameObject[];
2738
}

src/Actors/Mob.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Actor } from './Actor';
1+
import { Actor, Corpse } from './Actor';
22
import { ActionDirection } from '../Actions/Action';
33
import { WalkAction } from '../Actions/WalkAction';
44
import { WaitAction } from '../Actions/WaitAction';
@@ -8,6 +8,8 @@ import { World } from '../world';
88

99
export class Mob extends Actor {
1010

11+
dead: boolean = false;
12+
1113
constructor(name: string, x: number, y: number, tile: Tile) {
1214
super(name, x, y, tile);
1315
this.nextAction = new WaitAction(this);
@@ -17,19 +19,35 @@ export class Mob extends Actor {
1719

1820
takeTurn(world: World) {
1921

22+
if (this.dead) return;
2023
if (this.debug) console.log('DEBUG:', this.name, "taking turn.", this.x, this.y);
2124

2225
let actionList: Action[] = [
2326
new WaitAction(this),
2427
new WalkAction(ActionDirection.Up, this),
2528
new WalkAction(ActionDirection.Down, this),
2629
new WalkAction(ActionDirection.Left, this),
27-
new WalkAction(ActionDirection.Right, this)
30+
new WalkAction(ActionDirection.Right, this),
31+
new WalkAction(ActionDirection.UpRight, this),
32+
new WalkAction(ActionDirection.UpLeft, this),
33+
new WalkAction(ActionDirection.DownRight, this),
34+
new WalkAction(ActionDirection.DownLeft, this)
35+
2836
];
2937

3038
let r = Math.floor(Math.random() * (actionList.length));
3139

3240
actionList[r].perform(world);
33-
// actionList[0].perform();
41+
}
42+
43+
death(world: World) {
44+
45+
this.dead = true;
46+
let objects = [];
47+
objects.push(new Corpse(this));
48+
49+
// TODO: randomly select things from this mob's inventory to be dropped
50+
51+
return objects;
3452
}
3553
}

src/Actors/Player.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@ import { Tile } from '../tile';
88
import { World } from '../world';
99

1010
export class Player extends Actor {
11+
12+
visionDistance: number;
1113

1214
constructor(x0: number, y0: number, tile: Tile) {
1315
super("Player", x0, y0, tile);
1416
this.nextAction = new WaitAction(this);
17+
this.visionDistance = 2;
1518
}
1619

1720
receiveKeyInput(key: string) {
21+
22+
// directional movement
1823
if (key == 'w') {
1924
this.nextAction = new WalkAction(ActionDirection.Up, this);
2025
}
@@ -28,20 +33,41 @@ export class Player extends Actor {
2833
this.nextAction = new WalkAction(ActionDirection.Right, this);
2934
}
3035

31-
else if (key == 'P') {
36+
// diagonal movement:
37+
else if (key == 'q') {
38+
this.nextAction = new WalkAction(ActionDirection.UpLeft, this);
39+
}
40+
else if (key == 'e') {
41+
this.nextAction = new WalkAction(ActionDirection.UpRight, this);
42+
}
43+
else if (key == 'z') {
44+
this.nextAction = new WalkAction(ActionDirection.DownLeft, this);
45+
}
46+
else if (key == 'x') {
47+
this.nextAction = new WalkAction(ActionDirection.DownRight, this);
48+
}
49+
50+
else if (key == ',' || key == 'P') {
3251
this.nextAction = new PickupItemAction(this);
3352
}
3453

3554
else if (key == '>') {
3655
this.nextAction = new DoorAction(this);
3756
}
3857

39-
else {
58+
else if (key == 'j') {
4059
this.nextAction = new WaitAction(this);
4160
}
4261
}
4362

4463
takeTurn(world: World) {
4564
this.nextAction.perform(world);
4665
}
66+
67+
death(world: World) {
68+
69+
// TODO: somehow trigger the end of the game
70+
71+
return [];
72+
}
4773
}

src/GameObject.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { Tile } from "./tile";
22

33
export class GameObject {
4+
5+
public name: string = "Default GameObject Name";
6+
47
public x: number;
58
public y: number;
69
private tile: Tile;

src/Items/Shovel.ts

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,10 @@ class ShovelAction extends Action {
1717
perform(world: World) {
1818
let room = world.getActiveRoom();
1919

20-
let toPosX = null;
21-
let toPosY = null;
22-
if (this.dir == ActionDirection.Up) {
23-
toPosX = this.actor.x;
24-
toPosY = this.actor.y - 1;
25-
}
26-
if (this.dir == ActionDirection.Down) {
27-
toPosX = this.actor.x;
28-
toPosY = this.actor.y + 1;
29-
}
30-
31-
if (this.dir == ActionDirection.Left) {
32-
toPosX = this.actor.x - 1;
33-
toPosY = this.actor.y;
34-
}
35-
if (this.dir == ActionDirection.Right) {
36-
toPosX = this.actor.x + 1;
37-
toPosY = this.actor.y;
38-
}
20+
let toCoords = Action.DirectionToCoords(this.actor.x, this.actor.y, this.dir);
21+
22+
let toPosX = toCoords[0];
23+
let toPosY = toCoords[1];
3924

4025
if (room.objects[toPosX][toPosY] instanceof Wall) {
4126
// Add the wall to the actors inventory
@@ -54,6 +39,7 @@ export class Shovel extends Item {
5439

5540
constructor(x: number, y: number, tile: Tile) {
5641
super(x, y, tile);
42+
this.name = "shovel";
5743
}
5844

5945
use(actor: Actor, dir: ActionDirection, world: World) {

0 commit comments

Comments
 (0)