From 3e052d27bd08c6051501d6e16b84f903a67024d6 Mon Sep 17 00:00:00 2001 From: Nicholas Hayashi Date: Fri, 8 Jan 2021 04:38:59 -0500 Subject: [PATCH] some refactors --- build.sh | 5 ++++- main.lua | 56 +++++++++++++++++----------------------------- sound.lua | 2 +- src/entity.lua | 1 - src/geometry.lua | 13 +++++++++++ src/grid.lua | 5 +++-- src/gui.lua | 4 ---- src/mob.lua | 2 +- src/projectile.lua | 14 +++++++----- src/tower.lua | 4 +++- texture.lua | 14 ++++++------ 11 files changed, 61 insertions(+), 59 deletions(-) create mode 100644 src/geometry.lua diff --git a/build.sh b/build.sh index 992f258..f48e0a4 100755 --- a/build.sh +++ b/build.sh @@ -1 +1,4 @@ -amulet export -r -d bin/ -mac -a . + +# add -mac, -windows, -linux, or -html if you only want one target +rm -ri bin/* +amulet export -r -d bin/ -a . diff --git a/main.lua b/main.lua index 139fe89..8770881 100644 --- a/main.lua +++ b/main.lua @@ -5,15 +5,16 @@ math.random() math.random() math.random() +-- assets/non-or-trivial code require "color" require "sound" require "texture" -require "src/hexyz" require "src/entity" require "src/extra" +require "src/geometry" +require "src/hexyz" require "src/grid" -require "src/gui" require "src/mob" require "src/projectile" require "src/tower" @@ -24,38 +25,38 @@ WIN = am.window{ width = 1920, height = 1080, title = "hexyz", - resizable = false + } OFF_SCREEN = vec2(WIN.width * 2) -- arbitrary pixel position that is garunteed to be off screen WORLD = false -- root scene node of everything considered to be in the game world -TIME = 0 -- runtime of the current game in seconds +TIME = 0 -- runtime of the current game in seconds (not whole program runtime) SCORE = 0 -- score of the player MONEY = 0 -- available resources MOUSE = false -- position of the mouse at the start of every frame, if an action is tracking it -RAND = 0 -- result of first call to math.random() this frame -- global audio settings MUSIC_VOLUME = 0.1 SFX_VOLUME = 0.1 +-- top right display types local TRDTS = { NOTHING = -1, CENTERED_EVENQ = 0, EVENQ = 1, HEX = 2, PLATFORM = 3, - PERF = 4 + PERF = 4, + SEED = 5 } +local TRDT = TRDTS.SEED -local TRDT = TRDTS.CENTERED_EVENQ local function game_action(scene) if SCORE < 0 then game_end() end - TIME = am.current_time() + TIME = TIME + am.delta_time SCORE = SCORE + am.delta_time - RAND = math.random() MOUSE = WIN:mouse_position() local hex = pixel_to_hex(MOUSE - WORLDSPACE_COORDINATE_OFFSET) @@ -75,19 +76,8 @@ local function game_action(scene) end end - if WIN:key_pressed"escape" then - pause() - - elseif WIN:key_pressed"f2" then - delete_all_entities() - WIN.scene = game_scene() - - elseif WIN:key_pressed"f3" then - TRDT = (TRDT + 1) % #table.keys(TRDTS) - - elseif WIN:key_pressed"f4" then - log(HEX_MAP.seed) - print(HEX_MAP.seed) + if WIN:key_pressed"escape" then game_end() + elseif WIN:key_pressed"f1" then TRDT = (TRDT + 1) % #table.keys(TRDTS) end if tile and hot then @@ -102,28 +92,27 @@ local function game_action(scene) do local str = "" if TRDT == TRDTS.CENTERED_EVENQ then - str = string.format("%d,%d (cevenq)", centered_evenq.x, centered_evenq.y) + str = centered_evenq.x .. "," .. centered_evenq.y .. " (cevenq)" elseif TRDT == TRDTS.EVENQ then - str = string.format("%d,%d (evenq)", evenq.x, evenq.y) + str = evenq.x .. "," .. evenq.y .. " (evenq)" elseif TRDT == TRDTS.HEX then - str = string.format("%d,%d (hex)", hex.x, hex.y) + str = hex.x .. "," .. hex.y .. " (hex)" elseif TRDT == TRDTS.PLATFORM then str = string.format("%s %s lang %s", am.platform, am.version, am.language()) elseif TRDT == TRDTS.PERF then str = table.tostring(am.perf_stats()) + + elseif TRDT == TRDTS.SEED then + str = "SEED: " .. HEX_MAP.seed end WIN.scene"coords".text = str end end -function pause() - WORLD"group".paused = true -end - function game_end() WIN.scene.paused = true @@ -184,17 +173,12 @@ function game_scene() } scene:action(game_action) - scene:action(am.play(SOUNDS.TRACK1)) + --scene:action(am.play(SOUNDS.TRACK1)) return scene end -function get_debug_string() - -end - -require "texture" load_textures() -WIN.scene = am.scale(1) ^ game_scene() +WIN.scene = game_scene() noglobals() diff --git a/sound.lua b/sound.lua index ff6ab41..5f2d3f1 100644 --- a/sound.lua +++ b/sound.lua @@ -16,7 +16,7 @@ SOUNDS = { RANDOM5 = 36680709, -- audio buffers - TRACK1 = am.track(am.load_audio("./res/track1.ogg"), true, 1, 0.1) + TRACK1 = am.track(am.load_audio("res/track1.ogg"), true, 1, 0.1) } -- play a sound with variable pitch diff --git a/src/entity.lua b/src/entity.lua index 7dfca12..240eafd 100644 --- a/src/entity.lua +++ b/src/entity.lua @@ -11,7 +11,6 @@ ENTITIES = {} -- entity structure: -- { -- TOB - number - time of birth, const --- -- hex - vec2 - current occupied hex, if any -- position - vec2 - current pixel position of it's translate (forced parent) node -- update - function - runs every frame with itself and its index as an argument diff --git a/src/geometry.lua b/src/geometry.lua new file mode 100644 index 0000000..414f763 --- /dev/null +++ b/src/geometry.lua @@ -0,0 +1,13 @@ + + +function circles_intersect(center1, center2, radius1, radius2) + return (((center1.x - center2.x)^2 + (center1.y - center2.y)^2)^0.5) <= (radius1 + radius2) +end + +function point_in_rect(point, rect) + return point.x > rect.x1 + and point.x < rect.x2 + and point.y > rect.y1 + and point.y < rect.y2 +end + diff --git a/src/grid.lua b/src/grid.lua index 2ea2459..c9484a1 100644 --- a/src/grid.lua +++ b/src/grid.lua @@ -28,7 +28,7 @@ end GRID_PIXEL_DIMENSIONS = grid_pixel_dimensions() WORLDSPACE_COORDINATE_OFFSET = -GRID_PIXEL_DIMENSIONS/2 -HEX_GRID_INTERACTABLE_REGION_PADDING = 3 +HEX_GRID_INTERACTABLE_REGION_PADDING = 4 function is_interactable(tile, evenq) return point_in_rect(evenq, { @@ -97,7 +97,8 @@ function random_map(seed) , ((-evenq.y - HEX_GRID_HEIGHT/2) / HEX_GRID_HEIGHT) ^ 2)) local color = color_at(noise) - mask - local node = am.circle(hex_to_pixel(vec2(i, j)), HEX_SIZE, color, 6) + local node = am.translate(hex_to_pixel(vec2(i, j))) + ^ am.circle(vec2(0), HEX_SIZE, color, 6) map.set(i, j, { elevation = noise, diff --git a/src/gui.lua b/src/gui.lua index 49551de..99f2b9d 100644 --- a/src/gui.lua +++ b/src/gui.lua @@ -8,10 +8,6 @@ function register_widget(id, poll) widgets[id] = { id = id, poll = poll } end -function point_in_rect(point, rect) - return point.x > rect.x1 and point.x < rect.x2 and point.y > rect.y1 and point.y < rect.y2 -end - function set_hot(id) if not active then hot = { id = id } end end diff --git a/src/mob.lua b/src/mob.lua index fe80ac5..94e682b 100644 --- a/src/mob.lua +++ b/src/mob.lua @@ -136,7 +136,7 @@ local function make_and_register_mob() mob.health = 10 mob.speed = 1 mob.bounty = 5 - mob.hurtbox_radius = 15 + mob.hurtbox_radius = 100 end local SPAWN_CHANCE = 100 diff --git a/src/projectile.lua b/src/projectile.lua index 5ee3086..0d740c5 100644 --- a/src/projectile.lua +++ b/src/projectile.lua @@ -1,6 +1,6 @@ -function make_and_register_projectile(hex, vector, velocity) +function make_and_register_projectile(hex, vector, velocity, damage, hitbox_radius) local projectile = make_and_register_entity( -- type ENTITY_TYPE.PROJECTILE, @@ -8,7 +8,7 @@ function make_and_register_projectile(hex, vector, velocity) hex, -- node - am.circle(vec2(0), 2, COLORS.CLARET), + am.circle(vec2(0), hitbox_radius - 1, COLORS.CLARET), -- update function function(_projectile, _projectile_index) @@ -17,7 +17,11 @@ function make_and_register_projectile(hex, vector, velocity) _projectile.hex = pixel_to_hex(_projectile.position) local mob_index,mob = mob_on_hex(_projectile.hex) - if mob and math.distance(mob.position, _projectile.position) > math.abs(_projectile.hitbox_radius - mob.hurtbox_radius) then + if mob and circles_intersect(mob.position + , _projectile.position + , mob.hurtbox_radius + , _projectile.hitbox_radius) then + do_hit_mob(mob, _projectile.damage, mob_index) delete_entity(_projectile_index) WORLD:action(vplay_sound(SOUNDS.HIT1)) @@ -35,7 +39,7 @@ function make_and_register_projectile(hex, vector, velocity) projectile.vector = vector projectile.velocity = velocity - projectile.damage = 5 - projectile.hitbox_radius = 10 + projectile.damage = damage + projectile.hitbox_radius = hitbox_radius end diff --git a/src/tower.lua b/src/tower.lua index fa4c58e..ecfdaa4 100644 --- a/src/tower.lua +++ b/src/tower.lua @@ -38,7 +38,9 @@ function make_and_register_tower(hex) make_and_register_projectile( _tower.hex, math.normalize(hex_to_pixel(entity.hex) - _tower.position), - 15 + 15, + 5, + 4 ) _tower.last_shot_time = TIME diff --git a/texture.lua b/texture.lua index 269f6f1..9ddec91 100644 --- a/texture.lua +++ b/texture.lua @@ -1,16 +1,16 @@ function load_textures() - TEX_MARQUIS = am.texture2d("./res/marquis.png") + TEX_MARQUIS = am.texture2d("res/marquis.png") - TEX_ARROW = am.texture2d("./res/arrow.png") + TEX_ARROW = am.texture2d("res/arrow.png") - TEX_WALL_CLOSED = am.texture2d("./res/wall_closed.png") - TEX_TOWER1 = am.texture2d("./res/tower1.png") - TEX_TOWER2 = am.texture2d("./res/tower2.png") + TEX_WALL_CLOSED = am.texture2d("res/wall_closed.png") + TEX_TOWER1 = am.texture2d("res/tower1.png") + TEX_TOWER2 = am.texture2d("res/tower2.png") - TEX_MOB1_1 = am.texture2d("./res/mob1_1.png") - TEX_MOB2_1 = am.texture2d("./res/mob2_1.png") + TEX_MOB1_1 = am.texture2d("res/mob1_1.png") + TEX_MOB2_1 = am.texture2d("res/mob2_1.png") end function pack_texture_into_sprite(texture, width, height)