Browse Source

updated player movement bounds

master
NCLanceman 8 months ago
parent
commit
d8c6742d13
  1. 10
      entities/enemy.lua
  2. 13
      entities/player.lua
  3. 13
      main.lua
  4. 11
      userInterface.lua

10
entities/enemy.lua

@ -5,7 +5,9 @@ Enemy = Object:extend()
local fireTime = love.timer.getTime() local fireTime = love.timer.getTime()
local enemyBullets 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.origin = { x, y }
self.image = love.graphics.newImage("/assets/enemy/e-fighter.png") self.image = love.graphics.newImage("/assets/enemy/e-fighter.png")
@ -13,6 +15,8 @@ function Enemy:new(x, y, radius)
self.y = y self.y = y
self.width = self.image:getWidth() self.width = self.image:getWidth()
self.height = self.image:getHeight() self.height = self.image:getHeight()
self.playableLeft = playableArea[1]
self.playableRight = playableArea[2]
self.type = "enemy" self.type = "enemy"
self.health = 10 self.health = 10
@ -21,7 +25,7 @@ function Enemy:new(x, y, radius)
self.fireRate = 1 self.fireRate = 1
self.destroy = false 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 = {} enemyBullets = {}
end end
@ -42,7 +46,7 @@ function Enemy:update(dt, player)
self.x = self.x + self.speed * dt 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 --movement
if self.x > game_bounds[2] then if self.x > game_bounds[2] then
self.speed = -self.speed self.speed = -self.speed

13
entities/player.lua

@ -3,7 +3,7 @@ Player = Object:extend()
require("blaster") require("blaster")
local playerBullets local playerBullets
function Player:new(x, y) function Player:new(x, y, playableArea)
self.image = { self.image = {
{ "fullHealth", love.graphics.newImage("/assets/player/player01.png") }, { "fullHealth", love.graphics.newImage("/assets/player/player01.png") },
{ "damaged", love.graphics.newImage("/assets/player/player02.png") }, { "damaged", love.graphics.newImage("/assets/player/player02.png") },
@ -15,6 +15,8 @@ function Player:new(x, y)
self.y = y self.y = y
self.width = self.image[1][2]:getWidth() self.width = self.image[1][2]:getWidth()
self.height = self.image[1][2]:getHeight() self.height = self.image[1][2]:getHeight()
self.playableLeft = playableArea[1]
self.playableRight = playableArea[2]
self.type = "player" self.type = "player"
self.health = 100 self.health = 100
@ -28,6 +30,9 @@ function Player:new(x, y)
end end
function Player:update(dt, enemies) function Player:update(dt, enemies)
local maximumLeft = self.playableLeft - 8
local maximumRight = self.playableRight - 38
--movement --movement
if love.keyboard.isDown("left") then if love.keyboard.isDown("left") then
self.x = self.x - self.speed * dt self.x = self.x - self.speed * dt
@ -36,6 +41,12 @@ function Player:update(dt, enemies)
self.x = self.x + self.speed * dt self.x = self.x + self.speed * dt
end end
if self.x < maximumLeft then
self.x = maximumLeft
elseif self.x > maximumRight then
self.x = maximumRight
end
--update bounds --update bounds
self.bounds = { (self.x + 8), (self.x + 38), (self.y + 10), (self.y + 36) } self.bounds = { (self.x + 8), (self.x + 38), (self.y + 10), (self.y + 36) }

13
main.lua

@ -1,18 +1,21 @@
function love.load()
Object = require("scripts.classic") Object = require("scripts.classic")
require("entities.player") require("entities.player")
require("entities.enemy") require("entities.enemy")
require("ui") 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_width = love.graphics.getWidth()
local window_height = love.graphics.getHeight() local window_height = love.graphics.getHeight()
local bounds = ui:returnBounds()
--place the player in the middle of the screen, close to the bottom --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 = {} listOfEnemies = {}
table.insert(listOfEnemies, Enemy(100, 100, 70)) table.insert(listOfEnemies, Enemy(100, 100, 70, bounds))
--set background for space! --set background for space!
love.graphics.setBackgroundColor(27 / 255, 51 / 255, 85 / 255) love.graphics.setBackgroundColor(27 / 255, 51 / 255, 85 / 255)

11
ui.lua → userInterface.lua

@ -1,7 +1,7 @@
Ui = Object:extend() User_Interface = Object:extend()
--Set game canvas size --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.playableSpace = { window_width, window_height, (window_width * 0.25) }
self.score = 0 self.score = 0
@ -9,7 +9,7 @@ function Ui:new(window_width, window_height)
end end
--set window size --set window size
function Ui:draw() function User_Interface:draw()
local left_line = self.playableSpace[3] local left_line = self.playableSpace[3]
local right_line = self.playableSpace[1] + self.playableSpace[3] local right_line = self.playableSpace[1] + self.playableSpace[3]
local window_height = self.playableSpace[2] local window_height = self.playableSpace[2]
@ -22,3 +22,8 @@ end
--display the score --display the score
--display player health --display player health
function User_Interface:returnBounds()
--return left and right lines
return { self.playableSpace[3], (self.playableSpace[1] + self.playableSpace[3]) }
end
Loading…
Cancel
Save