Skip to content

Commit

Permalink
feat: basic beginnings of asteroid game demo
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMcAvoy committed Oct 13, 2024
1 parent 8efa2ce commit f0da30e
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.cache
build
imgui.ini

live.jenscene
Binary file added asteroids_test/main.jenscene
Binary file not shown.
1 change: 1 addition & 0 deletions asteroids_test/resources
2 changes: 2 additions & 0 deletions asteroids_test/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env bash
../build/jenjin/jenjin --runtime
21 changes: 21 additions & 0 deletions asteroids_test/scripts/asteroids.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
local asteroids = {}

function READY() end

function UPDATE()
if input.IsKeyDown("r") then
local quad = helpers.CreateQuad()
local name = tostring("asteroid_"..#asteroids)
local obj = GameObject.new(name, quad)
obj:SetPosition(vec2.new(math.random(-20, 20), math.random(-10, 10)))
obj:SetRotation(math.random(0, 360))

local r = math.random() / 5
obj:SetColor(vec3.new(r, r, r))

scene:AddGameObject(obj)
scene:Build()

asteroids[#asteroids+1] = obj
end
end
55 changes: 55 additions & 0 deletions asteroids_test/scripts/gun.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
local bullets = {}
local player = nil

local bulletCount = 0

local fromLeftGun = true
local timeSinceLastShot = 11

function READY()
player = scene:GetGameObject("Player")
end

function UPDATE()
if input.IsKeyDown("q") then
timeSinceLastShot = timeSinceLastShot + 1
if timeSinceLastShot > 1 then
timeSinceLastShot = 0
local obj = GameObject.new("bullet"..bulletCount, helpers.CreateQuad())
bulletCount = bulletCount + 1

fromLeftGun = not fromLeftGun

local left = vec2.new(math.sin(math.rad(player:GetRotation() - 90)), math.cos(math.rad(player:GetRotation() - 90)))
local up = vec2.new(math.sin(math.rad(player:GetRotation())), math.cos(math.rad(player:GetRotation())))

if fromLeftGun then
obj:SetPosition(player:GetPosition() + left * 1.1 + up * 1.2)
else
obj:SetPosition(player:GetPosition() - left * 1.1 + up * 1.2)
end

obj:SetScale(vec2.new(0.1, 0.2))
obj:SetRotation(player:GetRotation())
obj:SetColor(vec3.new(1, 0, 0))

scene:AddGameObject(obj)
scene:Build()

table.insert(bullets, obj)

player:Translate(up * -0.025)
end
end

for i, bullet in ipairs(bullets) do
local radians = math.rad(bullet:GetRotation())
local direction = vec2.new(math.sin(radians), math.cos(radians))
bullet:Translate(direction * 0.5)

if bullet:GetPosition():Distance(player:GetPosition()) > 250 then
scene:RemoveGameObject(bullet)
table.remove(bullets, i)
end
end
end
49 changes: 49 additions & 0 deletions asteroids_test/scripts/movement.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
local player = nil

local rotation_speed = 5
local movement_speed = 0.2
local acceleration = 0.02
local deceleration = 0.97

local direction = { x = 0, y = 0 }
local velocity = 0

function READY()
player = scene:GetGameObject("Player")
end

function UPDATE()
handleRotation()
handleMovement()

player:Translate(vec2.new(direction.x, direction.y) * movement_speed * velocity)
end

function handleRotation()
a = input.IsKeyDown("a")
d = input.IsKeyDown("d")

if a or d then
local rot = player:GetRotation()

if a then
player:SetRotation((rot - rotation_speed) % 360)
else
player:SetRotation((rot + rotation_speed) % 360)
end
end
end

function handleMovement()
if input.IsKeyDown("w") then
local radians = math.rad(player:GetRotation())
direction.y = math.cos(radians)
direction.x = math.sin(radians)

if velocity < 1 then
velocity = velocity + acceleration
end
else
velocity = velocity * deceleration
end
end
Binary file added asteroids_test/textures/player.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f0da30e

Please sign in to comment.