Skip to content

Commit

Permalink
3/5/22 11:30 PM
Browse files Browse the repository at this point in the history
 - documented inventory.js
 - added maxItemSlots, and inventory capacity
 - updated example / testing code
 - updated TODO list
  • Loading branch information
eboatwright committed Mar 5, 2022
1 parent afd047a commit 49dc18f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 20 deletions.
4 changes: 0 additions & 4 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ Main todo list:
Extras todo list:
- particles

- items

- inventory

- spells

- ai
Expand Down
20 changes: 14 additions & 6 deletions src/extras.js
Original file line number Diff line number Diff line change
Expand Up @@ -780,16 +780,15 @@ class Lightmap extends GameMap {
}

// extras/inventory.js
// TODO: under construction! DON'T USE!!
// TODO: document me!

// An Entity that holds everything you need to know about an item (at the moment ;))
class Item extends Entity {
// The name, is what shows up in the inventory screen
constructor(id = "item", position = vZero(), name = "?", charRenderer = undefined, tags = ["item"]) {
super(id, position, tags);
this.name = name;
this.renderer = charRenderer;
this.renderer.entity = this;
if(charRenderer === undefined)
if(charRenderer === undefined) // If no charRenderer as defined, make a default one
this.renderer = new CharRenderer(this);
}

Expand All @@ -801,6 +800,7 @@ class Item extends Entity {
}
}

// A very simple data class for holding an item Entity, and how many the player has
class ItemSlot {
constructor(item, amount) {
this.item = item;
Expand All @@ -809,8 +809,9 @@ class ItemSlot {
}

class Inventory extends Component {
constructor(entity) {
constructor(entity, maxItemSlots = 10) {
super(entity);
this.maxItemSlots = maxItemSlots;
this.inventory = [];
}

Expand All @@ -821,25 +822,31 @@ class Inventory extends Component {
return false;
}

// Returns the index of the item with the name specified, returns -1 if not found
getItemIndex(itemName) {
for(var i = 0; i < this.inventory.length; i++)
if(this.inventory[i].item.name == itemName)
return i;
return -1;
}

// Pickup an Item Entity
pickup(itemEntity) {
if(itemEntity.destoryed)
return;

if(this.hasItem(itemEntity.name))
this.inventory[this.getItemIndex(itemEntity.name)].amount += 1;
else
else {
if(this.inventory.length >= this.maxItemSlots)
return "full";
this.inventory.push(new ItemSlot(itemEntity, 1));
}

itemEntity.destroy();
}

// Drop the item, and return the Item Entity
drop(itemName) {
if(!this.hasItem(itemName))
return null;
Expand All @@ -856,6 +863,7 @@ class Inventory extends Component {
return item;
}

// Basically a format() function for the Inventory
get() {
var result = "";
for(const slot of this.inventory) {
Expand Down
20 changes: 14 additions & 6 deletions src/jscii/extras/inventory.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// TODO: under construction! DON'T USE!!
// TODO: document me!

// An Entity that holds everything you need to know about an item (at the moment ;))
class Item extends Entity {
// The name, is what shows up in the inventory screen
constructor(id = "item", position = vZero(), name = "?", charRenderer = undefined, tags = ["item"]) {
super(id, position, tags);
this.name = name;
this.renderer = charRenderer;
this.renderer.entity = this;
if(charRenderer === undefined)
if(charRenderer === undefined) // If no charRenderer as defined, make a default one
this.renderer = new CharRenderer(this);
}

Expand All @@ -19,6 +18,7 @@ class Item extends Entity {
}
}

// A very simple data class for holding an item Entity, and how many the player has
class ItemSlot {
constructor(item, amount) {
this.item = item;
Expand All @@ -27,8 +27,9 @@ class ItemSlot {
}

class Inventory extends Component {
constructor(entity) {
constructor(entity, maxItemSlots = 10) {
super(entity);
this.maxItemSlots = maxItemSlots;
this.inventory = [];
}

Expand All @@ -39,25 +40,31 @@ class Inventory extends Component {
return false;
}

// Returns the index of the item with the name specified, returns -1 if not found
getItemIndex(itemName) {
for(var i = 0; i < this.inventory.length; i++)
if(this.inventory[i].item.name == itemName)
return i;
return -1;
}

// Pickup an Item Entity
pickup(itemEntity) {
if(itemEntity.destoryed)
return;

if(this.hasItem(itemEntity.name))
this.inventory[this.getItemIndex(itemEntity.name)].amount += 1;
else
else {
if(this.inventory.length >= this.maxItemSlots)
return "full";
this.inventory.push(new ItemSlot(itemEntity, 1));
}

itemEntity.destroy();
}

// Drop the item, and return the Item Entity
drop(itemName) {
if(!this.hasItem(itemName))
return null;
Expand All @@ -74,6 +81,7 @@ class Inventory extends Component {
return item;
}

// Basically a format() function for the Inventory
get() {
var result = "";
for(const slot of this.inventory) {
Expand Down
11 changes: 7 additions & 4 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ class PlayerController extends Component {
if(keyJustDown(".")) {
for(var entity of level.getEntitiesAtPosition(this.entity.position)) {
if(entity.hasTag("item")) {
this.entity.inventory.pickup(entity);
level.getEntityById("log").renderer.text = `PICKED UP ${entity.name}`;
var result = this.entity.inventory.pickup(entity);
if(result == "full")
level.getEntityById("log").renderer.text = "FULL INVENTORY";
else
level.getEntityById("log").renderer.text = `PICKED UP ${entity.name}`;
}
}
}
Expand Down Expand Up @@ -87,7 +90,7 @@ class Player extends Entity {
constructor(position = vZero()) {
super("player", position, ["player"]);
this.controller = new PlayerController(this);
this.inventory = new Inventory(this);
this.inventory = new Inventory(this, 23);
this.health = new HealthStat(this);
this.renderer = new CharRenderer(this, "default", AT, LIGHT_BROWN, DARK_GRAY);
this.moveTimer = 0;
Expand Down Expand Up @@ -133,7 +136,7 @@ init = function() {
);

level.addEntity(new Player(playerPosition));
level.addEntity(new Item("item", playerPosition.minus(2), "ICE STAFF", new CharRenderer(null, "item", FWD_SLASH, LIGHT_BLUE, BLACK)));
level.addEntity(new Item("item", playerPosition.minus(2), "SAPPHIRE STAFF", new CharRenderer(null, "item", FWD_SLASH, MID_BLUE, BLACK)));

level.addEntity(new Text("hud", vector2(1, 1), "HP: X", WHITE, BLACK));
level.addEntity(new Seperator(vector2(0, 2), LIGHT_GRAY));
Expand Down

0 comments on commit 49dc18f

Please sign in to comment.