diff --git a/entities/enemy.lua b/entities/enemy.lua index 67a144d..cb16bd6 100644 --- a/entities/enemy.lua +++ b/entities/enemy.lua @@ -5,7 +5,9 @@ Enemy = Object:extend() local fireTime = love.timer.getTime() local enemyBullets -function Enemy:new(x, y, radius) +--int x, y, radius +--playableArea -> {int[2]} +function Enemy:new(x, y, radius, playableArea) self.origin = { x, y } self.image = love.graphics.newImage("/assets/enemy/e-fighter.png") @@ -13,6 +15,8 @@ function Enemy:new(x, y, radius) self.y = y self.width = self.image:getWidth() self.height = self.image:getHeight() + self.playableLeft = playableArea[1] + self.playableRight = playableArea[2] self.type = "enemy" self.health = 10 @@ -21,7 +25,7 @@ function Enemy:new(x, y, radius) self.fireRate = 1 self.destroy = false - self.bounds = { (self.x + 18), (self.x + 44), (self.y + 20), (self.y + 44) } + self.hitbox = { (self.x + 18), (self.x + 44), (self.y + 20), (self.y + 44) } enemyBullets = {} end @@ -42,7 +46,7 @@ function Enemy:update(dt, player) self.x = self.x + self.speed * dt - self.bounds = { (self.x + 18), (self.x + 44), (self.y + 20), (self.y + 44) } + self.hitbox = { (self.x + 18), (self.x + 44), (self.y + 20), (self.y + 44) } --movement if self.x > game_bounds[2] then self.speed = -self.speed diff --git a/entities/player.lua b/entities/player.lua index c7efe12..21eac28 100644 --- a/entities/player.lua +++ b/entities/player.lua @@ -3,7 +3,7 @@ Player = Object:extend() require("blaster") local playerBullets -function Player:new(x, y) +function Player:new(x, y, playableArea) self.image = { { "fullHealth", love.graphics.newImage("/assets/player/player01.png") }, { "damaged", love.graphics.newImage("/assets/player/player02.png") }, @@ -15,6 +15,8 @@ function Player:new(x, y) self.y = y self.width = self.image[1][2]:getWidth() self.height = self.image[1][2]:getHeight() + self.playableLeft = playableArea[1] + self.playableRight = playableArea[2] self.type = "player" self.health = 100 @@ -28,6 +30,9 @@ function Player:new(x, y) end function Player:update(dt, enemies) + local maximumLeft = self.playableLeft - 8 + local maximumRight = self.playableRight - 38 + --movement if love.keyboard.isDown("left") then self.x = self.x - self.speed * dt @@ -36,6 +41,12 @@ function Player:update(dt, enemies) self.x = self.x + self.speed * dt end + if self.x < maximumLeft then + self.x = maximumLeft + elseif self.x > maximumRight then + self.x = maximumRight + end + --update bounds self.bounds = { (self.x + 8), (self.x + 38), (self.y + 10), (self.y + 36) } diff --git a/main.lua b/main.lua index b003a9e..708ed18 100644 --- a/main.lua +++ b/main.lua @@ -1,18 +1,21 @@ -function love.load() - Object = require("scripts.classic") - require("entities.player") - require("entities.enemy") - require("ui") +Object = require("scripts.classic") +require("entities.player") +require("entities.enemy") +require("userInterface") - ui = Ui(600, 800) +function love.load() + --adjust the screen and set the bounds of the UI + ui = User_Interface(600, 800) local window_width = love.graphics.getWidth() local window_height = love.graphics.getHeight() + local bounds = ui:returnBounds() + --place the player in the middle of the screen, close to the bottom - player = Player(window_width / 2, window_height * 0.9) + player = Player(window_width / 2, window_height * 0.9, bounds) listOfEnemies = {} - table.insert(listOfEnemies, Enemy(100, 100, 70)) + table.insert(listOfEnemies, Enemy(100, 100, 70, bounds)) --set background for space! love.graphics.setBackgroundColor(27 / 255, 51 / 255, 85 / 255) diff --git a/ui.lua b/userInterface.lua similarity index 65% rename from ui.lua rename to userInterface.lua index f61c922..cf2dad5 100644 --- a/ui.lua +++ b/userInterface.lua @@ -1,7 +1,7 @@ -Ui = Object:extend() +User_Interface = Object:extend() --Set game canvas size -function Ui:new(window_width, window_height) +function User_Interface:new(window_width, window_height) self.playableSpace = { window_width, window_height, (window_width * 0.25) } self.score = 0 @@ -9,7 +9,7 @@ function Ui:new(window_width, window_height) end --set window size -function Ui:draw() +function User_Interface:draw() local left_line = self.playableSpace[3] local right_line = self.playableSpace[1] + self.playableSpace[3] local window_height = self.playableSpace[2] @@ -22,3 +22,8 @@ end --display the score --display player health + +function User_Interface:returnBounds() + --return left and right lines + return { self.playableSpace[3], (self.playableSpace[1] + self.playableSpace[3]) } +end