Posted 19 May 2012 - 10:52 AM
intro:
hello everybody,
today i am gonna be teaching you guys how to make a topDown Game with OOP!
now if you dont know what OOP is then you should have a look at this topic:
http://www.computercraft.info/forums2/index.php?/topic/984-advancedsnippetsmetatables-metamethods/
basic setup:
for this game we are gonna use a metatable, in this table we are gonna put some usefull stuff for later on like the health, position, sprite that kind of stuff
using this method lets us use only ONE function for example the movement of multiple objects or one function to draw all the objects
lets get coding:
Config:
first of all we want to make the config it look something like this:
what this does is it creates the needed variables for later on
a object is the main thing it can be the player and it can be a wall(later on)
Classes:
if you dont know what a class is click the link in the top of this post
a class:
line by line explanation:
1. we create a new array or list or whatever
2. in that array we put another array called default this will be the value of all the copy's
3. we prepare a metatable
4. a new function with a argument as table
5. it sets the metatable
6. it registers the new object in the array or list that we made in the config
7. it counts up to prevent overriding
8. it returns the modified table
9. the function ends
BTW: we are gonna use this class, these are the classes in total:
Game functions:
here we are gonna put the functions like move and checkPos and checkPosMove
first of all i like to make a function that draws everything:
explanation:
in that loop it checks if the object is visible
then it draws the sprite depending on the objects direction
then it does the same with the labels but without the direction
next up i made the checkPosMove function
it takes a look at all of the objects and check their position if its in the way then it returns the object thats in the way
thx for being here
wilcomega
btw: please leave a comment with what you think
hello everybody,
today i am gonna be teaching you guys how to make a topDown Game with OOP!
now if you dont know what OOP is then you should have a look at this topic:
http://www.computercraft.info/forums2/index.php?/topic/984-advancedsnippetsmetatables-metamethods/
basic setup:
for this game we are gonna use a metatable, in this table we are gonna put some usefull stuff for later on like the health, position, sprite that kind of stuff
using this method lets us use only ONE function for example the movement of multiple objects or one function to draw all the objects
lets get coding:
Config:
first of all we want to make the config it look something like this:
args = { ... }
-- config
running = true
debug = false
local w, h = term.getSize() w = math.floor(w) h = math.floor(h)
object = {}
objectCount = 0
label = {}
labelCount = 0
ticks = 0
what this does is it creates the needed variables for later on
a object is the main thing it can be the player and it can be a wall(later on)
Classes:
if you dont know what a class is click the link in the top of this post
a class:
local Object = {}
Object.default = {eType = "Living", name = "", strenght = 1, speed = 1, AI = {}, sprite = {"", "", "", ""}, static = true, visible = true, dir = 4, x = 1, y = 1}
Object.mt = {__index = Object.default}
function Object:new(t)
setmetatable(t, Object.mt)
object[objectCount] = t
objectCount = objectCount + 1
return t
end
line by line explanation:
1. we create a new array or list or whatever
2. in that array we put another array called default this will be the value of all the copy's
3. we prepare a metatable
4. a new function with a argument as table
5. it sets the metatable
6. it registers the new object in the array or list that we made in the config
7. it counts up to prevent overriding
8. it returns the modified table
9. the function ends
BTW: we are gonna use this class, these are the classes in total:
local Object = {}
Object.default = {eType = "Living", name = "", strenght = 1, speed = 1, AI = {}, sprite = {"", "", "", ""}, dir = 4, x = 1, y = 1}
Object.mt = {__index = Object.default}
function Object:new(t)
setmetatable(t, Object.mt)
object[objectCount] = t
objectCount = objectCount + 1
return t
end
local Label = {}
Label.default = {name = "", text = "", visible = true, x = 1, y = 1}
Label.mt = {__index = Label.default}
function Label:new(t)
setmetatable(t, Label.mt)
label[labelCount] = t
labelCount = labelCount + 1
return t
end
Game functions:
here we are gonna put the functions like move and checkPos and checkPosMove
first of all i like to make a function that draws everything:
function draw()
for i = 0,#object do
if object[i].visible == true then
term.setCursorPos(object[i].x, object[i].y)
term.write(object[i].sprite[object.dir])
end
end
for i = 0,#label do
if label[i].visible == true then
term.setCursorPos(label[i].x, label[i].y)
term.write(label[i].text)
end
end
end
explanation:
Spoiler
the first 'for loop' draws all the objectsin that loop it checks if the object is visible
then it draws the sprite depending on the objects direction
then it does the same with the labels but without the direction
next up i made the checkPosMove function
function checkPosMove( obj, dir )
if dir == 1 then
for i = 0,#object do
if object[i].static == true and object[i].x == obj.x - 1 and object[i].y == obj.y then
return object[i]
end
end
end
if dir == 2 then
for i = 0,#object do
if object[i].static == true and object[i].x == obj.x and object[i].y == obj.y - 1 then
return object[i]
end
end
end
if dir == 3 then
for i = 0,#object do
if object[i].static == true and object[i].x == obj.x + 1 and object[i].y == obj.y then
return object[i]
end
end
end
if dir == 4 then
for i = 0,#object do
if object[i].static == true and object[i].x == obj.x and object[i].y == obj.y + 1 then
return object[i]
end
end
end
return false
end
explanation: Spoiler
lets take a look at ONE of the directionsit takes a look at all of the objects and check their position if its in the way then it returns the object thats in the way
thx for being here
wilcomega
btw: please leave a comment with what you think