Sunday 27 April 2014

Corona and the Model-View-Presenter pattern (Part 5)

Hi, and welcome to part 5 of my seven part series.

The last exercise was to add a score to the game. We aren't tracking score, at the moment, so that needs two modifications - adding a score to the model (in model.lua, initialisation function)

model.ball = { x = 100,y = 100, dx = 1,dy = 1,speed = 10, radius = 10 }
model.score = 0 
end

and adding code to display it in view.lua (one modification to the initialise, one to the update)

view.scoreObject = display.newText("<score>",315,5,native.systemFontBold,32) 
view.scoreObject.anchorX,view.scoreObject.anchorY = 1,0
end

        view.scoreObject.text = ("00000" .. model.score):sub(-5,-1) 
end

In this part, we are going to add code to move the ball. I have decided to add the ball moving code to the presenter (the bat moving code is in the model).

Now, there are no fixed rules about this - some people believe in a very very dumb model which just has the data only (known as the "Tyra Banks design pattern" ....). Some people think all the code that manipulates and accesses the model should be in the model.

It doesn't matter too much, really, as long as there is reasonable consistency. This isn't very consistent, obviously, but that's because I'm showing what could be done.

So, in presenter.lua we need to add a call so the update method moves the ball.

function presenter.update(model, view)
presenter.model = model
model.moveBat() 
presenter.moveBall()
view.update(model) 
end

and then we need to create the ball moving method. This is very simplistic, but like the whole series it's designed to showcase concepts, not produce a real game :)

function presenter.moveBall()
local m = presenter.model.ball -- shortcut to model.
if m.dy < display.contentHeight then   -- stop moving if off the bottom
m.x = m.x + m.dx * m.speed -- move it.
m.y = m.y + m.dy * m.speed

if m.x < 0 or m.x > display.contentWidth then -- bounce off left/right sides
m.dx = -m.dx 
end

if m.y < 0 then -- bounce off top of screen
m.dy = -m.dy
end

if m.y > presenter.model.bat.y and m.y-m.dy*m.speed <= presenter.model.bat.y then
if math.abs(m.x-presenter.model.bat.x)<presenter.model.bat.width/2+m.radius then
m.dy = -m.dy -- bounce off the bat, score 10
presenter.model.score = presenter.model.score + 10
end
end
end
end

So it just bounces off the top, left and right, and the bat. You get 10 points for hitting the bat. Once its gone off the bottom it won't move any more.

With all this done, the game should work. Very simple and primitive, but it does work.

Next time we are going to make some simple changes, to show some of the advantages of this structure (hopefully). 

There is no exercise here (really) but if you want to try something, see if you can make the game play upside-down (bat at the top, goes off the top), changing as little as possible in the game :)

The complete code (without the upside down bit) is at https://github.com/autismuk/MVP-Intro/tree/master/Part5

No comments:

Post a Comment