Browse Source

some refactors

master
Nicholas Hayashi 4 years ago
parent
commit
3e052d27bd
  1. 5
      build.sh
  2. 56
      main.lua
  3. 2
      sound.lua
  4. 1
      src/entity.lua
  5. 13
      src/geometry.lua
  6. 5
      src/grid.lua
  7. 4
      src/gui.lua
  8. 2
      src/mob.lua
  9. 14
      src/projectile.lua
  10. 4
      src/tower.lua
  11. 14
      texture.lua

5
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 .

56
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()

2
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

1
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

13
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

5
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,

4
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

2
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

14
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

4
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

14
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)

Loading…
Cancel
Save