Final Code

Complete Code For 'My First Game' Penguin Shooter :

 


-- ExampleCode_PenguinShooter.lua --
 
    -- onSceneStart is called first to start the game
function onSceneStart()
 
-- Output to the debug console
print ("Hello World")
        -- Add camera controls
    pushFPSCamera()
 
        -- Set initial camera position
    Camera.setEye(vec(0,10,5))
    Camera.setFocus(vec(0,0,2))
   
        -- Add an event map to handle firing projectiles
    map = EventMap.new()
    map:setEvent("keyPress-leftMouse",fire)
    EventHandler.push(map)
   
        -- Create six targets
    for i=1,6 do
        spawnTarget()
    end
   
        -- Start the function that will draw the targets to the center
    Timer.every(attractTargets,0)
end
 
    -- Launch a projectile
function fire()
 
        -- Create the object
    local projectile = SceneManager.createObject("ecanaan/lowpoly_sphere")
   
        -- Position the object on the camera
    projectile:setPosition(Camera.getEye())
    projectile:setScale(0.3)
   
        -- Apply a force in the direction of the mouse
    projectile:applyForce(Mouse.getRay() * 50000)
   
        -- Apply a random color to the new projectile
    projectile:setColor(Color.new(math.random(0,100)/100,math.random(0,100)/100,math.random(0,100)/100))
   
        -- Apply a collision handler to the new projectile
    projectile:setCollisionHandler(collision)
   
        -- Schedule this projectile to be removed after 5 seconds
    Timer.later( function() delete(projectile) end , 5)
end
 
    -- Create a new target
function spawnTarget()
 
        -- Create the target and position it randomly
    local target = SceneManager.createObject("asmedberg/bobblePenguin")
    target:setPosition(vec(math.random(-10,10),math.random(-10,10),20))
   
        -- Apply the keyword target
    target:addKeywords("target")
 
end
 
function collision(self, other, phase)
 
    -- Only execute this code after the collision has been dealt with
    if phase == "end" then
   
        -- Only affect targets
        if other:hasKeyword("target") then
       
            -- Transfer color and remove the projectile
            other:setColor(self:getColor())
            delete(self)
        end
    end
end
 
-- This function will add an eventMap that control the camera
function pushFPSCamera()
 
    -- Create a new event map
    fpskeymap = EventMap:new()
   
    -- Add callback functions to specific input events
    fpskeymap:setEvent("scrollWheel",function(d) Camera.zoomIn(d/200) end)
    fpskeymap:setEvent("keyPress-rightMouse",function() Camera.beginRotate(150) end)
    fpskeymap:setEvent("keyRelease-rightMouse",function() Camera.endRotate() end)
 
    fpskeymap:setEvent("keyDown-left",function() Camera.moveLeft(0.1) end)
    fpskeymap:setEvent("keyDown-right",function() Camera.moveRight(0.1) end)
    fpskeymap:setEvent("keyDown-up",function() if EventHandler.keyIsDown("shift") then  Camera.moveUp(0.1) else Camera.moveForward(0.1) end end)
    fpskeymap:setEvent("keyDown-down",function() if EventHandler.keyIsDown("shift") then  Camera.moveDown(0.1) else Camera.moveBackward(0.1) end end)
 
 
    -- Add the eventmap to the stack
    EventHandler.push(fpskeymap)
end
 
function attractTargets()
 
    -- Loop through every target
    for k,v in pairs(SceneManager.getByKeyword("target")) do
   
        -- Apply a force directly opposite of the targets position
        v:applyForce(v:getPosition() * -10)
    end
end