Created
October 23, 2012 03:07
-
-
Save NimbusBP1729/3936427 to your computer and use it in GitHub Desktop.
LeafItem Leaf require loop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
----------------------------------------------- | |
-- leaf.lua | |
-- Represents a leaf when it is in the world | |
-- Created by HazardousPeach | |
----------------------------------------------- | |
local controls = require 'controls' | |
local Leaf = {} | |
Leaf.__index = Leaf | |
Leaf.leaf = true | |
local LeafImage = love.graphics.newImage('images/leaf.png') | |
local LeafItem = require('items/leafItem') | |
--- | |
-- Creates a new leaf object | |
-- @return the leaf object created | |
function Leaf.new(node, collider) | |
local leaf = {} | |
setmetatable(leaf, Leaf) | |
leaf.image = LeafImage | |
leaf.foreground = node.properties.foreground | |
leaf.bb = collider:addRectangle(node.x, node.y, node.width, node.height) | |
leaf.bb.node = leaf | |
leaf.collider = collider | |
leaf.position = {x = node.x, y = node.y} | |
leaf.width = node.width | |
leaf.height = node.height | |
leaf.touchedPlayer = nil | |
leaf.exists = true | |
return leaf | |
end | |
--- | |
-- Draws the leaf to the screen | |
-- @return nil | |
function Leaf:draw() | |
if not self.exists then | |
return | |
end | |
love.graphics.drawq(self.image, love.graphics.newQuad(0,0, self.width,self.height,self.width,self.height), self.position.x, self.position.y) | |
end | |
--- | |
-- Called when the leaf begins colliding with another node | |
-- @return nil | |
function Leaf:collide(node, dt, mtv_x, mtv_y) | |
if node and node.character then | |
self.touchedPlayer = node | |
end | |
end | |
--- | |
-- Called when the leaf finishes colliding with another node | |
-- @return nil | |
function Leaf:collide_end(node, dt) | |
if node and node.character then | |
self.touchedPlayer = nil | |
end | |
end | |
--- | |
-- Updates the leaf and allows the player to pick it up. | |
function Leaf:update() | |
if not self.exists then | |
return | |
end | |
if controls.isDown( 'UP' ) and self.touchedPlayer then | |
local item = LeafItem.new() | |
if self.touchedPlayer.inventory:addItem(item) then | |
self.exists = false | |
end | |
end | |
end | |
return Leaf |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
----------------------------------------------- | |
-- leafItem.lua | |
-- Represents a leaf when it is in the players inventory | |
-- Created by HazardousPeach | |
----------------------------------------------- | |
--!!!!!!!!! | |
--the following line kills hawkthorne by creating a loop | |
-- I imagine it has to do with the fact that leaf.lua includes leafItem.lua, | |
-- but I don't see why it doesn't just load an already loaded version if it exists | |
local Leaf = require('nodes/leaf') | |
local LeafItem = {} | |
LeafItem.__index = LeafItem | |
LeafItem.leafItem = true | |
local LeafItemImage = love.graphics.newImage('images/leaf_item.png') | |
--- | |
-- Creates a new leaf item object | |
-- @return the leaf item object created | |
function LeafItem.new() | |
local leafItem = {} | |
setmetatable(leafItem, LeafItem) | |
leafItem.image = LeafItemImage | |
leafItem.type = 'Material' | |
leafItem.name = 'leaf' | |
return leafItem | |
end | |
--- | |
-- Draws the rock to the screen | |
-- @return nil | |
function LeafItem:draw(position) | |
love.graphics.drawq(self.image, love.graphics.newQuad(0,0, 15,15,15,15), position.x, position.y) | |
end | |
return LeafItem |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For anyone reading this after the fact this problem was ameliorated by using an inline require.
I'd guess the underlying implementation of
require
doesn't use something like #IFNDEF macros to assert that it doesn't need to get stuck in a loop.