Permalink
Browse files

Now you can drink coffee

  • Loading branch information...
Dorthu committed Apr 30, 2016
1 parent 9ec67d1 commit f20a12ac7cde168cb7c756bbdc9e572dfec3162c
Showing with 74 additions and 33 deletions.
  1. +1 −1 levels/test/ai_test.json
  2. +4 −1 levels/test/entry_hall_update.json
  3. +12 −21 src/inventory.js
  4. +52 −0 src/inventory_item.js
  5. +3 −8 src/pickup.js
  6. +2 −2 src/player.js
View
@@ -9,7 +9,7 @@
{"type":"wall","mats":["wallpaper","ceiling"]},
{"type":"wall","mats":["brick","ceiling"],
"extra": { "object": { "type": "pickup", "desc": "Coffee", "mats": ["coffee"],
"extra": { "name": "Coffee" } } } },
"extra": { "name": "Coffee", "effects": [ { "type": "heal", "amount": 20 } ] } } } },
{"type":"wall","mats":["wallpaper","ceiling"]}],
[{"type":"door","mats":["wallpaper_door",null], "extra":
{ "to": "/levels/test/entry_hall_update", "player_pos": { "x": 1, "z": 2 }, "player_facing": 3 } },
@@ -1,6 +1,9 @@
[[null,{"type":"wall","mats":["brick"]}],
[{"type":"wall","mats":["brick"]},
{"type":"space","mats":["pavement","pavement"]},{"type":"wall","mats":["brick"]}],
{"type":"space","mats":["pavement","pavement"], "extra": { "object": {
"type": "pickup", "mats": ["revolver"], "extra": { "name": "revolver",
"effects": [ {"type": "equip", "target": "right_hand" } ] } }}},
{"type":"wall","mats":["brick"]}],
[{"type":"door","mats":["brick_door"], "extra": { "to": "/levels/test/ai_test", "player_pos": { "x": 1, "z": 2 }, "player_facing": 3 } },{"type":"space","mats":["sidewalk","pavement"]},{"type":"wall","mats":["brick"]}],
[{"type":"wall","mats":["brick"]},{"type":"space","mats":["sidewalk","pavement"]},{"type":"wall","mats":["brick"]},{"type":"wall","mats":["brick"]},{"type":"wall","mats":["brick"]},null,null,null,{"type":"wall","mats":["brick","pavement"]},{"type":"wall","mats":["brick","pavement"]},
{"type":"door","mats":["brick_door","pavement"],
View
@@ -46,6 +46,12 @@ class Inventory {
}
remove(name) {
if(name instanceof Object) {
if(name['name'])
name = name['name'];
else
return;
}
if(this.items.length == 1) {
if(this.items[0]['name'] && this.items[0].name == name) {
this.items = [];
@@ -81,29 +87,13 @@ class Inventory {
+'</div>';
}
equip() {
let i = this.items[this.selected];
if(i['equips_to']) {
let tmp = this.equipped[i.equips_to];
console.log(tmp);
this.equipped[i.equips_to] = i;
if(tmp) {
this.items[this.selected] = tmp;
} else {
this.items.splice(this.selected, 1);
this.selected--;
if(this.selected == -1 && this.items.length) {
this.selected = 0;
}
}
console.log("Equipped");
}
else {
console.log("Can't equip this");
use(player) {
if(this.items[this.selected]) {
this.items[this.selected].use(player);
}
}
input(event) {
input(event, player) {
if(!this.items) { return; }
if(event.keyCode == 37) {
@@ -127,7 +117,8 @@ class Inventory {
this.selected = nsel;
}
} else if(event.keyCode == 69) {
this.equip();
this.use(player);
this.update();
}
this.update();
View
@@ -0,0 +1,52 @@
function equip(i, effect, inventory) {
let tmp = inventory.equipped[effect.target];
inventory.equipped[effect.target] = i;
if(tmp) {
inventory.items[inventory.selected] = tmp;
} else {
inventory.items.splice(inventory.selected, 1);
inventory.selected--;
if(inventory.selected == -1 && inventory.items.length) {
inventory.selected = 0;
}
}
console.log("Equipped");
}
function heal(i, effect, player, inventory) {
player.stats.health.value += effect.amount;
player.stats.update();
remove_item(i, effect, inventory);
}
function remove_item(i, effect, inventory) {
inventory.remove(i);
}
class InventoryItem {
constructor(name, icon, effects=null) {
this.name = name;
this.icon = icon;
this.effects = effects;
}
use(player) {
///handle effects
for(let e of this.effects) {
switch(e['type']) {
case 'equip':
equip(this, e, player.inventory);
break;
case 'heal':
heal(this, e, player, player.inventory);
break;
case null:
break;
default:
console.log("Unhandled effect "+e['effect']);
}
}
}
}
export default InventoryItem;
View
@@ -1,4 +1,5 @@
import SpriteObject from './sprite_object'
import InventoryItem from './inventory_item'
class Pickup extends SpriteObject {
constructor(grid, loc, mats, desc, extra) {
@@ -14,14 +15,8 @@ class Pickup extends SpriteObject {
this.grid.scene.remove(this.meshes[0]);
let inv_item = {
name: this.name,
icon: this._mats[0]
};
if(this.extra['equips_to']) {
inv_item['equips_to'] = this.extra.equips_to;
}
console.log(this.extra);
let inv_item = new InventoryItem(this.name, this._mats[0], this.extra['effects']);
player.inventory.add_item(inv_item);
}
}
View
@@ -141,7 +141,7 @@ class Player {
if(event.keyCode == 73 || event.keyCode == 27) {
this.inv_mode = false;
} else {
this.inventory.input(event);
this.inventory.input(event, this);
}
return;
}
@@ -170,7 +170,7 @@ class Player {
this._inv_hotbar(event.keyCode-48);
console.log('the business');
} else if(event.keyCode == 69) {
this.inventory.equip();
this.inventory.use(this);
this.inventory.update();
}

0 comments on commit f20a12a

Please sign in to comment.