Optimize code
This commit is contained in:
		
							
								
								
									
										46
									
								
								main.lua
									
									
									
									
									
								
							
							
						
						
									
										46
									
								
								main.lua
									
									
									
									
									
								
							| @@ -123,44 +123,52 @@ function love.update(dt) | |||||||
| 			fire.pos_frame = 1 | 			fire.pos_frame = 1 | ||||||
| 		end | 		end | ||||||
| 		-- Controls | 		-- Controls | ||||||
|  | 		local control_up, control_right, control_left, control_quit = false, false, false, false | ||||||
| 			-- Keyboard | 			-- Keyboard | ||||||
| 		if love.keyboard.isDown('escape') or love.keyboard.isDown('q') then | 		if love.keyboard.isDown('escape') or love.keyboard.isDown('q') then | ||||||
| 			love.event.push('quit') | 			control_quit = true | ||||||
| 		end | 		end | ||||||
| 		if love.keyboard.isDown('right') then  | 		if love.keyboard.isDown('right') then  | ||||||
| 			ship.body:applyForce(ship.power, 0) | 			control_right = true | ||||||
| 			fire.visible = true |  | ||||||
| 			sounds.fire:play() |  | ||||||
| 	    elseif love.keyboard.isDown('left') then  | 	    elseif love.keyboard.isDown('left') then  | ||||||
| 	  		ship.body:applyForce(-ship.power, 0) | 	    	control_left = true | ||||||
| 			fire.visible = true |  | ||||||
| 			sounds.fire:play() |  | ||||||
| 	    end | 	    end | ||||||
| 	  	if love.keyboard.isDown('up') then  | 	  	if love.keyboard.isDown('up') then  | ||||||
| 			ship.body:applyForce(0, -ship.power) | 	  		control_up = true | ||||||
| 			fire.visible = true |  | ||||||
| 			sounds.fire:play() |  | ||||||
| 	    end | 	    end | ||||||
| 	    	-- Mouse | 	    	-- Mouse | ||||||
| 		if love.mouse.isDown(1) then | 		if love.mouse.isDown(1) then | ||||||
| 			local x, y = love.mouse.getPosition() | 			local x, y = love.mouse.getPosition() | ||||||
| 			-- Up | 			-- Up | ||||||
| 			if x > width / 3 and x < width - (width / 3) and y < height / 3 then | 			if x > width / 3 and x < width - (width / 3) and y < height / 3 then | ||||||
| 				ship.body:applyForce(0, -ship.power) | 	  			control_up = true | ||||||
| 				fire.visible = true |  | ||||||
| 				sounds.fire:play() |  | ||||||
| 			end | 			end | ||||||
| 			-- Left | 			-- Left | ||||||
| 			if x < width / 3 then | 			if x < width / 3 then | ||||||
| 				ship.body:applyForce(-ship.power, 0) | 	    		control_left = true | ||||||
| 				fire.visible = true |  | ||||||
| 				sounds.fire:play() |  | ||||||
| 			elseif x > width - (width / 3) then | 			elseif x > width - (width / 3) then | ||||||
| 				ship.body:applyForce(ship.power, 0) | 				-- Right	 | ||||||
| 				fire.visible = true | 				control_right = true | ||||||
| 				sounds.fire:play() |  | ||||||
| 			end | 			end | ||||||
| 		end | 		end | ||||||
|  | 		-- Ship move | ||||||
|  | 		if control_up then | ||||||
|  | 			ship.body:applyForce(0, -ship.power) | ||||||
|  | 			fire.visible = true | ||||||
|  | 			sounds.fire:play() | ||||||
|  | 		end | ||||||
|  | 		if control_right then | ||||||
|  | 			ship.body:applyForce(ship.power, 0) | ||||||
|  | 			fire.visible = true | ||||||
|  | 			sounds.fire:play() | ||||||
|  | 		elseif control_left then | ||||||
|  | 			ship.body:applyForce(-ship.power, 0) | ||||||
|  | 			fire.visible = true | ||||||
|  | 			sounds.fire:play() | ||||||
|  | 		end | ||||||
|  | 		if control_quit then | ||||||
|  | 			love.event.push('quit') | ||||||
|  | 		end | ||||||
| 		-- Rotate asteroids  | 		-- Rotate asteroids  | ||||||
| 		for key, value in pairs(asteroids) do | 		for key, value in pairs(asteroids) do | ||||||
| 			value.angle = asteroids[key].angle + (dt * math.pi / 10) | 			value.angle = asteroids[key].angle + (dt * math.pi / 10) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user