mirror of
https://github.com/SleepingInsomniac/pixelfaucet
synced 2025-02-11 08:48:15 +01:00
Change Point to Vector, remove Point
This commit is contained in:
parent
1a76662f62
commit
e5237337ed
10 changed files with 37 additions and 147 deletions
|
@ -22,7 +22,7 @@ module PF
|
|||
end
|
||||
|
||||
direction = rand((@rotation - @emit_angle)..(@rotation + @emit_angle))
|
||||
particle.velocity = @velocity + Point.new(Math.cos(direction), Math.sin(direction)) * @strength
|
||||
particle.velocity = @velocity + Vector[Math.cos(direction), Math.sin(direction)] * @strength
|
||||
particle.lifespan = @max_age
|
||||
particle
|
||||
end
|
||||
|
|
|
@ -5,8 +5,8 @@ module PF
|
|||
class Entity
|
||||
property sprite : Sprite? = nil
|
||||
|
||||
property position : Point(Float64) = Point.new(0.0, 0.0)
|
||||
property velocity : Point(Float64) = Point.new(0.0, 0.0)
|
||||
property position : Vector(Float64, 2) = Vector[0.0, 0.0]
|
||||
property velocity : Vector(Float64, 2) = Vector[0.0, 0.0]
|
||||
property rotation : Float64 = 0.0
|
||||
property rotation_speed : Float64 = 0.0
|
||||
property mass : Float64 = 1.0
|
||||
|
|
|
@ -28,7 +28,7 @@ module PF
|
|||
|
||||
# Calculate the new velocities
|
||||
normal_vec = (position - other.position) / d
|
||||
tangental_vec = Point(Float64).new(-normal_vec.y, normal_vec.x)
|
||||
tangental_vec = Vector(Float64, 2).new(-normal_vec.y, normal_vec.x)
|
||||
|
||||
# Dot product of velocity with the tangent
|
||||
# (the direction in which to bounce towards)
|
||||
|
|
43
src/game.cr
43
src/game.cr
|
@ -1,6 +1,5 @@
|
|||
require "./lib_sdl"
|
||||
require "./pixel"
|
||||
require "./point"
|
||||
require "./controller"
|
||||
require "./game/*"
|
||||
|
||||
|
@ -11,7 +10,7 @@ module PF
|
|||
|
||||
getter width : Int32
|
||||
getter height : Int32
|
||||
@viewport : Point(Int32)? = nil
|
||||
@viewport : Vector(Int32, 2)? = nil
|
||||
property scale : Int32
|
||||
property title : String
|
||||
property running = true
|
||||
|
@ -51,7 +50,7 @@ module PF
|
|||
end
|
||||
|
||||
def viewport
|
||||
@viewport ||= Point.new(@width, @height)
|
||||
@viewport ||= Vector[@width, @height]
|
||||
end
|
||||
|
||||
def elapsed_time
|
||||
|
@ -84,17 +83,12 @@ module PF
|
|||
end
|
||||
|
||||
# ditto
|
||||
def draw_point(vector : Vector2, pixel : Pixel = Pixel.new, surface = @screen)
|
||||
draw_point(vector.x.to_i32, vector.y.to_i32, pixel, surface)
|
||||
end
|
||||
|
||||
# ditto
|
||||
def draw_point(point : Point(Int), pixel : Pixel = Pixel.new, surface = @screen)
|
||||
def draw_point(point : Vector(Int, 2), pixel : Pixel = Pixel.new, surface = @screen)
|
||||
draw_point(point.x, point.y, pixel, surface)
|
||||
end
|
||||
|
||||
# ditto
|
||||
def draw_point(point : Point(Float64), pixel : Pixel = Pixel.new, surface = @screen)
|
||||
def draw_point(point : Vector(Float, 2), pixel : Pixel = Pixel.new, surface = @screen)
|
||||
draw_point(point.to_i32, pixel, surface)
|
||||
end
|
||||
|
||||
|
@ -103,16 +97,16 @@ module PF
|
|||
# Draw a line using Bresenham’s Algorithm
|
||||
def draw_line(x1 : Int, y1 : Int, x2 : Int, y2 : Int, pixel : Pixel = Pixel.new, surface = @screen)
|
||||
# The slope for each axis
|
||||
slope = Point.new((x2 - x1).abs, -(y2 - y1).abs)
|
||||
slope = Vector[(x2 - x1).abs, -(y2 - y1).abs]
|
||||
|
||||
# The step direction in both axis
|
||||
step = Point.new(x1 < x2 ? 1 : -1, y1 < y2 ? 1 : -1)
|
||||
step = Vector[x1 < x2 ? 1 : -1, y1 < y2 ? 1 : -1]
|
||||
|
||||
# The final decision accumulation
|
||||
# Initialized to the height of x and y
|
||||
decision = slope.x + slope.y
|
||||
|
||||
point = Point.new(x1, y1)
|
||||
point = Vector[x1, y1]
|
||||
|
||||
loop do
|
||||
draw_point(point.x, point.y, pixel, surface)
|
||||
|
@ -136,12 +130,12 @@ module PF
|
|||
end
|
||||
|
||||
# ditto
|
||||
def draw_line(p1 : Point(Int), p2 : Point(Int), pixel : Pixel = Pixel.new, surface = @screen)
|
||||
def draw_line(p1 : Vector(Int, 2), p2 : Vector(Int, 2), pixel : Pixel = Pixel.new, surface = @screen)
|
||||
draw_line(p1.x, p1.y, p2.x, p2.y, pixel, surface)
|
||||
end
|
||||
|
||||
# ditto
|
||||
def draw_line(p1 : Point(Float), p2 : Point(Float), pixel : Pixel = Pixel.new, surface = @screen)
|
||||
def draw_line(p1 : Vector(Float, 2), p2 : Vector(Float, 2), pixel : Pixel = Pixel.new, surface = @screen)
|
||||
draw_line(p1.to_i32, p2.to_i32, pixel, surface)
|
||||
end
|
||||
|
||||
|
@ -164,14 +158,14 @@ module PF
|
|||
end
|
||||
end
|
||||
|
||||
def draw_rect(p1 : PF::Point(Int), p2 : PF::Point(Int), pixel : Pixel = Pixel.new, surface = @screen)
|
||||
def draw_rect(p1 : PF::Vector(Int, 2), p2 : PF::Vector(Int, 2), pixel : Pixel = Pixel.new, surface = @screen)
|
||||
draw_rect(p1.x, p1.y, p2.x, p2.y, pixel, surface)
|
||||
end
|
||||
|
||||
# =================
|
||||
|
||||
# Draw lines enclosing a shape
|
||||
def draw_shape(frame : Array(PF::Point), pixel : Pixel = Pixel.new, surface = @screen)
|
||||
def draw_shape(frame : Enumerable(Point), pixel : Pixel = Pixel.new, surface = @screen)
|
||||
0.upto(frame.size - 1) do |n|
|
||||
draw_line(frame[n], frame[(n + 1) % frame.size], pixel, surface)
|
||||
end
|
||||
|
@ -207,29 +201,18 @@ module PF
|
|||
end
|
||||
end
|
||||
|
||||
def draw_circle(c : Point(Int), r : Int, pixel : Pixel = Pixel.new, surface = @screen)
|
||||
def draw_circle(c : Vector(Int, 2), r : Int, pixel : Pixel = Pixel.new, surface = @screen)
|
||||
draw_circle(c.x, c.y, r, pixel, surface)
|
||||
end
|
||||
|
||||
def draw_circle(c : Vector2, r : Int, pixel : Pixel = Pixel.new, surface = @screen)
|
||||
draw_circle(c.x.to_i, c.y.to_i, r, pixel, surface)
|
||||
end
|
||||
|
||||
# =================
|
||||
|
||||
def draw_triangle(p1 : Point, p2 : Point, p3 : Point, pixel : Pixel = Pixel.new, surface = @screen)
|
||||
def draw_triangle(p1 : Vector, p2 : Vector, p3 : Vector, pixel : Pixel = Pixel.new, surface = @screen)
|
||||
draw_line(p1, p2, pixel, surface)
|
||||
draw_line(p2, p3, pixel, surface)
|
||||
draw_line(p3, p1, pixel, surface)
|
||||
end
|
||||
|
||||
def draw_triangle(p1 : Vector2, p2 : Vector2, p3 : Vector2, pixel : Pixel = Pixel.new, surface = @screen)
|
||||
p1 = Point(Int32).new(x: p1.x.to_i, y: p1.y.to_i)
|
||||
p2 = Point(Int32).new(x: p2.x.to_i, y: p2.y.to_i)
|
||||
p3 = Point(Int32).new(x: p3.x.to_i, y: p3.y.to_i)
|
||||
draw_triangle(p1, p2, p3, pixel, surface)
|
||||
end
|
||||
|
||||
# =================
|
||||
|
||||
# Fill a rect
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
module PF
|
||||
abstract class Game
|
||||
# Fill an abitrary polygon. Expects a clockwise winding of points
|
||||
def fill_shape(points : Enumerable(Point), color : Pixel = Pixel.new, surface = @screen)
|
||||
def fill_shape(points : Enumerable(Vector), color : Pixel = Pixel.new, surface = @screen)
|
||||
return if points.empty?
|
||||
return draw_point(points[0], color, surface) if points.size == 1
|
||||
return draw_line(points[0], points[1], color, surface) if points.size == 2
|
||||
|
@ -61,11 +61,11 @@ module PF
|
|||
end
|
||||
end
|
||||
|
||||
def fill_shape(*points : Point, color : Pixel = Pixel.new, surface = @screen)
|
||||
def fill_shape(*points : Vector, color : Pixel = Pixel.new, surface = @screen)
|
||||
fill_shape(points, color, surface)
|
||||
end
|
||||
|
||||
def draw_shape(*points : Point, color : Pixel = Pixel.new, surface = @screen)
|
||||
def draw_shape(*points : Vector, color : Pixel = Pixel.new, surface = @screen)
|
||||
0.upto(points.size - 1) do |n|
|
||||
draw_line(points[n], points[(n + 1) % points.size], color, surface)
|
||||
end
|
||||
|
|
|
@ -2,7 +2,7 @@ require "../line"
|
|||
|
||||
module PF
|
||||
abstract class Game
|
||||
def fill_triangle(p1 : PF::Point, p2 : PF::Point, p3 : PF::Point, pixel : Pixel = Pixel.new, surface = @screen)
|
||||
def fill_triangle(p1 : Vector, p2 : Vector, p3 : Vector, pixel : Pixel = Pixel.new, surface = @screen)
|
||||
# Sort points from top to bottom
|
||||
p1, p2 = p2, p1 if p2.y < p1.y
|
||||
p1, p3 = p3, p1 if p3.y < p1.y
|
||||
|
@ -54,7 +54,7 @@ module PF
|
|||
end
|
||||
end
|
||||
|
||||
def fill_triangle(points : Enumerable(PF::Point), pixel : Pixel = Pixel.new, surface = @screen)
|
||||
def fill_triangle(points : Enumerable(Vector), pixel : Pixel = Pixel.new, surface = @screen)
|
||||
fill_triangle(points[0], points[1], points[2], pixel, surface)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
require "./vector"
|
||||
|
||||
module PF
|
||||
struct Line(T)
|
||||
property p1 : Point(T), p2 : Point(T)
|
||||
property p1 : Vector(T, 2), p2 : Vector(T, 2)
|
||||
|
||||
def initialize(@p1 : Point(T), @p2 : Point(T))
|
||||
def initialize(@p1 : Vector(T, 2), @p2 : Vector(T, 2))
|
||||
end
|
||||
|
||||
def rise
|
||||
|
@ -68,7 +70,7 @@ module PF
|
|||
end
|
||||
|
||||
def to_point
|
||||
Point.new(run, rise)
|
||||
Vector[run, rise]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
96
src/point.cr
96
src/point.cr
|
@ -1,96 +0,0 @@
|
|||
module PF
|
||||
struct Point(T)
|
||||
property x : T, y : T
|
||||
|
||||
def initialize(@x : T, @y : T)
|
||||
end
|
||||
|
||||
def ==(other : Point(Float | Int))
|
||||
self.x == other.x && self.y == other.y
|
||||
end
|
||||
|
||||
def *(n : Float | Int)
|
||||
Point.new(x * n, y * n)
|
||||
end
|
||||
|
||||
def *(other : Point(Float | Int))
|
||||
Point.new(x * other.x, y * other.y)
|
||||
end
|
||||
|
||||
def /(n : Float | Int)
|
||||
Point.new(x / n, y / n)
|
||||
end
|
||||
|
||||
def /(other : Point(Float | Int))
|
||||
Point.new(x / other.x, y / other.y)
|
||||
end
|
||||
|
||||
def +(other : Point(Float | Int))
|
||||
Point.new(x + other.x, y + other.y)
|
||||
end
|
||||
|
||||
def +(n : Float | Int)
|
||||
Point.new(x + n, y + n)
|
||||
end
|
||||
|
||||
def -(other : Point(Float | Int))
|
||||
Point.new(x - other.x, y - other.y)
|
||||
end
|
||||
|
||||
def -(n : Float | Int)
|
||||
Point.new(x - n, y - n)
|
||||
end
|
||||
|
||||
def >(other : Point)
|
||||
@x > other.x && @y > other.y
|
||||
end
|
||||
|
||||
def <(other : Point)
|
||||
@x < other.x && @y < other.y
|
||||
end
|
||||
|
||||
def %(other : Point)
|
||||
Point.new(x % other.x, y % other.y)
|
||||
end
|
||||
|
||||
def %(n : Float | Int)
|
||||
Point.new(x % n, y % n)
|
||||
end
|
||||
|
||||
def abs
|
||||
Point.new(x.abs, y.abs)
|
||||
end
|
||||
|
||||
def length
|
||||
Math.sqrt((x.abs ** 2) + (y.abs ** 2))
|
||||
end
|
||||
|
||||
def normalized
|
||||
l = length
|
||||
return self if l == 0.0
|
||||
i = (1.0 / l)
|
||||
Point.new(x * i, y * i)
|
||||
end
|
||||
|
||||
def dot(other : Point)
|
||||
x * other.x + y * other.y
|
||||
end
|
||||
|
||||
def cross(other : Point)
|
||||
Point.new(x * other.y - y * other.x, y * other.x - x * other.y)
|
||||
end
|
||||
|
||||
# Distance between two points
|
||||
def distance(other : Point)
|
||||
(self - other).length
|
||||
end
|
||||
|
||||
def to_i32
|
||||
Point(Int32).new(@x.to_i32, @y.to_i32)
|
||||
end
|
||||
|
||||
def inspect
|
||||
"(#{@x}, #{@y})"
|
||||
end
|
||||
end
|
||||
end
|
14
src/shape.cr
14
src/shape.cr
|
@ -7,37 +7,37 @@ module PF
|
|||
x = size + rand(-jitter..jitter)
|
||||
rc = Math.cos(angle)
|
||||
rs = Math.sin(angle)
|
||||
Point.new(0.0 * rc - x * rs, x * rc + 0.0 * rs)
|
||||
Vector[0.0 * rc - x * rs, x * rc + 0.0 * rs]
|
||||
end.to_a
|
||||
end
|
||||
|
||||
# Rotate points by *rotation*
|
||||
def self.rotate(points : Enumerable(Point), rotation : Float64)
|
||||
def self.rotate(points : Enumerable(Vector), rotation : Float64)
|
||||
rc = Math.cos(rotation)
|
||||
rs = Math.sin(rotation)
|
||||
|
||||
points.map do |point|
|
||||
Point.new(point.x * rc - point.y * rs, point.y * rc + point.x * rs)
|
||||
Vector[point.x * rc - point.y * rs, point.y * rc + point.x * rs]
|
||||
end
|
||||
end
|
||||
|
||||
# Translate points by *translation*
|
||||
def self.translate(points : Enumerable(Point), translation : Point)
|
||||
def self.translate(points : Enumerable(Vector), translation : Vector)
|
||||
points.map { |p| p + translation }
|
||||
end
|
||||
|
||||
# ditto
|
||||
def self.translate(*points : Point, translation : Point)
|
||||
def self.translate(*points : Vector, translation : Vector)
|
||||
self.translation(points, translation: translation)
|
||||
end
|
||||
|
||||
# Scale points by a certain *amount*
|
||||
def self.scale(points : Enumerable(Point), amount : Point)
|
||||
def self.scale(points : Enumerable(Vector), amount : Vector)
|
||||
points.map { |p| p * amount }
|
||||
end
|
||||
|
||||
# calculate length from center for all points, and then get the average
|
||||
def self.average_radius(points : Enumerable(Point))
|
||||
def self.average_radius(points : Enumerable(Vector))
|
||||
points.map(&.length).reduce { |t, p| t + p } / points.size
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
require "sdl/image"
|
||||
require "./vector"
|
||||
|
||||
module PF
|
||||
class Sprite
|
||||
|
@ -22,7 +23,7 @@ module PF
|
|||
end
|
||||
|
||||
def size
|
||||
Point.new(width, height)
|
||||
Vector[width, height]
|
||||
end
|
||||
|
||||
def draw(surface : SDL::Surface, x : Int32, y : Int32)
|
||||
|
@ -48,7 +49,7 @@ module PF
|
|||
end
|
||||
|
||||
# ditto
|
||||
def sample(point : Point(Int))
|
||||
def sample(point : Vector(Int, 2))
|
||||
sample(point.x, point.y)
|
||||
end
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue