Codea Noob's Game

--# Main


-- Use this function to perform your initial setup
displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)
function setup()
    print("Crate Runner")
    print("Welcome to the Console!")
  parameter.text("Input_Password","",callBack)
    saveProjectInfo("Description", "Navigate the maze to discover the secret in the middle")

    stick = Stick()
    a = math.random(0,2)
    b = math.random(0,2)
    c = math.random(0,2)
    d = math.random(0,2)
    e = math.random(0,2)
    f = math.random(0,2)
    g = math.random(0,2)
    h = math.random(0,2)
    i = math.random(0,2)
    j = math.random(0,2)
    k = math.random(0,2)
    l = math.random(0,2)
    m = math.random(0,2)
    n = math.random(0,2)
    o = math.random(0,2)
    p = math.random(0,2)
    q = math.random(0,2)
    r= math.random(0,2)
    s= math.random(0,2)
    t= math.random(0,2)
    u= math.random(0,2)
    v= math.random(0,2)
    w= math.random(0,2)
    x= math.random(0,2)
    y= math.random(0,2)
    z= math.random(0,2)

    pylon = Wall("SpaceCute:Background")
    wall = Wall("SpaceCute:Background")
    floor = Floor("SpaceCute:Background")
    EndGame = Floor("SpaceCute:Background")
    


    world = World()
    
    hero = Hero(3,4)

    TO_DEG = 180/math.pi
        
        end
            -- This function gets called once every frame
function draw()


    local TO_DEG = TO_DEG
    local hero = hero

    perspective(60)
    camera(hero.x, 3, 1 + hero.z, hero.x, 0, hero.z, 0, 1, 0)

-- Draw world
    pushMatrix()
    world:draw()
    popMatrix()

-- Draw hero
    translate(hero.x, hero.y, hero.z)
    rotate(stick.direction*TO_DEG, 0, 1, 0)

    -- roll animation
    if stick.active then
        rotate(-ElapsedTime*10*TO_DEG, 0, 0, 1)
    end

    scale(.25, .25, .25)
    hero:draw()

-- Restore orthographic projection
    ortho()
    viewMatrix(matrix())
    resetMatrix()

    -- fade out overlay
    

    if stick.active then
        local ceil = math.ceil
        stick:draw()

        -- move hero based on stick direction
        local mvtx = math.cos(stick.direction)/50*stick.dist
        local mvtz = -math.sin(stick.direction)/50*stick.dist
        hero.x = hero.x + mvtx
        hero.z = hero.z + mvtz

        -- convert to table coordinates
        hero.px = ceil(hero.x - .5)
        hero.py = ceil(hero.z - .5)

        -- lazy collision check
        if world.data[hero.py][hero.px] ~=  0 and 3 then
            hero.x = hero.x - mvtx
            hero.z = hero.z - mvtz
            hero.px = ceil(hero.x - .5)
            hero.py = ceil(hero.z - .5)
    

            
            end
            
    end
end

function touched(touch)
    stick:touched(touch)
if touch.tapCount == 2 and touch.state == ENDED then
    start = ElapsedTime
    startRecording()
    end
    if touch.tapCount == 3 and touch.state == ENDED and isRecording() then
        stopRecording()
        
    end
    end
    function callBack()
    if Input_Password == "Creator" then 
    print("Austin W.") 
    --Save image to dropbox code
    end
    if Input_Password == "Close" then
        close()
end
if Input_Password == "Lucky#" then
    print("Your Lucky Number is: "..math.random(1,100))
end
if Input_Password == "CoDGhosts" then
    print("Comes out November 5th, 2013")
end
if Input_Password == "Mexican User" then
    close()
end
if Input_Password == ("Im2Lazy") then
    hero = Hero(12,10)
end
end

--# World
World = class()

function World:init()

    -- define the world
    self.data =
        {
        {2, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2,},
        {2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2,},
        {1, 2, 1, 2, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0, 2, 1, h, 1, 2, 0, 1, 3, 0, 0, 0, 0, 2, 1,},
        {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 1, 0, 0, 0, 0, 2, 1, 2, 0, 1, 2,},
        {1, 2, 1, 1, 2, a, a, 1, 2, 0, f, 0, 0, 0, 2, 1, 0, 0, 2, 1, 0, 2, 1,},
        {1, 0, 0, 2, 1, 0, 0, 1, 1, 2, f, 2, g, 1, 2, 1, 2, 0, 1, 2, 0, 2, 2,},
        {1, 0, 0, 0, 0, 0, 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 2, 1, 1, 1, 0, 0, 0, 0, 0, 0,},
        {2, 2, 1, 0, 0, 0, 0, 1, 2, 1, 0, 2, 2, 1, 1, 2, 1, 2, 2, 1, 2, 1, 2, 2, 2, 2, 2, 2, 2,},
        {2, 1, 2, 2, 3, 1, 1, 0, 1, 0, 2, 1, 1, 2, 2, 1, 2, 1, 2, 1, 2, 1, 1, 2, 1, 2, 1, 1, 1,},
        {2, 2, 1, 1, 0, 1, 2, 1, 0, 1, 0, 1, 1, 2, 1, 2, 1, 2, 1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2,},
    }

end

function World:draw()
    local floor, wall, pylon, EndGame = floor, wall, pylon, EndGame
    local offSet = 6
    local px, py = hero.px, hero.py

    -- look around the hero to draw whatever is around him
    translate(px - offSet, 0, py - offSet)
    for y = py - offSet, py + offSet do
        for x = px - offSet, px + offSet do
            if self.data[y] then
                local val = self.data[y][x]
                if val == 0 then
                    floor:draw()
                elseif val == 1 then
                    wall:draw()
                elseif val == 2 then
                    pylon:draw()
                elseif val == 3 then
                    EndGame:draw()
    
            
                end
    

            end
            translate(1,0,0)
        end
        translate(-(1 + 2 * offSet), 0, 1)
    end
end

--# Hero
Hero = class()

function Hero:init(x, z)
    self.x, self.y, self.z = x,0,z
    self.px, self.py = math.ceil(.5+x), math.ceil(.5+z)

    self.mdl = Wall("SpaceCute:Background")
end

function Hero:draw()
    self.mdl:draw()
end

--# Wall
Wall = class()


function Wall:init(tex)
    -- all the unique vertices that make up a cube
    local vertices =
    {
        vec3(-0.5, -0.5,  0.5), -- Left  bottom front
        vec3( 0.5, -0.5,  0.5), -- Right bottom front
        vec3( 0.5,  0.5,  0.5), -- Right top    front
        vec3(-0.5,  0.5,  0.5), -- Left  top    front
        vec3(-0.5, -0.5, -0.5), -- Left  bottom back
        vec3( 0.5, -0.5, -0.5), -- Right bottom back
        vec3( 0.5,  0.5, -0.5), -- Right top    back
        vec3(-0.5,  0.5, -0.5), -- Left  top    back
    }

    -- now construct a cube out of the vertices above
    local verts =
    {
        -- Front
        vertices[1], vertices[2], vertices[3],
        vertices[1], vertices[3], vertices[4],
        -- Right
        vertices[2], vertices[6], vertices[7],
        vertices[2], vertices[7], vertices[3],
        -- Back
        vertices[6], vertices[5], vertices[8],
        vertices[6], vertices[8], vertices[7],
        -- Left
        vertices[5], vertices[1], vertices[4],
        vertices[5], vertices[4], vertices[8],
        -- Top
        vertices[4], vertices[3], vertices[7],
        vertices[4], vertices[7], vertices[8],
       -- Bottom
        vertices[5], vertices[6], vertices[2],
        vertices[5], vertices[2], vertices[1],
    }

    -- all the unique texture positions needed
    local texvertices =
    {
        vec2(0,0),
        vec2(1,0),
        vec2(0,1),
        vec2(1,1)
    }

    -- apply the texture coordinates to each triangle
    local texCoords =
    {
        -- Front
        texvertices[1], texvertices[2], texvertices[4],
        texvertices[1], texvertices[4], texvertices[3],
        -- Right
        texvertices[1], texvertices[2], texvertices[4],
        texvertices[1], texvertices[4], texvertices[3],
        -- Back
        texvertices[1], texvertices[2], texvertices[4],
        texvertices[1], texvertices[4], texvertices[3],
        -- Left
        texvertices[1], texvertices[2], texvertices[4],
        texvertices[1], texvertices[4], texvertices[3],
        -- Top
        texvertices[1], texvertices[2], texvertices[4],
        texvertices[1], texvertices[4], texvertices[3],
        -- Bottom
        texvertices[1], texvertices[2], texvertices[4],
        texvertices[1], texvertices[4], texvertices[3],
    }

    self.model = mesh()
    self.model.vertices = verts
    self.model.texture = tex
    self.model.texCoords = texCoords
    self.model:setColors(255,255,255,255)
end

function Wall:draw()
    self.model:draw()
end

--# Stick
Stick = class()

function Stick:init()
    self.direction = 0
    self.dist = 0

    self.active = false
    self.origin = vec2(150, 150)
    self.center = self.origin
    self.pos = self.origin

    self.stick_bg = readImage("Space Art:Eclipse")
    self.stick = readImage("Space Art:UFO")

end

function Stick:draw()
    sprite(self.stick_bg, self.center.x, self.center.y)
    sprite(self.stick, self.pos.x, self.pos.y)
end

function Stick:touched(touch)
    if touch.state == BEGAN then
        self.center = vec2(touch.x, touch.y)
        self.active = true
    end

    self.pos = vec2(touch.x, touch.y)
    self.direction = math.atan2(self.pos.y - self.center.y, self.pos.x - self.center.x)

    self.dist = math.min(2, self.pos:dist(self.center)/32)

    if touch.state == ENDED then
        self.center = self.origin
        self.pos = self.center
        self.active = false
    end


end


--# Floor
Floor = class()

function Floor:init(tex)
    -- all the unique vertices that make up a cube
    local vertices =
    {
        vec3( 0.5,  -0.5,  0.5), -- Right top    front
        vec3(-0.5,  -0.5,  0.5), -- Left  top    front
        vec3( 0.5,  -0.5, -0.5), -- Right top    back
        vec3(-0.5,  -0.5, -0.5), -- Left  top    back
    }


    -- now construct a cube out of the vertices above
    local verts =
    {
        -- Bottom
        vertices[3], vertices[4], vertices[2],
        vertices[3], vertices[2], vertices[1],
    }

    -- all the unique texture positions needed
    local texvertices =
    {
        vec2(0,0),
        vec2(1,0),
        vec2(0,1),
        vec2(1,1)
    }

    -- apply the texture coordinates to each triangle
    local texCoords =
    {
        -- Bottom
        texvertices[1], texvertices[2], texvertices[4],
        texvertices[1], texvertices[4], texvertices[3],
    }

    self.model = mesh()
    self.model.vertices = verts
    self.model.texture = tex
    self.model.texCoords = texCoords
    self.model:setColors(255,255,255,255)
end

function Floor:draw()
    self.model:draw()
end

Sorry about the three different comments, the code was too long for the first comment

If it’s this long, I suggest putting it in a gist next time (gisthub.com) and then you can just put the link here.

It looks like something I’ve seen on the forum before, have you adapted something? I notice the moving cube goes halfway through walls, you need to stop it when the edge hits the walls (you are probably using the x,y position of the cube, which is at the centre, not the edge).

Also, there’s no need to have cubes for the floor, that’s just wasting Codea’s processing power on vertices that will never be used.

But you have it working, and that’s nice.

@Codeanoob as ignatz suggests use the edges of the box for collision detection, this is where vector/matrix rotation (a miracle) comes in handy. The collision detection you use at the moment allows me to pass through two blocks attached diagonally and I would use a big image on a sprite for the floor, this will free up a lot of memory. As you stated the map changes? When does this occur?

The change occurs when the user starts up the code, and a variable in the world() class changes with a math.random(0,2) where 0 is a floor and 1 and 2 are walls

@Ignatz, and yes, i adapted it from @Xaviers tile based example

Haha, I was just looking my own game based off of @spacemonkey’s game with his ADS lighting class, which was based off of @Xavier’s tile based game, too. I made mine generate a 200x200 tile big world with lots and lots of objects in a grassy world with water pools and trees and plants and houses… I need to work on that one more. :stuck_out_tongue:

I’m waiting for you to finish so I can make a cool game based off yours :))

@ignatz you already have tile mapped houses, terrain, trees, even windows! I’m going to wait until your all finished then make a game with water pools and houses and trees and fences and plants and grass and I shall call it the residents!

=))