Browse Source

Fixed monsters spawned from search

master
Armen138 9 months ago
parent
commit
386b7023a3
12 changed files with 80 additions and 38 deletions
  1. +7
    -0
      data/locations/dark_hallway.yml
  2. +5
    -0
      data/locations/strange_room.yml
  3. +45
    -13
      game/game.js
  4. +3
    -2
      game/items.js
  5. +2
    -2
      game/monsters.js
  6. +3
    -4
      game/world.js
  7. +1
    -0
      mobile/components/equipment.js
  8. +1
    -1
      mobile/components/history.js
  9. +1
    -0
      mobile/components/inventory.js
  10. +1
    -1
      mobile/components/items.js
  11. +10
    -0
      mobile/components/location.js
  12. +1
    -15
      mobile/components/monsters.js

+ 7
- 0
data/locations/dark_hallway.yml View File

@@ -1,6 +1,13 @@
title: When your eyes adjust to the dim light
description: You see something moving in the far corner. It looks like it is coming toward you.
name: dark hallway
search:
items:
- coin
monsters:
- rat
prints: Behind a wooden box, another rat jumps out of a dark corner! It appears to be guarding a golden coin!
description: A wooden box sits in the far corner.
monsters:
- rat
items:


+ 5
- 0
data/locations/strange_room.yml View File

@@ -6,6 +6,11 @@ description: >

A door that looks like it leads outside is clumsily barricaded with scraps of wood and rusty nails. One of the nails has fallen on the floor.
name: strange room
search:
items:
- a shiny key
description: Some wood scraps lay disturbed on the floor.
prints: After kicking some scraps around the floor, you spot something shiny. It's a key! What could this open?
items:
- rusty nail
- orange


+ 45
- 13
game/game.js View File

@@ -6,6 +6,7 @@ import World from './world.js';
import Character from './character.js';
import Items from './items.js';
import Events from './events.js';
import Monster from './monster.js';
// import Monsters from './monsters.js';

const healthScale = [
@@ -20,7 +21,7 @@ class Game extends Events {
super();
this.time = 0;
this.renderer = renderer;
// this.monsters = new Monsters(loader);
// this.world.monsters = new Monsters(loader);
this.items = new Items(renderer, loader);
loader.get('data/world.yml').then(worldConfig => {
loader.get(`data/locations/${worldConfig.spawn.replace(/ /g, '_')}.yml`).then(location => {
@@ -30,6 +31,7 @@ class Game extends Events {
loader.get('data/messages.yml').then(messageData => {
this.messages = new Messages(messageData);
this.character.messages = this.messages;
this.world.messages = this.messages;
loader.get('data/countdown.yml').then(countdown => {
this.countdown = countdown;
this.emit('ready');
@@ -53,6 +55,7 @@ class Game extends Events {
use: this.use.bind(this),
go: this.go.bind(this),
equip: this.equip.bind(this),
search: this.search.bind(this),
unequip: this.unequip.bind(this),
attack: this.attack.bind(this),
};
@@ -148,7 +151,7 @@ class Game extends Events {
}
this.emit('monsters', this.world.location.spawned);
this.emit('stats', this.character);
callback();
if (callback) { callback(); }
}

equip(itemName, callback) {
@@ -181,7 +184,7 @@ class Game extends Events {
this.advance();
}
this.emit('stats', this.character);
callback();
if (callback) { callback(); }
}

unequip(itemName, callback) {
@@ -210,7 +213,7 @@ class Game extends Events {
this.advance();
}
this.emit('stats', this.character);
callback();
if (callback) { callback(); }
}

use(itemName, callback) {
@@ -242,7 +245,36 @@ class Game extends Events {
} else {
this.advance();
}
callback();
if (callback) { callback(); }
}

search(itemName, callback) {
const allItems = this.character.inventory.concat(this.world.location.items);
if (allItems.indexOf(itemName) !== -1 || (!itemName && this.world.location.search)) {
const item = itemName ? this.items.get(itemName) : this.world.location;
if (item.search && !item.searched) {
item.searched = true;
item.description += ` ${item.search.description}`;
this.renderer.text(item.search.prints, null, itemName);
this.emit('message', item.search.prints);
if (item.search.items) {
this.world.location.items = this.world.location.items.concat(item.search.items);
}
if (item.search.monsters) {
this.world.location.spawned = this.world.location.spawned
.concat(item.search.monsters
.map(monster => new Monster(this.world.monsters.get(monster), this.items)));
}
this.emit('location', this.world.location);
this.advance();
} else {
this.emit('message', 'You don\'t find anything new.');
}
} else {
this.renderer.text(this.messages.not_found, { color: 'red' });
this.emit('error', this.messages.not_found);
}
if (callback) { callback(); }
}

go(place, callback) {
@@ -281,7 +313,7 @@ class Game extends Events {
this.advance();
}
}
callback();
if (callback) { callback(); }
}

take(itemName, callback) {
@@ -292,11 +324,11 @@ class Game extends Events {
this.emit('inventory', this.character.inventory);
this.emit('message', message);
this.advance();
callback();
if (callback) { callback(); }
}).catch(error => {
this.emit('error', error);
this.renderer.text(error, { color: 'red' });
callback();
if (callback) { callback(); }
});
}

@@ -306,7 +338,7 @@ class Game extends Events {
this.renderer.text(`Damage: ${this.character.damage}`);
this.renderer.text(`Inventory: ${this.character.inventory.join(',')}`);
this.renderer.text(`Equipment: ${this.character.equipped}`);
callback();
if (callback) { callback(); }
}

drop(itemName, callback) {
@@ -323,7 +355,7 @@ class Game extends Events {
}
this.emit('location-items', this.world.location.items);
this.emit('inventory', this.character.inventory);
callback();
if (callback) { callback(); }
}

inventory(args, callback) {
@@ -341,7 +373,7 @@ class Game extends Events {
if (this.character.inventory.length > 0) {
this.renderer.text(this.character.inventory.map(item => this.items.render(item)).join('\n'));
}
callback();
if (callback) { callback(); }
}

examine(item, callback) {
@@ -352,7 +384,7 @@ class Game extends Events {
} else {
this.renderer.text(this.messages.not_found, { color: 'red' });
}
callback();
if (callback) { callback(); }
}

look(args, callback) {
@@ -372,7 +404,7 @@ class Game extends Events {
this.renderer.text(`This place connects to:\n${this.world.location.connects.join('\n')}`);
}
if (callback) {
callback();
if (callback) { callback(); }
}
}



+ 3
- 2
game/items.js View File

@@ -29,8 +29,9 @@ class Items extends Events {
}
}
return {
name: 'unknown',
description: 'An item was referenced, but not defined.',
name: itemName,
prevalence: 'rare',
description: 'This item was referenced, but not defined.',
};
}



+ 2
- 2
game/monsters.js View File

@@ -19,10 +19,10 @@ class Monsters extends Events {
}
}
return {
name: 'unknown',
name: `Unknown monster: ${name}`,
health: 1,
damage: 1,
description: 'A monster was referenced, but not defined.',
description: 'This monster was referenced, but not defined.',
};
}
}


+ 3
- 4
game/world.js View File

@@ -1,11 +1,10 @@
/* eslint-disable no-param-reassign */
/* eslint-disable arrow-parens */
/* eslint-disable import/extensions */
import Messages from './messages.js';
import Monsters from './monsters.js';
import Monster from './monster.js';

const messages = new Messages('data/messages.yml');
// const this.messages = new Messages('data/this.messages.yml');

class World {
constructor(location, loader) {
@@ -28,11 +27,11 @@ class World {
take(item) {
const idx = this.location.items.indexOf(item.name);
if (idx === -1) {
return { error: messages.not_found };
return { error: this.messages.not_found };
}
// const worldItem = items.get(this.location.items[idx]);
if (item.static) {
return { error: messages.static_item };
return { error: this.messages.static_item };
}
return { item: this.location.items.splice(idx, 1)[0] };
}


+ 1
- 0
mobile/components/equipment.js View File

@@ -21,6 +21,7 @@ const template = /* html */`
class="mx-auto mb-2"
max-width="640"
v-for="(item, index) in items"
:key="index"
dark
outlined
>


+ 1
- 1
mobile/components/history.js View File

@@ -17,7 +17,7 @@ const template = /* html */`
<span class="title font-weight-light">History</span>
</v-card-title>
</v-card>
<v-alert v-for="item in items" :color="item.color" v-html="item.text"></v-alert>
<v-alert class="mx-auto" max-width="640" v-for="(item, index) in items" :key="index" :color="item.color" v-html="item.text"></v-alert>
</div>
`;
const history = {


+ 1
- 0
mobile/components/inventory.js View File

@@ -21,6 +21,7 @@ const template = /* html */`
class="mx-auto mb-2"
max-width="640"
v-for="(item, index) in items"
:key="index"
dark
outlined
>


+ 1
- 1
mobile/components/items.js View File

@@ -107,7 +107,7 @@ const items = {
},
methods: {
take(item) {
this.game.take(item.name, () => {});
this.game.take(item.name);
},
},
data: () => ({


+ 10
- 0
mobile/components/location.js View File

@@ -29,6 +29,11 @@ const template = `
<v-card-text>
{{ location.description }}
</v-card-text>
<v-card-actions v-show="location.search && !location.searched">
<v-btn small text @click="search">
search
</v-btn>
</v-card-actions>
</v-card>
`;
const location = {
@@ -42,6 +47,11 @@ const location = {
this.location = this.game.world.location;
});
},
methods: {
search() {
this.game.search();
},
},
data: () => ({
location: {},
}),


+ 1
- 15
mobile/components/monsters.js View File

@@ -18,7 +18,7 @@ const template = /* html */`
<v-list two-line>

<template v-for="(item, index) in items">
<v-list-item :key="item.name">
<v-list-item>
<template v-slot:default="{ active, toggle }">
<v-list-item-content>
<v-list-item-title v-text="item.name"></v-list-item-title>
@@ -43,20 +43,6 @@ const template = /* html */`

</v-list>
</v-card-text>
<v-snackbar
v-model="snackbar"
><span>
You've added <span style="color: yellow">item with super long name</span> to your inventory.
</span>
<v-btn
color="pink"
text
multi-line
@click="snackbar = false"
>
Close
</v-btn>
</v-snackbar>
</v-card>
`;
const monsters = {


Loading…
Cancel
Save