Skip to content

Commit

Permalink
Merge pull request #1297 from Nicko21/consumables
Browse files Browse the repository at this point in the history
Add support for potions
  • Loading branch information
danhp committed Apr 25, 2013
2 parents c4f11e5 + 13e92b6 commit 33364a2
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 27 deletions.
Binary file added src/images/consumables/baggle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/images/consumables/healthpot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/images/inventory/inventory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 21 additions & 8 deletions src/inventory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ function Inventory.new( player )
for i=0, 3 do
inventory.pages[i] = {}
end
inventory.pageNames = {'Weapons', 'Keys', 'Materials', 'Potions'}
inventory.pageIndexes = {weapons = 0, keys = 1, materials = 2, potions = 3}
inventory.pageNames = {'Weapons', 'Keys', 'Materials', 'Consumables'}
inventory.pageIndexes = {weapons = 0, keys = 1, materials = 2, consumables = 3}
inventory.cursorPos = {x=0,y=0} --The position of the cursor.
inventory.selectedWeaponIndex = 0 --The index of the item on the weapons page that is selected as the current weapon.

Expand All @@ -70,7 +70,7 @@ function Inventory.new( player )
openWeapons = anim8.newAnimation('once', g('6,1'), 1), --The box is open, and on the weapons page.
openKeys = anim8.newAnimation('once', g('7,1'), 1), --The box is open, and on the keys page.
openMaterials = anim8.newAnimation('once', g('8,1'), 1), --The box is open, and on the materials page.
openPotions = anim8.newAnimation('once', g('9,1'), 1), --The box is open, and on the potions page.
openConsumables = anim8.newAnimation('once', g('9,1'), 1), --The box is open, and on the consumables page.
closing = anim8.newAnimation('once', g('1-5,1'),0.02), --The box is currently closing.
closed = anim8.newAnimation('once', g('1,1'),1) --The box is fully closed. Strictly speaking, this animation is not necessary as the box is invisible when in this state.
}
Expand Down Expand Up @@ -318,7 +318,7 @@ end
-- Determines whether the inventory is currently open
-- @return whether the inventory is currently open
function Inventory:isOpen()
return self.state == 'openKeys' or self.state == 'openMaterials' or self.state == 'openPotions' or self.state == 'openWeapons'
return self.state == 'openKeys' or self.state == 'openMaterials' or self.state == 'openConsumables' or self.state == 'openWeapons'
end

---
Expand Down Expand Up @@ -376,9 +376,9 @@ function Inventory:nextScreen()
nextState = "openMaterials"
end
if self.state == "openMaterials" then
nextState = "openPotions"
nextState = "openConsumables"
end
if self.state == "openPotions" then
if self.state == "openConsumables" then
nextState = "openWeapons"
end
if nextState ~= "" then
Expand All @@ -399,11 +399,11 @@ function Inventory:prevScreen()
if self.state == "openMaterials" then
nextState = "openKeys"
end
if self.state == "openPotions" then
if self.state == "openConsumables" then
nextState = "openMaterials"
end
if self.state == "openWeapons" then
nextState = "openPotions"
nextState = "openConsumables"
end
if nextState ~= "" then
self.state = nextState
Expand Down Expand Up @@ -572,11 +572,24 @@ function Inventory:selectCurrentSlot()
self.player.doBasicAttack = false
end

---
-- Consumes the currently selected consumable
-- @return nil
function Inventory:consumeCurrentSlot()
self.selectedConsumableIndex = self:slotIndex(self.cursorPos)
local consumable = self.pages[self.pageIndexes['consumables']][self.selectedConsumableIndex]
if consumable ~= nil then
consumable:use(self.player)
sound.playSfx('confirm')
end
end

---
-- Handles the player selecting a slot in thier inventory
-- @return nil
function Inventory:select()
if self.state == "openWeapons" then self:selectCurrentSlot() end
if self.state == "openConsumables" then self:consumeCurrentSlot() end

---------This is all crafting stuff.
if self.state == "openMaterials" then --We can only craft in the materials section.
Expand Down
8 changes: 8 additions & 0 deletions src/items/consumables/baggle.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
return{
name = "baggle",
type = "consumable",
MAX_ITEMS = 10,
use = function( consumable, player )
player.health = player.max_health
end
}
13 changes: 13 additions & 0 deletions src/items/consumables/healthpot.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
return{
name = "healthpot",
type = "consumable",
MAX_ITEMS = 10,
regen = 5,
use = function( consumable, player )
if (player.health + consumable.props.regen) >= player.max_health then
player.health = player.max_health
else
player.health = player.health + consumable.props.regen
end
end
}
45 changes: 27 additions & 18 deletions src/items/item.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,27 +79,36 @@ function Item:select(player)
end

function Item:use(player)
assert(not self.type=='weapon' or self.props.subtype,"A subtype is required for weapon ("..self.name..")")
--can be used primarily for potions
if self.props.use then
self.props.use(player,self)
elseif self.props.subtype == "melee" then
--if wieldable do nothing
elseif self.props.subtype == "projectile" then
if self.type == "weapon" then
assert(self.props.subtype,"A subtype is required for weapon ("..self.name..")")

if self.props.subtype == "melee" then
--if wieldable do nothing
elseif self.props.subtype == "projectile" then
self.quantity = self.quantity - 1
local node = require('nodes/projectiles/'..self.props.name)
node.x = player.position.x
node.y = player.position.y + player.height/2
node.directory = self.props.type.."s/"
local level = GS.currentState()
local proj = require('nodes/projectile').new(node, level.collider)
proj:throw(player)
level:addNode(proj)
end
if self.quantity <= 0 then
player.inventory:removeItem(player.inventory.selectedWeaponIndex, player.inventory.pageIndexes['weapons'])
end
elseif self.type == "consumable" then
if self.props.use then
self.props.use(self,player)
end
self.quantity = self.quantity - 1
local node = require('nodes/projectiles/'..self.props.name)
node.x = player.position.x
node.y = player.position.y + player.height/2
node.directory = self.props.type.."s/"
local level = GS.currentState()
local proj = require('nodes/projectile').new(node, level.collider)
proj:throw(player)
level:addNode(proj)
if self.quantity <= 0 then
player.inventory:removeItem(player.inventory.selectedConsumableIndex, player.inventory.pageIndexes['consumables'])
end
end

if self.quantity <= 0 then
player.inventory:removeItem(player.inventory.selectedWeaponIndex, 0)
end


end

Expand Down
1 change: 1 addition & 0 deletions src/maps/greendale-exterior.tmx
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@
<property name="to" value="main"/>
</properties>
</object>
<object name="baggle" type="consumable" x="3468" y="30" width="24" height="24"/>
</objectgroup>
<objectgroup color="#a40098" name="platform" width="220" height="18">
<object x="226" y="240" width="37" height="15"/>
Expand Down
90 changes: 90 additions & 0 deletions src/nodes/consumable.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
-----------------------------------------------
-- consumable.lua
-- Represents a consumable when it is in the world
-- Created by Nicko21
-----------------------------------------------

local controls = require 'controls'
local Item = require 'items/item'

local Consumable = {}
Consumable.__index = Consumable
Consumable.isConsumable = true

---
-- Creates a new consumable object
-- @return the consumable object created
function Consumable.new(node, collider)
local consumable = {}
setmetatable(consumable, Consumable)
consumable.name = node.name
consumable.type = 'consumable'
consumable.image = love.graphics.newImage('images/consumables/'..node.name..'.png')
consumable.image_q = love.graphics.newQuad( 0, 0, 24, 24, consumable.image:getWidth(),consumable.image:getHeight() )
consumable.foreground = node.properties.foreground
consumable.collider = collider
consumable.bb = collider:addRectangle(node.x, node.y, node.width, node.height)
consumable.bb.node = consumable
collider:setPassive(consumable.bb)

consumable.position = {x = node.x, y = node.y}
consumable.width = node.width
consumable.height = node.height

consumable.touchedPlayer = nil
consumable.exists = true

return consumable
end

---
-- Draws the consumable to the screen
-- @return nil
function Consumable:draw()
if not self.exists then
return
end
love.graphics.drawq(self.image, self.image_q, self.position.x, self.position.y)
end


function Consumable:keypressed( button, player )
if button ~= 'INTERACT' then return end

local itemNode = require( 'items/consumables/' .. self.name )
itemNode.type = 'consumable'
local item = Item.new(itemNode)
if player.inventory:addItem(item) then
self.exists = false
self.containerLevel:removeNode(self)
self.collider:remove(self.bb)
end
end

---
-- Called when the consumable begins colliding with another node
-- @return nil
function Consumable:collide(node, dt, mtv_x, mtv_y)
if node and node.character then
self.touchedPlayer = node
end
end

---
-- Called when the consumable finishes colliding with another node
-- @return nil
function Consumable:collide_end(node, dt)
if node and node.character then
self.touchedPlayer = nil
end
end

---
-- Updates the consumable and allows the player to pick it up.
function Consumable:update()
if not self.exists then
return
end
end

return Consumable
3 changes: 3 additions & 0 deletions src/nodes/consumables/baggle.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
return{
name = "baggle"
}
2 changes: 1 addition & 1 deletion src/shopping.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function state:init()
self.categories = {}
table.insert(self.categories,"weapons")
table.insert(self.categories,"materials")
table.insert(self.categories,"potions")
table.insert(self.categories,"consumables")
table.insert(self.categories,"keys")
table.insert(self.categories,"armor")
table.insert(self.categories,"misc")
Expand Down
3 changes: 3 additions & 0 deletions src/suppliers/blacksmith.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@ return {
{"bone",30,50},
{"ember",30,70},
},
consumables = {
{"healthpot",5,100}
}
}

0 comments on commit 33364a2

Please sign in to comment.