-- battlefield 0.1a, (c) David Heuschmann
--[[ Notes
------ NOTES ------
-------------------
]]
-- CONSTANTS
Dimensions = {}
Dimensions.TILESWIDTH = 8 -- number of tiles on the x axis
Dimensions.TILESHEIGHT = 8 -- same for y axis
--[[ CLASS: Rectangle ##########
Creates a rectangle with the specified position, size, border width and color and background color
x: X-Position of the rectangle
y: Y-Position of the rectangle
width: width of the rectangle
height: height of the rectangle
lineWidth: width of the rectangles border | default: 1
lineColor: color of the rectangles border | default: black
bgColor: color of the rectangles background | default: white
]]
Rectangle = Core.class(Shape)
function Rectangle:init(x,y,width,height,lineWidth,lineColor,bgColor)
-- set rectangles position
self:setX(x)
self:setY(y)
-- draw the rectangle
self:beginPath()
self:setLineStyle(lineWidth or 1,lineColor or 0x000000) -- set border width and color
self:setFillStyle(Shape.SOLID,bgColor or 0xFFFFFF) -- set background color
self:moveTo(0,0)
self:lineTo(width,0)
self:lineTo(width,height)
self:lineTo(0,height)
self:lineTo(0,0)
self:endPath()
print("Rectangle added.")
end
-- END CLASS: Rectangle \\\\\\\\\
--[[ CLASS: Button ##########
a clickable button
x: buttons x-Position
y: buttons y-Position
text: the text displayed on the button
graphic: the buttons background graphic (a sprite)
action: a function that specifies what should happen if the button is clicked
]]
Button = Core.class(Sprite)
function Button:init(x,y,text,graphic,action)
-- save the properties (should be different, because they should not be changeable later on)
self.graphic = graphic
self.text = text
self.action = action
-- set x and y position
self:setX(x)
self:setY(y)
-- create the textfield with the buttons label and set its position to be in the buttons center
tf = TextField.new(nil, text)
tf:setX(graphic:getX() + graphic:getWidth()/2 - tf:getWidth()/2)
tf:setY(graphic:getY() + graphic:getHeight()/2 + tf:getHeight()/2)
-- add the graphic and the label
self:addChild(graphic)
self:addChild(tf)
-- add the event listener to call self.onTouchesEnd when a click was performed
self:addEventListener(Event.TOUCHES_END, self.onTouchesEnd, self)
print("Button added.")
end
-- when the click was performed inside the buttons bounds call the action function that was passed
-- to the button on initialisation
function Button:onTouchesEnd(event)
if self:hitTestPoint(event.touch.x, event.touch.y) then self:action() end
end
-- END CLASS: Button \\\\\\\\\
--[[CLASS: Tile ##########
a tile on the field, which contains all the information about itself, like if there is a unit on it or if its highlighted
width: the tiles width
height: the tiles height
fieldx: the x position of the tile on the field (ATTENTION: not the pixel position, but the count of tiles)
fieldy: same, but for y
]]
Tile = Core.class(Sprite)
function Tile:init(width, height, fieldx, fieldy)
self.width = width
self.height = height
self.fieldx = fieldx
self.fieldy = fieldy
self:setX((fieldx - 1) * width)
self:setY((fieldy - 1) * width)
print("Tile "..fieldx..fieldy.." added at "..self:getX()..", "..self:getY())
rect = Rectangle.new(0,0,width, height, null,null,0xFF0000)
button = Button.new(0,0,fieldx..fieldy,rect,function() print(fieldx..fieldy.." "..self:getX().." "..self:getY()) end)
self:addChild(button)
end
--[[ CLASS: Field ##########
represents the playing field with individual tiles on it
]]
Field = Core.class(Sprite)
function Field:init()
self:setX(0) -- set the fields x position to be all the way to the left
self:setY((application:getLogicalHeight() - application:getLogicalWidth()) / 2) -- set the fields y position to center it vertically
rect = Rectangle.new(0,0,application:getLogicalWidth(), application:getLogicalWidth()) -- test rectangle
tilewidth = application:getLogicalWidth() / Dimensions.TILESWIDTH
tileheight = application:getLogicalWidth() / Dimensions.TILESHEIGHT
tiles = {}
for x = 1, Dimensions.TILESWIDTH do
tiles[x] = {}
for y = 1, Dimensions.TILESHEIGHT do
tiles[x][y] = Tile.new(tilewidth, tileheight, x, y)
self:addChild(tiles[x][y])
end
end
self:addChild(rect)
end
-- END CLASS: Field \\\\\\\\\\
--[[ CLASS: Menu ##########
Displays the menu and implements all its functionality
screenmanager: the screenmanager object, that holds and changes the screen currently displayed
]]
Menu = Core.class(Sprite)
function Menu:init(screenmanager)
-- things every screen needs:
self.getScreenManager = function () return screenmanager end
-- /////
self.buttonbg = Rectangle.new(0,0,100,50) -- the background sprite
-- add buttons
-- button that starts the game (ofa)
self.playbutton = Button.new(application:getLogicalWidth()/2 - self.buttonbg:getWidth()/2,
100,"Play",self.buttonbg, function () screenmanager:changeScreen(Screenmanager.GAME) end)
-- add all sprites
self:addChild(self.playbutton)
end
-- END CLASS: Menu \\\\\\\\\
--[[ CLASS: Game ##########
Displays the game and implements all its functionality
]]
Game = Core.class(Sprite)
function Game:init(screenmanager)
-- things every screen needs:
self.getScreenManager = function () return screenmanager end
-- /////
self.buttonbg = Rectangle.new(0,0,100,50)
self.backbutton = Button.new(application:getLogicalWidth()/25,
application:getLogicalHeight()/50,"Back",self.buttonbg, function () screenmanager:changeScreen(Screenmanager.MENU) end)
field = Field.new()
self:addChild(field)
self:addChild(self.backbutton)
end
-- END CLASS: Game \\\\\\\\\
--[[ CLASS: Screenmanager ##########
manages which screen is displayed, screen change is triggered by eve
]]
Screenmanager = Core.class(Sprite)
Screenmanager.MENU = "menu"
Screenmanager.GAME = "game"
function Screenmanager:init()
-- instantiate the different screens
self.menu = Menu.new(self)
self.game = Game.new(self)
-- set and add the start screen
self.currentScreen = self.menu
self:addChild(self.menu)
end
-- called when a screenchange event is dispatched
function Screenmanager:changeScreen(newScreen)
if newScreen == Screenmanager.MENU then screen = self.menu
elseif newScreen == Screenmanager.GAME then screen = self.game
else return false
end
self:removeChild(self.currentScreen)
self.currentScreen = screen
self:addChild(self.currentScreen)
return true
end
-- END CLASS: Screenmanager \\\\\\\\\
Comments
Here's what it looks like now:
Likes: dddent