5 Commits a3d04e771f ... 797e9342b9

Auteur SHA1 Bericht Datum
  Eric Santos 797e9342b9 updated newPlayer table movement in player object, added linearDamping to the body 5 jaren geleden
  Eric Santos b3a6f5a136 Updated player object and usability in main function 5 jaren geleden
  Eric Santos bb653d711a minor change 5 jaren geleden
  Eric Santos ca09a847f8 Merge branch 'master' into testes 5 jaren geleden
  Eric Santos bf1122e6e9 background fixed 5 jaren geleden
3 gewijzigde bestanden met toevoegingen van 73 en 40 verwijderingen
  1. 1 2
      background.lua
  2. 11 3
      main.lua
  3. 61 35
      player.lua

+ 1 - 2
background.lua

@@ -1,5 +1,4 @@
 -- background file for Navy
-
 local bg = {}
 
 function bg.new()
@@ -31,7 +30,7 @@ function objGen(height, width, objCount)
 		star.y = math.random(0, width)
 		table.insert(objList, star)
 	end
-	print(#objList)
+	
 	return objList
 end
 

+ 11 - 3
main.lua

@@ -16,12 +16,16 @@ local objectPile = {}
 local threadPile = {}
 local drawPile = {}
 local updatePile = {}
+local bgPile = {}
+
+local world = love.physics.newWorld(0, 0)
 
 -- collision checker
 local collision = require("collision")
 
 -- player object
 local player = require("player")
+player1, playerP = player.new(world, 200, 200, 30, 30)
 local playerPile = {}
 local points = 0
 
@@ -34,7 +38,6 @@ local downKeys = dofile("keys.lua")
 
 -- background object
 local background = require("background")
-local bgPile = {}
 
 -- shot variables
 local shot = require("shots")
@@ -57,7 +60,7 @@ local enemy2 = enemy.new(400, 300, 20, 12)
 
 function love.load(args)
 	-- Player props
-	table.insert(playerPile, player)
+	table.insert(playerPile, player1)
 	table.insert(objectPile, playerPile)
 
 	-- Shot props
@@ -77,6 +80,7 @@ function love.load(args)
 end
 
 function love.update(dt)
+	world:update(dt)
 	-- timing questions updates
 	timer = timer + dt
 	time = time + dt
@@ -90,7 +94,7 @@ function love.update(dt)
 
 	-- checks if a shot is performed
 	if downKeys.spacebar and shootable then
-		local shot = shot.new(player, shotSpeed)
+		local shot = shot.new(player1, shotSpeed)
 		time = 0
 		table.insert(shotPile, shot)
 	end
@@ -117,6 +121,8 @@ function love.update(dt)
 
 	-- shows player points
 	stats = "Pontos: " .. points
+	playerP:update(downKeys)
+	stats = stats .. "x: " .. playerP.body:getX() .. "\ny: " .. playerP.body:getY()
 end
 
 function love.draw()
@@ -130,6 +136,8 @@ function love.draw()
 		end
 	end
 
+	playerP:draw()
+
 end
 
 function love.keypressed(key)

+ 61 - 35
player.lua

@@ -1,50 +1,76 @@
 -- DONE: implementar função keyDown e keyReleased no bloco main
 local player = {}
 
-function new()
-	player.x = 50
-	player.y = 400
-	player.width = 20
-	player.height = 20
-end
+p = love.physics
+g = love.graphics
 
-function player:update(keys)
-	if keys.up == true then
-		if player.y > 0 then
-			player.y = player.y - 4
-		else
-			player.y = 0
-		end
+function player.new(world, x, y, width, height)
+	local player1 = {}
+	player1.x = x or 50
+	player1.y = y or 400
+	player1.width = width or 20
+	player1.height = height or 20
+
+	local newPlayer = {}
+	newPlayer.body = p.newBody(world, x, y, "dynamic")
+	newPlayer.shape = p.newRectangleShape(width, height)
+	newPlayer.fixture = p.newFixture(newPlayer.body, newPlayer.shape)
+
+	newPlayer.body:setLinearDamping(5)
+
+	newPlayer.update = function(self, keys)
+		if keys.up then self.body:applyForce(0, -600) end
+		if keys.down then self.body:applyForce(0, 600) end
+		if keys.left then self.body:applyForce(-600, 0) end
+		if keys.right then self.body:applyForce(600, 0) end
 	end
-	
-	if keys.down == true then
-		if player. y < (love.graphics.getHeight() - player.height) then
-			player.y = player.y + 4
-		else
-			player.y = love.graphics.getHeight() - player.height
-		end
+
+	newPlayer.draw = function(self)
+		love.graphics.polygon("fill", self.body:getWorldPoints(self.shape:getPoints()))
 	end
 
-	if keys.left == true then
-		if player.x > 0 then
-			player.x = player.x - 4
-		else
-			player.x = 0
+	newPlayer.world = world
+
+	player1.update = function(self, keys)
+		if keys.up == true then
+			if player1.y > 0 then
+				player1.y = player1.y - 4
+			else
+				player1.y = 0
+			end
+		end
+		
+		if keys.down == true then
+			if player1. y < (love.graphics.getHeight() - player1.height) then
+				player1.y = player1.y + 4
+			else
+				player1.y = love.graphics.getHeight() - player1.height
+			end
+		end
+
+		if keys.left == true then
+			if player1.x > 0 then
+				player1.x = player1.x - 4
+			else
+				player1.x = 0
+			end
 		end
-	end
 
-	if keys.right == true then
-		if player.x + player.width < love.graphics.getWidth() then
-			player.x = player.x + 4
-		else
-			player.x = love.graphics.getWidth() - player.width
+		if keys.right == true then
+			if player1.x + player1.width < love.graphics.getWidth() then
+				player1.x = player1.x + 4
+			else
+				player1.x = love.graphics.getWidth() - player1.width
+			end
 		end
 	end
-end
 
-function player:draw()
-	love.graphics.rectangle("fill", self.x, self.y, self.width, self.height)
+	player1.draw = function(self)
+		love.graphics.rectangle("fill", self.x, self.y, self.width, self.height)
+	end
+
+	return player1, newPlayer
 end
 
-new()
+
 return player