This is going to be a lot, and my paragraphs might be choppy so brace yourself.
self is Lua's way to do Object Orientated Programming with classes. Self is used to grab an instances' variables/attributes what'ever you want to call it. But self is just a table so you can generally by pass that if you can find what table that is and instead of "self.variable", you can do "box.variable" like you always have in Lua. (that paragraph was for in case you didn't know bout OOP, and maybe if you read carfully a way to fix it, maybe but not really.)
However it looks like what's happening here is that you're adding stuff like the map and player data to the game_state table. This is indicated when you call "self.variable". self in this case is the table that is before the colon in your function name, "game_state" is that table. in reality when you call "self.player = Entity(32, 256, playerImage)" in your "game_state:enter()" function, you're saying "game_state.player = Entity(32, 256, playerImage)"
To "shortcut around it" you can just stick the player information in a player table rather than the game_state table. And just keep the gameState methods in the game_state table. (everytime you create a function with game_state before the colon you are adding a function into the game_state table.)
So instead of that game_state:enter() you have there maybe do something like this (only doing part of your code there).
Code: Select all
require "entity"
require "map"
game_state = {}
player = {}
function game_state:enter()
local playerImage = lg.newImage("images/player.png")
player.image = Entity(32, 256, playerImage)
map = Map()
map:init()
end
This part is a little touchy (what I'm about to put down). In all seriousness, the only reason you probably have player movement in a game_state method is because you were storing the player data in the game_state table. (because of self and all that jazzyness)
So I would make two seperate things to try and get rid of self if you just want to rid yourself of... self... that seemed a little odd sounding to me... anyway let's get this through!
Code: Select all
function game_state:keypressed(key)
if key == 'escape' then
Gamestate.switch(menu_state)
end
end
function player:keypressed(key)
if key == 'left' then
player.x = player.x - map.tile_size
elseif key == 'right' then
player.x = player.x + map.tile_size
elseif key == 'up' then
player.y = player.y - map.tile_size
elseif key == 'down' then
player.y = player.y + map.tile_size
end
end
Don't even need self there, because it's kind of unnecessary to put player and map data in a game state table, and you can just access the player and map data through the good ol' fashioned way of doing stuff
. (I'm assuming that that Map() function returns data value such as tile_size.)
The last one is pretty easy to fix so, let's just replace every self with the correct and corresponding table. And remove the ones that don't matter.
Code: Select all
function game_state:draw()
-- draw our tileset
for i = 0, 19 do
lg.draw(map.tilemap, map.tiles[i+1], i*map.tile_size, 0)
end
-- a square of grass
for i=0,9 do
lg.draw(map.tilemap, map.tiles[1], i*map.tile_size, WINDOW_HEIGHT-map.tile_size)
end
-- draw player
player:draw()
end
That works in my head. Now you don't even need to use the function name "game_state:draw()" you could do more like "gameDraw()" Because if what I think is right about that game state library is that all it does is handle game states and the switching of them. Like what you did with "Gamestate.switch(menu_state)". So I'm not really sure why there is a game_state table at all. But you can program how you want to, within the boundaries of things working correctly of course (a joke).
I'm getting the vibe that you don't fully understand OOP and self so it's a sensitive subject that if It's not explained very carefully to how you could comprehend it, then you are more confused then when you were before the explanation and it gets worse and worse (I have a whole forum post about me trying to get other to teach me OOP and it took me some months.)
If you need anything clarified just ask. Because I would after this big blocky mess of a post I just made.