Browse Source

save game button

master
Nicholas Hayashi 4 years ago
parent
commit
fc224715e4
  1. 2
      conf.lua
  2. 38
      main.lua
  3. BIN
      res/savegamehex.png
  4. 6
      src/entity.lua
  5. 35
      src/game.lua
  6. 2
      src/hexyz.lua
  7. 18
      src/mob.lua
  8. 6
      src/projectile.lua
  9. 6
      src/tower.lua
  10. 1
      texture.lua

2
conf.lua

@ -3,7 +3,7 @@ title = "hexyz"
author = "nick hayashi" author = "nick hayashi"
shortname = "hexyz" shortname = "hexyz"
version = "0.1.1" version = "0.1.1"
support_email = "hayashi.nicholas@gmail.com"
support_email = ""
copyright_message = "Copyright © 2021 Nick Hayashi" copyright_message = "Copyright © 2021 Nick Hayashi"
dev_region = "en" dev_region = "en"

38
main.lua

@ -44,6 +44,16 @@ require "src/projectile"
require "src/tower" require "src/tower"
function alert(message)
win.scene:append(
am.scale(3) ^ am.text(message)
:action(coroutine.create(function(self)
am.wait(am.tween(self, 1, { color = vec4(0) }))
win.scene:remove(self)
end))
)
end
function main_action(self) function main_action(self)
if win:key_pressed("escape") then if win:key_pressed("escape") then
if win.scene("game") then if win.scene("game") then
@ -59,6 +69,7 @@ function main_action(self)
end end
function make_main_scene_toolbelt() function make_main_scene_toolbelt()
local include_save_option = game
local options = { local options = {
false, false,
false, false,
@ -71,6 +82,14 @@ function make_main_scene_toolbelt()
end end
}, },
false, false,
include_save_option and {
texture = TEXTURES.SAVE_GAME_HEX,
action = function()
game_save()
alert("succesfully saved!")
end
} or false,
false,
{ {
texture = TEXTURES.LOAD_GAME_HEX, texture = TEXTURES.LOAD_GAME_HEX,
action = function() action = function()
@ -78,8 +97,7 @@ function make_main_scene_toolbelt()
game_init(am.load_state("save", "json")) game_init(am.load_state("save", "json"))
end end
}, },
false,
false,
false, false,
{ {
texture = TEXTURES.MAP_EDITOR_HEX, texture = TEXTURES.MAP_EDITOR_HEX,
@ -109,6 +127,8 @@ function make_main_scene_toolbelt()
local hvs = hex_vertical_spacing(spacing) local hvs = hex_vertical_spacing(spacing)
local grid_pixel_width = grid_width * hhs local grid_pixel_width = grid_width * hhs
local grid_pixel_height = grid_height * hvs local grid_pixel_height = grid_height * hvs
-- @TODO the vertical offset should be different depending on if this is the main menu or the pause menu
-- perhaps the map that makes the grid of hexes should be different as well
local pixel_offset = vec2(-grid_pixel_width/2, win.bottom + hex_height(spacing)/2 + 20) local pixel_offset = vec2(-grid_pixel_width/2, win.bottom + hex_height(spacing)/2 + 20)
local map = hex_rectangular_map(grid_width, grid_height, HEX_ORIENTATION.POINTY) local map = hex_rectangular_map(grid_width, grid_height, HEX_ORIENTATION.POINTY)
@ -161,7 +181,7 @@ function make_main_scene_toolbelt()
return am.translate(pixel_offset) ^ group return am.translate(pixel_offset) ^ group
end end
function main_scene(do_backdrop)
function main_scene(do_backdrop, do_logo)
local group = am.group() local group = am.group()
if do_backdrop then if do_backdrop then
@ -173,7 +193,7 @@ function main_scene(do_backdrop)
color = color{a=color.a - 0.1} color = color{a=color.a - 0.1}
local node = am.translate(hex_to_pixel(vec2(i, j), vec2(HEX_SIZE))) local node = am.translate(hex_to_pixel(vec2(i, j), vec2(HEX_SIZE)))
^ am.circle(vec2(0), HEX_SIZE, vec4(0), 6)
^ am.circle(vec2(0), HEX_SIZE, vec4(0), 6)
node"circle":action(am.tween(0.6, { color = color })) node"circle":action(am.tween(0.6, { color = color }))
@ -190,8 +210,12 @@ function main_scene(do_backdrop)
^ am.text(version, COLORS.WHITE, "right") ^ am.text(version, COLORS.WHITE, "right")
) )
local logo_height = 480
group:append(am.translate(0, win.top - 20 - logo_height/2) ^ am.sprite("res/logo.png"))
if do_logo then
group:append(
am.translate(0, win.top - 20 - TEXTURES.LOGO.height/2)
^ pack_texture_into_sprite(TEXTURES.LOGO, TEXTURES.LOGO.width, TEXTURES.LOGO.height)
)
end
group:append(make_main_scene_toolbelt()) group:append(make_main_scene_toolbelt())
@ -201,7 +225,7 @@ function main_scene(do_backdrop)
end end
win.scene = am.group( win.scene = am.group(
main_scene(true)
main_scene(true, true)
) )
noglobals() noglobals()

BIN
res/savegamehex.png

After

Width: 283  |  Height: 290  |  Size: 59 KiB

6
src/entity.lua

@ -55,12 +55,6 @@ function delete_entity(t, index)
t[index] = false -- leave empty indexes so other entities can learn that this entity was deleted t[index] = false -- leave empty indexes so other entities can learn that this entity was deleted
end end
function delete_all_entities()
delete_all_mobs()
delete_all_towers()
delete_all_projectiles()
end
function do_entity_updates() function do_entity_updates()
do_mob_updates() do_mob_updates()
do_tower_updates() do_tower_updates()

35
src/game.lua

@ -1,10 +1,14 @@
game = false -- flag to tell if there is a game running
state = {} state = {}
function game_end(saved_state)
delete_all_entities()
game_init(saved_state)
function game_end()
state = {}
game = false
-- @TODO
end end
function update_score(diff) state.score = state.score + diff end function update_score(diff) state.score = state.score + diff end
@ -45,7 +49,7 @@ local function get_initial_game_state(seed)
time_until_next_wave = 0, time_until_next_wave = 0,
time_until_next_break = 0, time_until_next_break = 0,
spawning = false, spawning = false,
spawn_chance = 0.01,
spawn_chance = 0.002,
last_mob_spawn_time = 0, last_mob_spawn_time = 0,
selected_tower_type = false, selected_tower_type = false,
@ -88,16 +92,6 @@ local function get_top_right_display_text(hex, evenq, centered_evenq, display_ty
return str return str
end end
function alert(message)
win.scene:append(
am.scale(3) ^ am.text(message)
:action(coroutine.create(function(self)
am.wait(am.tween(self, 1, { color = vec4(0) }))
win.scene:remove(self)
end))
)
end
-- initialized later, as part of the init of the toolbelt -- initialized later, as part of the init of the toolbelt
local function select_tower_type(tower_type) end local function select_tower_type(tower_type) end
local function select_toolbelt_button(i) end local function select_toolbelt_button(i) end
@ -118,7 +112,7 @@ end
local function game_pause() local function game_pause()
win.scene("game").paused = true win.scene("game").paused = true
win.scene:append(main_scene(false))
win.scene:append(main_scene(false, false))
end end
local function game_deserialize(json_string) local function game_deserialize(json_string)
@ -202,7 +196,7 @@ local function game_serialize()
return am.to_json(serialized) return am.to_json(serialized)
end end
local function game_save()
function game_save()
am.save_state("save", game_serialize(), "json") am.save_state("save", game_serialize(), "json")
log("succesfully saved!") log("succesfully saved!")
end end
@ -302,10 +296,6 @@ local function game_action(scene)
elseif win:key_pressed"f3" then elseif win:key_pressed"f3" then
game_save() game_save()
elseif win:key_pressed"f4" then
game_end(am.load_state("save", "json"))
return true
elseif win:key_pressed"tab" then elseif win:key_pressed"tab" then
if win:key_down"lshift" then if win:key_down"lshift" then
select_toolbelt_button((state.selected_toolbelt_button + table.count(TOWER_TYPE) - 2) % table.count(TOWER_TYPE) + 1) select_toolbelt_button((state.selected_toolbelt_button + table.count(TOWER_TYPE) - 2) % table.count(TOWER_TYPE) + 1)
@ -381,7 +371,7 @@ local function make_game_toolbelt()
return button return button
end end
local toolbelt_height = hex_height(HEX_SIZE) * 2
local toolbelt_height = win.height * 0.08
local function get_tower_tooltip_text_node(tower_type) local function get_tower_tooltip_text_node(tower_type)
local name = get_tower_name(tower_type) local name = get_tower_name(tower_type)
local placement_rules = get_tower_placement_rules_text(tower_type) local placement_rules = get_tower_placement_rules_text(tower_type)
@ -392,7 +382,7 @@ local function make_game_toolbelt()
return am.group():tag"tower_tooltip_text" return am.group():tag"tower_tooltip_text"
end end
local color = COLORS.PALE_SILVER
local color = COLORS.WHITE
return (am.translate(win.left + 10, win.bottom + toolbelt_height + 20) return (am.translate(win.left + 10, win.bottom + toolbelt_height + 20)
^ am.group{ ^ am.group{
am.translate(0, 60) am.translate(0, 60)
@ -566,6 +556,7 @@ function game_init(saved_state)
end end
end end
game = true
win.scene:remove("game") win.scene:remove("game")
win.scene:append(game_scene()) win.scene:append(game_scene())
end end

2
src/hexyz.lua

@ -44,7 +44,7 @@ local HEX_DEFAULT_ORIENTATION = HEX_ORIENTATION.FLAT
-- whenever |size| for a hexagon appears as an argument, if it isn't provided, use this -- whenever |size| for a hexagon appears as an argument, if it isn't provided, use this
-- 'size' here is distance from the centerpoint to any vertex in pixel -- 'size' here is distance from the centerpoint to any vertex in pixel
local HEX_DEFAULT_SIZE = vec2(20)
local HEX_DEFAULT_SIZE = vec2(26)
-- actual width (longest contained horizontal line) of the hexagon -- actual width (longest contained horizontal line) of the hexagon
function hex_width(size, orientation) function hex_width(size, orientation)

18
src/mob.lua

@ -72,6 +72,11 @@ function mob_die(mob, mob_index)
delete_entity(state.mobs, mob_index) delete_entity(state.mobs, mob_index)
end end
function mob_reach_center(mob, mob_index)
update_score(-(mob.health + mob.bounty))
delete_entity(state.mobs, mob_index)
end
local HEALTHBAR_WIDTH = HEX_PIXEL_WIDTH/2 local HEALTHBAR_WIDTH = HEX_PIXEL_WIDTH/2
local HEALTHBAR_HEIGHT = HEALTHBAR_WIDTH/4 local HEALTHBAR_HEIGHT = HEALTHBAR_WIDTH/4
function do_hit_mob(mob, damage, mob_index) function do_hit_mob(mob, damage, mob_index)
@ -148,8 +153,7 @@ local function resolve_frame_target_for_mob(mob, mob_index)
mob.hex = pixel_to_hex(mob.position, vec2(HEX_SIZE)) mob.hex = pixel_to_hex(mob.position, vec2(HEX_SIZE))
if mob.hex == HEX_GRID_CENTER then if mob.hex == HEX_GRID_CENTER then
update_score(-mob.health)
mob_die(mob, mob_index)
mob_reach_center(mob, mob_index)
return true return true
end end
@ -320,29 +324,19 @@ local function can_spawn_mob()
end end
if math.random() <= state.spawn_chance then if math.random() <= state.spawn_chance then
--log('yes %f', state.spawn_chance)
state.last_mob_spawn_time = state.time state.last_mob_spawn_time = state.time
return true return true
else else
--log('no %f', state.spawn_chance)
return false return false
end end
end end
function do_mob_spawning() function do_mob_spawning()
--if win:key_pressed"space" then
if can_spawn_mob() then if can_spawn_mob() then
--if #state.mobs < 1 then
make_and_register_mob(MOB_TYPE.BEEPER) make_and_register_mob(MOB_TYPE.BEEPER)
end end
end end
function delete_all_mobs()
for mob_index,mob in pairs(state.mobs) do
if mob then delete_entity(state.mobs, mob_index) end
end
end
function do_mob_updates() function do_mob_updates()
for mob_index,mob in pairs(state.mobs) do for mob_index,mob in pairs(state.mobs) do
if mob and mob.update then if mob and mob.update then

6
src/projectile.lua

@ -236,12 +236,6 @@ function projectile_deserialize(json_string)
return projectile return projectile
end end
function delete_all_projectiles()
for projectile_index,projectile in pairs(state.projectiles) do
if projectile then delete_entity(state.projectiles, projectile_index) end
end
end
function do_projectile_updates() function do_projectile_updates()
for projectile_index,projectile in pairs(state.projectiles) do for projectile_index,projectile in pairs(state.projectiles) do
if projectile and projectile.update then if projectile and projectile.update then

6
src/tower.lua

@ -511,12 +511,6 @@ function build_tower(hex, tower_type)
return tower return tower
end end
function delete_all_towers()
for tower_index,tower in pairs(state.towers) do
if tower then delete_entity(state.towers, tower_index) end
end
end
function do_tower_updates() function do_tower_updates()
for tower_index,tower in pairs(state.towers) do for tower_index,tower in pairs(state.towers) do
if tower and tower.update then if tower and tower.update then

1
texture.lua

@ -16,6 +16,7 @@ TEXTURES = {
SHADED_HEX = load_texture("res/shaded_hex.png"), SHADED_HEX = load_texture("res/shaded_hex.png"),
NEW_GAME_HEX = load_texture("res/newgamehex.png"), NEW_GAME_HEX = load_texture("res/newgamehex.png"),
SAVE_GAME_HEX = load_texture("res/savegamehex.png"),
LOAD_GAME_HEX = load_texture("res/loadgamehex.png"), LOAD_GAME_HEX = load_texture("res/loadgamehex.png"),
SETTINGS_HEX = load_texture("res/settingshex.png"), SETTINGS_HEX = load_texture("res/settingshex.png"),
MAP_EDITOR_HEX = load_texture("res/mapeditorhex.png"), MAP_EDITOR_HEX = load_texture("res/mapeditorhex.png"),

Loading…
Cancel
Save