From 245e88147e137a09105eb479e1603e080d7266c4 Mon Sep 17 00:00:00 2001 From: Brecert <11599528+Brecert@users.noreply.github.com> Date: Thu, 27 Sep 2018 14:00:27 -0400 Subject: [PATCH 1/6] :bulb: Documented the Library --- src/leafgem/library.cr | 147 ++++++++++++++++++++++++++++------------- 1 file changed, 101 insertions(+), 46 deletions(-) diff --git a/src/leafgem/library.cr b/src/leafgem/library.cr index e340d4d..4778792 100644 --- a/src/leafgem/library.cr +++ b/src/leafgem/library.cr @@ -1,24 +1,70 @@ require "./library/*" +# all the goofy global functions that are +# essentially simple wrappers around less intuitive +# actions. module Leafgem::Library # include Leafgem::Mouse::WheelManager - # ======================== # - # LIBRARY # - # ======================== # - # Beginning of the publically accessible library, - # all the goofy global functions that are - # essentially simple wrappers around less intuitive - # actions. - + # Begins to run the gameloop + # + # ``` + # require "leafgem" + # include Leafgem::Library + # set_window("Hello world!", 320, 240, 3) + # + # game_run + # ``` def game_run Leafgem::Game.run end + # Ends the gameloop def game_exit Leafgem::Game.quit end + # Will call the given function every frame **before** every object starts updating + def set_loop_function(function : Proc(Void)) + Leafgem::Game.set_loopfunc(function) + end + + # Creates a window for our game. + # + # In order, parameters are the title of the window, the width in pixels, the height in pixels, and the scale which sprites and shapes will be drawn; + # + # For example, if set to 2.0, sprites will appear twice as big. (For best results, use an integer) + # + # NOTE: These parameters are immuteable + def set_window(window_title : String, window_width : Int32, window_height : Int32, scale : Float32 = 1.0) + title = window_title || "Leafgem Game" + width = window_width || 640 + height = window_height || 480 + Leafgem::Renderer.create(title, width, height, scale) + end + + # Gets the screen width + def screen_width + Leafgem::Renderer.size.x + end + + # Gets the screen height + def screen_height + Leafgem::Renderer.size.y + end + + # Set or unset fullscreen + def set_fullscreen(bool : Bool) + if (win = Leafgem::Renderer.window) + win.fullscreen = (bool) ? SDL::Window::Fullscreen::FULLSCREEN_DESKTOP : SDL::Window::Fullscreen::WINDOW + end + end + + # Adds the given object to gameworld at the given coordinates + # + # ``` + # create_object(Player, 100, 100) + # ``` def create_object(thing, x = 0, y = 0) # Give objects a unique identifier on create? new_obj = thing.new @@ -33,32 +79,39 @@ module Leafgem::Library Leafgem::Game.loop[thing.to_s].push(new_obj) end + # Destroys the given object from the `Game` + # + # ``` + # player = create_object(Player, 100, 100) + # destroy(player) + # ``` def destroy(thing : Leafgem::Object) Leafgem::Game.to_destroy.push(thing) end + # Returns an array of IDs for every instance of a class that currently exists in the gameworld. def get(object_class) Leafgem::Game.loop[object_class.to_s] end + # Creates and returns a new `Spritesheet` def new_spritesheet(filepath : String, tilewidth : Int32, tileheight : Int32) Leafgem::Spritesheet.new(filepath, tilewidth, tileheight) end + # Leads an image from a path and returns and `SDL::Surface` def sprite(filepath : String) Leafgem::AssetManager.image(filepath) end + # Sets the draw color for `Renderer` def set_draw_color(r : Int32, g : Int32, b : Int32, a : Int32 = 255) if (lgr = Leafgem::Renderer.renderer) lgr.draw_color = SDL::Color.new(r, g, b, a) end end - def set_background_color(r : Int32, g : Int32, b : Int32) - Leafgem::Renderer.set_background_color(r, g, b) - end - + # Ditto def set_draw_color(color_tuple) out_color = SDL::Color.new(*color_tuple) if (lgr = Leafgem::Renderer.renderer) @@ -66,41 +119,35 @@ module Leafgem::Library end end + # Sets the background color for the `Renderer` + def set_background_color(r : Int32, g : Int32, b : Int32) + Leafgem::Renderer.set_background_color(r, g, b) + end + + # Fills a rectangle based on the position, and size def fill_rect(x, y, w, h, ignore_camera = false) Leafgem::Draw.fill_rect(x, y, w, h, ignore_camera = false) end + # Draws (outlines) a rectangle based on the position, and size def draw_rect(x, y, w, h, ignore_camera = false) Leafgem::Draw.draw_rect(x, y, w, h, ignore_camera = false) end + # Fills a circle based on the position and radius def fill_circ(x, y, r) raise Exception.new(Leafgem_errors["circ_invalid_radius"]) if (r < 0) Leafgem::Draw.fill_circ(x, y, r) end + # Draws (outlines) a circle based on the position and radius def draw_circ(x, y, r) raise Exception.new(Leafgem_errors["circ_invalid_radius"]) if (r < 0) Leafgem::Draw.sprite_circ(x, y, r) end - def debug_show_hitboxes(bool) - Leafgem::Game.show_hitboxes(bool) - end - - def key_pressed?(keycode : String) - Leafgem::KeyManager.key_is_pressed(keycode.downcase) - end - - def key?(keycode : String) - Leafgem::KeyManager.key_is_held(keycode.downcase) - end - - def set_loop_function(function : Proc(Void)) - Leafgem::Game.set_loopfunc(function) - end - - # wowee this is super messy + # NOTE: wowee this is super messy + # Draws a sprite def draw_sprite(texture : SDL::Surface, x = 0, y = 0, alpha = 255, xscale = 1, yscale = 1, gui = false) alpha = Math.min(alpha.to_f, 255) texture.alpha_mod = alpha @@ -112,6 +159,7 @@ module Leafgem::Library gui) end + # Draws a sprite from a file def draw_sprite(file : String, x = 0, y = 0, alpha = 255, gui = false) alpha = Math.min(alpha.to_f, 255) if (tex = Leafgem::AssetManager.image(file)) @@ -123,67 +171,72 @@ module Leafgem::Library end end - def set_window(window_title : String, window_width : Int32, window_height : Int32, scale : Float32 = 1.0) - title = window_title || "Leafgem Game" - width = window_width || 640 - height = window_height || 480 - Leafgem::Renderer.create(title, width, height, scale) - end - - def camera - Leafgem::Renderer.camera.pos + # Enable or disable rendering of debug hitboxes + def debug_show_hitboxes(bool : Bool) + Leafgem::Game.show_hitboxes(bool) end - def screen_width - Leafgem::Renderer.size.x + # Is the key pressed? + def key_pressed?(keycode : String) + Leafgem::KeyManager.key_is_pressed(keycode.downcase) end - def screen_height - Leafgem::Renderer.size.y + # Is the key held? + def key?(keycode : String) + Leafgem::KeyManager.key_is_held(keycode.downcase) end - def set_fullscreen(bool) - if (win = Leafgem::Renderer.window) - win.fullscreen = (bool) ? SDL::Window::Fullscreen::FULLSCREEN_DESKTOP : SDL::Window::Fullscreen::WINDOW - end + # Get the camera position + def camera + Leafgem::Renderer.camera.pos end + # Plays a sound from a filename def play_sound(filename : String) Leafgem::Audio.play_sound(filename) end + # Plays music from a filename def play_music(filename : String) Leafgem::Audio.play_music(filename) end + # Fades out the music def fade_out_music(seconds) Leafgem::Audio.fade_out_music(seconds.to_f32) end + # Camera x position def camera_x Leafgem::Renderer.camera.pos.x end + # Set the camera x position def set_camera_x(x) Leafgem::Renderer.camera.posbuffer.x = x.to_f end + # Camera y position def camera_y Leafgem::Renderer.camera.pos.y end + # Set the camera y position def set_camera_y(y) Leafgem::Renderer.camera.posbuffer.y = y.to_f end + # Lineral interpolation def lerp(a, b, t) return a + (b - a) * t end + # Restrict a value between the min and the max value def clamp(val, min, max) return Math.max(Math.min(val, max), min) end + # NOTE: bad system, bad boolean AAAAAAAAaAAA def sign(val) if (val > 0) 1 @@ -194,10 +247,12 @@ module Leafgem::Library end end + # Display debug text def debug(text) Leafgem::Game.debug(text.to_s) end + # Load a map from a filename def load_map(filename) Leafgem::Map.loadmap(filename) end From 31e8b270cea9a43c7d853dcaae67c2453aa01fd8 Mon Sep 17 00:00:00 2001 From: Brecert <11599528+Brecert@users.noreply.github.com> Date: Thu, 27 Sep 2018 14:03:23 -0400 Subject: [PATCH 2/6] :bulb: Update library docs --- src/leafgem/library.cr | 1 + 1 file changed, 1 insertion(+) diff --git a/src/leafgem/library.cr b/src/leafgem/library.cr index 4778792..1a97ad6 100644 --- a/src/leafgem/library.cr +++ b/src/leafgem/library.cr @@ -147,6 +147,7 @@ module Leafgem::Library end # NOTE: wowee this is super messy + # # Draws a sprite def draw_sprite(texture : SDL::Surface, x = 0, y = 0, alpha = 255, xscale = 1, yscale = 1, gui = false) alpha = Math.min(alpha.to_f, 255) From abdb2731c9baf8ff98d9e59be709ec3370cb145d Mon Sep 17 00:00:00 2001 From: Brecert <11599528+Brecert@users.noreply.github.com> Date: Thu, 27 Sep 2018 14:15:15 -0400 Subject: [PATCH 3/6] :bulb: Documented the leafjem object --- src/leafgem/objects/object.cr | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/leafgem/objects/object.cr b/src/leafgem/objects/object.cr index 8a7b0b6..bdb7e6d 100644 --- a/src/leafgem/objects/object.cr +++ b/src/leafgem/objects/object.cr @@ -1,16 +1,23 @@ require "./hitbox" +# The leafgem base object class Leafgem::Object property pos = Vec2(Float64).new 0.0, 0.0 property size = Vec2(Int32).new 0, 0 property hitbox = Hitbox.new(Vec2.from(0, 0), Vec2.from(0, 0)) + # Called when the object is added and initialized def init; end + # Called whenever the object is updated def update; end + # Called when drawing the object + # + # Drawing logic should go here def draw; end + # Drestroys self from the `Game` def destroy Leafgem::Game.to_destroy.push(self) end From 3c31c51b44f3946c4d656c5c4bfab9a5ce21e3dc Mon Sep 17 00:00:00 2001 From: Brecert <11599528+Brecert@users.noreply.github.com> Date: Thu, 27 Sep 2018 14:22:43 -0400 Subject: [PATCH 4/6] :bulb: Documented hitbox --- src/leafgem/objects/hitbox.cr | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/leafgem/objects/hitbox.cr b/src/leafgem/objects/hitbox.cr index d92e4c7..4f86277 100644 --- a/src/leafgem/objects/hitbox.cr +++ b/src/leafgem/objects/hitbox.cr @@ -1,3 +1,9 @@ +# A hitbox +# +# Hitboxes are used for collision +# +# By default any `Leafgem::Object` +# will have a hitbox attatched to it class Leafgem::Hitbox property pos : Vec2(Int32) property size : Vec2(Int32) @@ -5,15 +11,18 @@ class Leafgem::Hitbox def initialize(@pos : Vec2, @size : Vec2) end + # Sets the hitboxx position and size def set(x, y, w, h) @pos = Vec2.from x, y @size = Vec2.from w, h end + # Get the hitbox position and size as a tuple def get {@pos.x, @pos.y, @size.x, @size.y} end + # is the hitbox meeting another object? def meeting?(x, y, foreign_object) # basic box collision # check each corner of self with every corner of every instance of the foreign object @@ -32,10 +41,14 @@ class Leafgem::Hitbox return false end + # Is the position in the hitbox? def point_in?(x, y) x >= @pos.x && x <= @pos.y + @size.x && y >= @pos.y && y <= @pos.y + @size.y end + # NOTE: This is weird + # + # Is their another hitbox in this one? def box_collision_check(this, other, x, y) if this.x + x >= other.x && this.x + x < other.x + other.w && this.y + y >= other.y && this.y + y < other.y + other.h || (this.x + x + this.w) >= other.x && (this.x + x + this.w) < other.x + other.w && this.y + y >= other.y && this.y + y < other.y + other.h || @@ -47,6 +60,7 @@ class Leafgem::Hitbox end end + # Is the hitbox meeting a tile? def meeting_tile?(xoffset, yoffset, tile, accuracy = 2) # insert corners points_to_check = [ @@ -71,6 +85,7 @@ class Leafgem::Hitbox return false end + # Is the hitbox meeting a tile based on the layer def meeting_tile_layer?(xoffset, yoffset, tilelayer, accuracy = 2) points_to_check = [ [self.x + self.hitbox.x + xoffset, self.y + self.hitbox.y + yoffset], @@ -98,7 +113,7 @@ class Leafgem::Hitbox point_in? Library::Mouse.pos.x, Library::Mouse.pos.y end - # Did the hitbox get pressed? + # Is the hitbox pressed? def pressed? if b = Library::Mouse.primary mouse_in? && b.pressed? From b84a95b5af523909defa17f42f9562852dfbd4c2 Mon Sep 17 00:00:00 2001 From: Brecert <11599528+Brecert@users.noreply.github.com> Date: Thu, 27 Sep 2018 14:40:49 -0400 Subject: [PATCH 5/6] :bulb: Documented Vec2 --- src/leafgem/util/vec2.cr | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/leafgem/util/vec2.cr b/src/leafgem/util/vec2.cr index 063aa0a..3abcfb4 100644 --- a/src/leafgem/util/vec2.cr +++ b/src/leafgem/util/vec2.cr @@ -1,15 +1,47 @@ +# Vec2 is a struct used for almost all positioning code +# in leafgem +# +# Vec2 takes a Generic +# +# For example: +# +# ``` +# Vec2(Int32).new(0, 0) +# ``` +# +# This is done to support Floats, Ints, BigInts and so forth. +# +# Vec2 has `#from` to make this a little simpler +# +# ``` +# Vec2.from 0, 1 # => Vec2(Int32).new(0,1) +# ``` struct Leafgem::Util::Vec2(T) include Enumerable(T) include Comparable(T) + # The x position property x : T + + # The y position property y : T # Automatically determine from the two types + # + # ``` + # Vec2.from 0, 1 # => Vec2(Int32).new(0,1) + # ``` + # + # Supported multiple types + # + # ``` + # Vec2.from 0.3, 10 => Vec2(Float64 | Int32).new(0.3, 10) + # ``` def self.from(x, y) Vec2(typeof(x) | typeof(y)).new(x, y) end + # Creates a vec2 at position *x*, *y* def initialize(@x, @y); end def each @@ -17,6 +49,9 @@ struct Leafgem::Util::Vec2(T) yield y end + # Combined comparison operator. Returns 0 if self equals other, 1 if self is greater than other and -1 if self is smaller than other. + # + # Adds `#x` and `#y` together and compares the results def <=>(other : Vec2) @x + @y <=> other.x + other.y end @@ -67,6 +102,7 @@ struct Leafgem::Util::Vec2(T) (self - offset) / scale + pos end + # position self relative to the world def relative_to_world self.relative_to_world(Leafgem::Renderer.scale, Leafgem::Renderer.offset, Leafgem::Renderer.camera.pos) end From 073e51e52e24ba9dbdf4da2777568b146b2cbfeb Mon Sep 17 00:00:00 2001 From: Brecert <11599528+Brecert@users.noreply.github.com> Date: Thu, 27 Sep 2018 14:44:13 -0400 Subject: [PATCH 6/6] :bulb: Updated Vec2 --- src/leafgem/util/vec2.cr | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/leafgem/util/vec2.cr b/src/leafgem/util/vec2.cr index 3abcfb4..b0899cd 100644 --- a/src/leafgem/util/vec2.cr +++ b/src/leafgem/util/vec2.cr @@ -1,7 +1,7 @@ -# Vec2 is a struct used for almost all positioning code +# `Vec2` is a struct used for almost all positioning code # in leafgem # -# Vec2 takes a Generic +# A `Vec2` takes a Generic # # For example: # @@ -11,7 +11,7 @@ # # This is done to support Floats, Ints, BigInts and so forth. # -# Vec2 has `#from` to make this a little simpler +# `Vec2` has `#from` to make this a little simpler # # ``` # Vec2.from 0, 1 # => Vec2(Int32).new(0,1)