Otherworldly Crossfire

For my Nature of Code class, I’ve decided to try to create a two-player iPad game inspired by the classic tabletop action game Crossfire, albeit with otherworldly physics and powerups. Here’s a little code written in Lua, intended for use with the Corona SDK. At this stage, the example does not do much except create a physics object (a triangle) in the middle of the screen and create listeners for the two purple “hot zones” at each end of the iPad. When touched, these hot zones fire pellets from the corresponding side of the screen toward the other player.

----------------------------------------------------------------------------------
--
-- game.lua
-- Game Screen for 'Crossfire'
--
----------------------------------------------------------------------------------
 
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
 
-- Called when the scene's view does not exist:
function scene:createScene( event )
    local group = self.view
end

-- Function that shoots a pellet - takes two arguments, which player fired, and where they fired from.
local function shootFrom(whichPlayer, loc)
    local group = scene.view;
    local ball

    -- If the first player fired ...
    if (whichPlayer == 1) then

        ball = display.newCircle( 100, 100, 10 )
        physics.addBody( ball, { density=10.0, friction=0.8, bounce=0.3, radius = 10 } )
        ball.x = loc
        ball.y = 10
        ball:setFillColor(255,30, 30, 255)
        
        -- Apply a force downward on the ball
        ball:applyForce( 0, 1000, ball.x, ball.y )
        ball.isBullet = true;
        group:insert(ball);
    
    end

    -- If the second player fired...
    
    if (whichPlayer == 2) then
  
        ball = display.newCircle( 100, 100, 10 )
        physics.addBody( ball, { density=10.0, friction=0.8, bounce=0.3, radius = 10 } )
        ball.x = loc
        ball.y =  display.contentHeight - 10;
        ball:setFillColor(30,30, 255, 255)
        
        -- Apply a force upward on the ball
        ball:applyForce( 0, -1000, ball.x, ball.y )
        ball.isBullet = true;
        group:insert(ball);

    end

    -- This function eventually removes a pellet from memory if it has been on the screen too long
    -- (In the future add a 'fade out')
    
    local function removeBall()
        print("ball removed");
        ball:removeSelf();
        ball = nil
    end  
    
    -- In 10 seconds, remove the ball.
    timer.performWithDelay(10000, removeBall )
end

-- These are the event listeners for tapping in the 'fire zones'

local function firePlayer1(event)

    -- Note how pellet fires when the finger is lifted. Later, I will implement a 'charge up' that will change the size of the pellet based on how long the finger has been on the screen.
    
    if (event.phase == "ended") then
        print (event.x);
        shootFrom(1, event.x)
    end
end

local function firePlayer2(event)

    -- Note how pellet fires when the finger is lifted. Later, I will implement a 'charge up' that will change the size of the pellet based on how long the finger has been on the screen.
    print (event.x);
    if (event.phase == "ended") then
        shootFrom(2,event.x)
    end
end
 
-- Note that other event.phases could be "began", "moved", "stationary", "cancelled"

-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )
    local group = self.view
    
    -- Create two goal zones
    
    local zone1 = display.newRect( 0, 0, display.contentWidth, 150 )
	zone1:setFillColor(153, 0, 153, 100)
	group:insert(zone1)
	
	-- Register an event listener for touching the first goal zone
	
	zone1:addEventListener("touch", firePlayer1)
	
	local zone2 = display.newRect( 0, display.contentHeight - 150, display.contentWidth, 150 )
    zone2:setFillColor(153, 0, 153, 100)
    
    -- Register an event listener for touching the first goal zone
    
    zone2:addEventListener("touch", firePlayer2)
    
    -- Everything must be added to the local group to be handled on scene changes.
    
    group:insert(zone2)
    group:insert(zone1)
	
	-- Define a function to place the initial objects on screen. For now, it's just a triangle.
	
	local function placeObjects()

        -- Draw a triangle
        local triangle = display.newLine(100,100, 300,100);
        triangle:append(200,0, 100,100);
        triangle.width = 3;
        triangle:setColor(100,100,100,255);
        

        -- Add a custom physics body shape based on the triangle shape
        triShape = {0,0, 100,-100, 200,0, 0, 0}
        physics.addBody( triangle, { density=3.0, friction=0.8, bounce=0.3, shape = triShape } )
        triangle.x = 200;
        triangle.y = 500;
        triangle.isSleepingAllowed = false
        triangle.linearDamping = 1
        triangle.angularDamping = 1;

        -- Everything must be added to the local group to be handled appropriately on 'scene changes'
        group:insert(triangle);
    end
    
    -- Place objects on screen
    
    placeObjects();
end
 
 -- Called when scene is about to move offscreen:
function scene:exitScene( event )
        local group = self.view
end
 
-- Called prior to the removal of scene's "view" (display group)
function scene:destroyScene( event )
        local group = self.view
end
 

---------------------------------------------------------------------------------
-- Listeners required for Storyboard API.
---------------------------------------------------------------------------------
 
-- "createScene" event is dispatched if scene's view does not exist
scene:addEventListener( "createScene", scene )
 
-- "enterScene" event is dispatched whenever scene transition has finished
scene:addEventListener( "enterScene", scene )
 
-- "exitScene" event is dispatched before next scene's transition begins
scene:addEventListener( "exitScene", scene )
 
-- "destroyScene" event is dispatched before view is unloaded, which can be
-- automatically unloaded in low memory situations, or explicitly via a call to
-- storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( "destroyScene", scene )
 
---------------------------------------------------------------------------------
 
return scene

----