Skip to content

Instantly share code, notes, and snippets.

@erickzanardo
Last active February 18, 2016 00:00
Show Gist options
  • Select an option

  • Save erickzanardo/552b10f4f1a4e29cdfe3 to your computer and use it in GitHub Desktop.

Select an option

Save erickzanardo/552b10f4f1a4e29cdfe3 to your computer and use it in GitHub Desktop.
Simple implementation of a Asteroids Like game mechanic
function ControllerAdapter(left, right, up, down)
local controller = {
l = left;
r = right;
u = up;
d = down
}
function controller:left()
return love.keyboard.isDown(self.l)
end
function controller:right()
return love.keyboard.isDown(self.r)
end
function controller:up()
return love.keyboard.isDown(self.u)
end
function controller:down()
return love.keyboard.isDown(self.d)
end
return controller
end
require "Vector"
require "ControllerAdapter"
require "Player"
local player1
function love.load()
local wasdController = ControllerAdapter("a", "d", "w", "s")
player1 = Player(400, 100, wasdController)
end
function love.draw()
player1:draw()
end
function love.update(dt)
player1:update(dt)
end
local acceleration = 500
local accelerationReverse = acceleration * -1
local rotationSpeed = 5
local friction = 0.98
local pi = math.pi
local minusPi = pi * -1
function Player(x, y, controller)
local player = {
position = Vector(x, y);
r = 50;
controller = controller;
orientation = pi;
velocity = 0;
direction = Vector(0, 0);
speed = Vector(0, 0)
}
function player:draw()
love.graphics.push()
love.graphics.translate(self.position.x, self.position.y)
love.graphics.rotate(self.orientation)
love.graphics.setColor(255, 255, 255)
love.graphics.circle("fill", 0, 0, self.r, 100)
love.graphics.setColor(255, 0, 0)
love.graphics.line(self.r * -1, 0, 0, 0)
love.graphics.setColor(0, 0, 255)
love.graphics.line(0, 0, self.r, 0)
love.graphics.pop()
love.graphics.reset()
end
function player:update(dt)
--Friction
self.speed.x = self.speed.x * friction
self.speed.y = self.speed.y * friction
local controller = self.controller
self.direction.x = math.cos(self.orientation)
self.direction.y = math.sin(self.orientation)
if controller:left() then
self.orientation = self.orientation - (rotationSpeed * dt)
end
if controller:right() then
self.orientation = self.orientation + (rotationSpeed * dt)
end
if controller:up() then
self.speed.x = self.speed.x + (acceleration * self.direction.x * dt)
self.speed.y = self.speed.y + (acceleration * self.direction.y * dt)
end
if controller:down() then
self.speed.x = self.speed.x + (accelerationReverse * self.direction.x * dt)
self.speed.y = self.speed.y + (accelerationReverse * self.direction.y * dt)
end
self.position.x = self.position.x + (self.speed.x * dt)
self.position.y = self.position.y + (self.speed.y * dt)
-- keep rotation between PI and -PI
if self.orientation > pi then self.orientation = minusPi
elseif self.orientation < minusPi then self.orientation = pi end
end
return player
end
function Vector(x, y)
local v = {x = x; y = y }
function v:sum(v2)
return Vector(self.x + v2.x, self.y + v2.y)
end
function v:subtract(v2)
return Vector(self.x - v2.x, self.y - v2.y)
end
return v
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment