alterecco avatar alterecco committed 08ea114

primitive love.physics implementation

Comments (0)

Files changed (2)

 			map.layers[i].properties[k] = j
 		end
 	end
-	
+
 	self.data = map
 end
 
+function Map.world(self, world)
+	-- TODO only add physics to main layer
+	for k, v in pairs(self.data.layers) do
+		for y = 0, v.height - 1 do
+			for x = 1, v.width do
+				local curTile = v.data[x + (y * v.width)]
+				if curTile > 0 then
+					local body = love.physics.newBody(world, x * 64, y * 64, 0, 0)
+					local shape = love.physics.newRectangleShape(body, 0, 0, 64, 64, 0)
+				end
+			end
+		end
+	end
+end
+
 function Map.draw(self)
 	local cameraCX = (camera.x + 400) / self.data.tileset.width
 	local cameraCY = (camera.y + 320) / self.data.tileset.height
 local Vector = require 'lib/hump/vector'
 local Map = require 'lib/map'
 
+local world = nil
+
 player = 
 	{ pos = Vector(0,0)
 	,	w = 26
 	,	h = 54
-	,	physBox = nil
+	, shape = nil
+	, body = nil
+	-- ,	physBox = nil
 	,	anim = nil
 	,	controlSet =  {up = "w", left = "a", down = "s", right = "d"}
 	,	images = {}
 	,	facing
 	
-	
-	
+	, load = function(pos)
+		player.body = love.physics.newBody(world, pos.x, pos.y, 10, 0)
+		player.shape = love.physics.newRectangleShape(player.body, 0, 0, player.w, player.h, 0)
+	end
 	, update = function(dt)
 			local up = love.keyboard.isDown(player.controlSet.up)
 			local left = love.keyboard.isDown(player.controlSet.left)
 			local right = love.keyboard.isDown(player.controlSet.right)
 			player.facing = right
 
-			player.physBox.xm = left and -2000 or right and 2000 or 0
-			player.physBox.ym = up and -2000 or down and 2000 or 0
+			-- player.physBox.xm = left and -2000 or right and 2000 or 0
+			-- player.physBox.ym = up and -2000 or down and 2000 or 0
 			--[[
 			if fizz.isTouching(player.physBox) then
 			if up then 
 			player.physBox.ym = (love.keyboard.isDown("w") and player.physBox.yv<0) and -1000 or 0
 			end]]
 
-			player.anim:setSpeed(math.abs(player.physBox.xv) / 128)
-			player.anim:update(dt)
+			-- local x, y = player.body:getLinearVelocity()
+			-- player.anim:setSpeed(math.abs(x / 128)
+			-- player.anim:update(dt)
 
-			player.pos.x = player.physBox.x
-			player.pos.y = player.physBox.y
+			-- player.pos.x = player.physBox.x
+			-- player.pos.y = player.physBox.y
 		end
 	, draw = function()
-			player.anim:draw(player.pos.x - player.w / 2 - camera.x, player.pos.y - player.h / 2 - camera.y)
+			-- player.anim:draw(player.pos.x - player.w / 2 - camera.x, player.pos.y - player.h / 2 - camera.y)
 
-			love.graphics.setColor(127,255,127, 100)
-			camera.rectangle("fill", player.pos.x-player.w/2,player.pos.y-player.h/2, player.w, player.h)
-			love.graphics.setColor(0,0,0, 127)
-			camera.rectangle("line", player.pos.x-player.w/2,player.pos.y-player.h/2, player.w, player.h)
+			love.graphics.setColor(127, 255, 127, 100)
+			camera.rectangle("fill", player.body:getX() - player.w / 2, player.body:getY() - player.h / 2, player.w, player.h)
+			love.graphics.setColor(0, 0, 0, 127)
+			camera.rectangle("line", player.body:getX() - player.w / 2, player.body:getY() - player.h / 2, player.w, player.h)
 		end
 	}
 
+function addCollision(a, b, coll)
+	print("COLLISION!!!!")
+end
+
+function persistCollision(a, b, coll)
+end
+
+function removeCollision(a, b, coll)
+	print("END COLLISION!!!!")
+end
+
+function resultCollision(a, b, coll)
+end
+
 function love.load()
 	love.graphics.setBackgroundColor(255, 255, 255)
 
-	map = Map("map002")
+	map = Map("test2")
 	-- map = Map("test1")
 	map:load()
 
-	dynamics = {}
+	world = love.physics.newWorld(0, 0, map.data.width*map.data.tileset.width, map.data.height*map.data.tileset.height)
+
+	map:world(world)
+
+	-- world = love.physics.newWorld(-map.data.width, -map.data.height, map.data.width, map.data.height)
+	world:setGravity(0, 9.81)
+	world:setMeter(32)
+	world:setCallbacks(addCollision, persistCollision, removeCollision, resultCollision)
+
+	local pos = (map.data.player * map.data.tileset.width) + Vector(map.data.tileset.width * 1.5, map.data.tileset.height / 2)
+	player.load(pos)
+
+	-- dynamics = {}
 
-	-- place player on spawn tile
-	player.pos = (map.data.player * map.data.tileset.width) + Vector(map.data.tileset.width * 1.5, map.data.tileset.height / 2)
 
 	-- player related things
-	player.images.walk = love.graphics.newImage("assets/tiles/character/walk.png")
-	player.anim = newAnimation(player.images.walk, 26, 53.5, 0.15, 0)
-	player.physBox = fizz.addDynamic("rect", player.pos.x, player.pos.y, player.w, player.h, 3)
-	player.physBox.hover = 0.5
+	-- player.images.walk = love.graphics.newImage("assets/tiles/character/walk.png")
+	-- player.anim = newAnimation(player.images.walk, 26, 53.5, 0.15, 0)
+	-- player.physBox = fizz.addDynamic("rect", player.pos.x, player.pos.y, player.w, player.h, 3)
+	-- player.physBox.hover = 0.5
 
 	-- player.pos.x = map.player.x * 64
 	-- player.pos.y = map.player.y * 64
 	-- love.event.push("q")
 	
 	
-	pl_jump = love.audio.newSource("assets/audio/pl_jump.wav")
-	pl_jump:setVolume(1)
+	-- pl_jump = love.audio.newSource("assets/audio/pl_jump.wav")
+	-- pl_jump:setVolume(1)
 	-- Some music
 	music = love.audio.newSource("assets/audio/nysang.mp3") music:setVolume(0.5) love.audio.play(music)
 end
 
 function love.update(dt)
+ 
+	world:update(dt)
+
+
 	dt = math.min(dt, .05)
 	fizz.update(dt)
 	player.update(dt)
 	
 	local mouseAimMul = 0.8
-	camera.x = (player.pos.x - 400 + (love.mouse.getX() - 400) * mouseAimMul - camera.x) * dt * 5 + camera.x
-	camera.y = (player.pos.y - 320 + (love.mouse.getY() - 320) * mouseAimMul - camera.y) * dt * 5 + camera.y
+	camera.x = (player.body:getX() - 400 + (love.mouse.getX() - 400) * mouseAimMul - camera.x) * dt * 5 + camera.x
+	camera.y = (player.body:getY() - 320 + (love.mouse.getY() - 320) * mouseAimMul - camera.y) * dt * 5 + camera.y
 end
 
 function love.draw()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.