mirror of
https://github.com/SleepingInsomniac/asteroids
synced 2024-11-15 19:48:28 +01:00
Add astroids
This commit is contained in:
parent
f219e74a40
commit
27aeb7acea
6 changed files with 57 additions and 11 deletions
12
src/asteroid.cr
Normal file
12
src/asteroid.cr
Normal file
|
@ -0,0 +1,12 @@
|
|||
class Asteroid < VectorSprite
|
||||
def initialize
|
||||
super
|
||||
@frame = VectorSprite.generate_circle(12, size: 10.0, jitter: 3.0)
|
||||
end
|
||||
|
||||
def draw(renderer)
|
||||
frame = project_points(@frame)
|
||||
renderer.draw_color = SDL::Color[128, 128, 128, 255]
|
||||
draw_frame(renderer, frame)
|
||||
end
|
||||
end
|
|
@ -4,10 +4,12 @@ include CrystalEdge
|
|||
require "./lx_game/*"
|
||||
include LxGame
|
||||
require "./ship"
|
||||
require "./asteroid"
|
||||
require "./bullet"
|
||||
|
||||
class Asteroids < Game
|
||||
@ship : Ship
|
||||
@asteroids = [] of Asteroid
|
||||
@controller : Controller(LibSDL::Keycode)
|
||||
|
||||
def initialize(*args)
|
||||
|
@ -17,6 +19,13 @@ class Asteroids < Game
|
|||
ship.position = Vector2.new(x: width / 2.0, y: height / 2.0)
|
||||
end
|
||||
|
||||
4.times do
|
||||
@asteroids << Asteroid.build do |a|
|
||||
a.position = Vector2.new(x: rand(0.0..width.to_f), y: rand(0.0..height.to_f))
|
||||
a.velocity = Vector2.new(x: rand(-20.0..20.0), y: rand(-20.0..20.0))
|
||||
end
|
||||
end
|
||||
|
||||
@controller = Controller(LibSDL::Keycode).new({
|
||||
LibSDL::Keycode::UP => "Thrust",
|
||||
LibSDL::Keycode::RIGHT => "Rotate Right",
|
||||
|
@ -54,6 +63,10 @@ class Asteroids < Game
|
|||
end
|
||||
|
||||
@bullets = @bullets.reject { |b| b.age >= 4.0 }
|
||||
@asteroids.each do |a|
|
||||
a.update(dt)
|
||||
a.position = wrap(a.position)
|
||||
end
|
||||
end
|
||||
|
||||
def draw
|
||||
|
@ -61,6 +74,7 @@ class Asteroids < Game
|
|||
@renderer.clear
|
||||
@ship.draw(@renderer)
|
||||
@bullets.each { |b| b.draw(@renderer) }
|
||||
@asteroids.each { |a| a.draw(@renderer) }
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -13,8 +13,6 @@ module LxGame
|
|||
|
||||
def initialize(@width, @height, @scale = 1)
|
||||
SDL.init(SDL::Init::VIDEO)
|
||||
at_exit { SDL.quit }
|
||||
|
||||
@window = SDL::Window.new("SDL test", @width * @scale, @height * @scale)
|
||||
@renderer = SDL::Renderer.new(@window, flags: SDL::Renderer::Flags::SOFTWARE)
|
||||
@renderer.scale = {@scale, @scale}
|
||||
|
@ -63,6 +61,8 @@ module LxGame
|
|||
engine_update
|
||||
engine_draw
|
||||
end
|
||||
ensure
|
||||
SDL.quit
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
module LxGame
|
||||
# Draw a line using Bresenham’s Algorithm
|
||||
def draw_line(renderer : SDL::Renderer, p1 : Vector2, p2 : Vector2, draw_points = false)
|
||||
def draw_line(renderer : SDL::Renderer, p1 : Vector2, p2 : Vector2, draw_points = false, thickness = 1)
|
||||
return draw_line(renderer, p2, p1) if p1.x > p2.x
|
||||
x1, y1, x2, y2 = p1.x.to_i, p1.y.to_i, p2.x.to_i, p2.y.to_i
|
||||
|
||||
|
|
|
@ -1,5 +1,19 @@
|
|||
module LxGame
|
||||
abstract class VectorSprite < Sprite
|
||||
getter frame = [] of Vector2
|
||||
|
||||
def self.generate_circle(num_points : Int, size = 1.0, jitter = 0.0) : Array(Vector2)
|
||||
0.upto(num_points).map do |n|
|
||||
angle = (2 * Math::PI) * (n / 12)
|
||||
|
||||
x = size + rand(-jitter..jitter)
|
||||
|
||||
rc = Math.cos(angle)
|
||||
rs = Math.sin(angle)
|
||||
Vector2.new(0.0 * rc - x * rs, x * rc + 0.0 * rs)
|
||||
end.to_a
|
||||
end
|
||||
|
||||
def project_points(points : Array(Vector2), rotation = @rotation, translate : Vector2? = nil, scale : Vector2? = nil)
|
||||
rc = Math.cos(rotation)
|
||||
rs = Math.sin(rotation)
|
||||
|
@ -21,5 +35,15 @@ module LxGame
|
|||
translation + rotated
|
||||
end
|
||||
end
|
||||
|
||||
def update(dt : Float64)
|
||||
update_position(dt)
|
||||
end
|
||||
|
||||
def draw_frame(renderer : SDL::Renderer, frame = @frame)
|
||||
0.upto(frame.size - 1) do |n|
|
||||
draw_line(renderer, frame[n], frame[(n + 1) % frame.size])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
12
src/ship.cr
12
src/ship.cr
|
@ -1,5 +1,4 @@
|
|||
class Ship < VectorSprite
|
||||
getter frame : Array(Vector2)
|
||||
@fire_cooldown : Float64 = 0.0
|
||||
@fire_rate : Float64 = 0.2
|
||||
@emitter : Emitter
|
||||
|
@ -74,8 +73,9 @@ class Ship < VectorSprite
|
|||
end
|
||||
|
||||
def update(dt : Float64)
|
||||
super
|
||||
|
||||
@fire_cooldown -= dt unless can_fire?
|
||||
update_position(dt)
|
||||
|
||||
@emitter.update(dt)
|
||||
@emitter.position = project_points([Vector2.new(-3.0, 0.0)]).first
|
||||
|
@ -100,13 +100,9 @@ class Ship < VectorSprite
|
|||
@l_emitter.emitting = false
|
||||
@r_emitter.draw(renderer)
|
||||
@r_emitter.emitting = false
|
||||
|
||||
frame = project_points(@frame)
|
||||
|
||||
renderer.draw_color = SDL::Color[128, 128, 128, 255]
|
||||
renderer.draw_color = SDL::Color[255, 255, 255, 255]
|
||||
|
||||
draw_line(renderer, frame[0], frame[1])
|
||||
draw_line(renderer, frame[1], frame[2])
|
||||
draw_line(renderer, frame[2], frame[0])
|
||||
draw_frame(renderer, frame)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue