Skip to content

Commit

Permalink
Added helpers.
Browse files Browse the repository at this point in the history
  • Loading branch information
jyaif committed Nov 1, 2020
1 parent 398c7d7 commit d48ac4e
Show file tree
Hide file tree
Showing 18 changed files with 734 additions and 2 deletions.
9 changes: 9 additions & 0 deletions content/levels/useful_helpers/graphics.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
local helper = require("/dynamic/helpers/mesh_helpers.lua")

meshes = {{
vertexes = {{0,0}, {0,300}, {300,300}, {300,0}},
segments = {{0,1,2,3,0}},
colors = {0xff0000ff, 0xff0000ff, 0xff0000ff, 0xff0000ff}
}}

helper.add_vertical_cylinder_to_mesh(meshes[1], {150,150,-200}, 400, 400, 100, 0xff0000ff)
11 changes: 11 additions & 0 deletions content/levels/useful_helpers/helpers/angle_helpers.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
local helpers = {}

function helpers.random_angle()
return fmath.random_fixedpoint(0fx, fmath.tau())
end

function helpers.angle_from_ratio_of_tau(numerator, denominator)
return fmath.tau() * fmath.from_fraction(numerator, denominator)
end

return helpers
13 changes: 13 additions & 0 deletions content/levels/useful_helpers/helpers/boxes/box_graphics.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
local helpers = require("/dynamic/helpers/mesh_helpers.lua")

function create_box(color)
local mesh = helpers.new_mesh()
helpers.add_vertical_cylinder_to_mesh(mesh, {0,0,0}, 40, 22, 7, color)
return mesh
end

meshes = {
create_box(0xffff00ff), -- Yellow box (used by the Shield box)
create_box(0xffffffff), -- White box (used by the Shoot box)
}

66 changes: 66 additions & 0 deletions content/levels/useful_helpers/helpers/boxes/box_template.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@

local box = {}

--- Creates and returns a new box.
-- @param x fixedpoint: The x location of the center of the box.
-- @param y fixedpoint: The y location of the center of the box.
-- @param outer_mesh_info table: Contains the path to the main mesh of the box, and its index.
-- @param inner_mesh_info table: Contains the path to the inner mesh of the box that spins, and its index.
-- @param collision_callback function: The callback called when the ship of a player takes the box. Receives in argument the player's index and the ship's EntityId.
-- @param expiration int: The duration in game ticks of the bonus. If nil, the bonus lasts forever.
function box.new(x, y, outer_mesh_info, inner_mesh_info, collision_callback, expiration)
if expiration == nil then
expiration = -1
end

-- Create the main entity: it responds to collisions, holds the outer_mesh.
local id = pewpew.new_customizable_entity(x, y)
pewpew.customizable_entity_start_spawning(id, 15)
pewpew.customizable_entity_set_mesh(id, outer_mesh_info[1], outer_mesh_info[2])
pewpew.entity_set_radius(id, 20fx)

-- Create the secondary entity. It holds the inner_mesh and rotates.
local inner_mesh_id = pewpew.new_customizable_entity(x, y)
pewpew.customizable_entity_set_mesh(inner_mesh_id, inner_mesh_info[1], inner_mesh_info[2])
pewpew.customizable_entity_set_mesh_z(inner_mesh_id, 10fx)
local inner_mesh_angle = 0fx

local function collision_callback_wrapper(player_id, ship_id)
-- Remove the update callback to stop the rotation of the inner_mesh.
pewpew.entity_set_update_callback(id, nil)
-- Start the explosion
pewpew.customizable_entity_start_exploding(inner_mesh_id, 40)
pewpew.customizable_entity_start_exploding(id, 30)
-- Notify about the collision
if collision_callback ~= nil then
collision_callback(player_id, ship_id)
collision_callback = nil
end
end
pewpew.customizable_entity_set_player_collision_callback(id, collision_callback_wrapper)

local function update_callback()
inner_mesh_angle = inner_mesh_angle + 0.409fx
pewpew.customizable_entity_set_mesh_angle(inner_mesh_id, inner_mesh_angle, 1fx, 0.2047fx, 0.1365fx)
if expiration > 0 then
expiration = expiration - 1
if expiration < 180 then -- start to fade out the bonus
local alpha = (expiration * 255) // 180
local color = 0xffffff00 + alpha
pewpew.customizable_entity_set_mesh_color(id, color)
pewpew.customizable_entity_set_mesh_color(inner_mesh_id, color)
if expiration == 0 then
-- because of a bug in PewPew 0.0.83 and earlier, you must not destroy
-- the entity immediately because of the arrow pointing to it.
-- pewpew.entity_destroy(id)
pewpew.customizable_entity_start_exploding(id, 1)
pewpew.entity_destroy(inner_mesh_id)
end
end
end
end
pewpew.entity_set_update_callback(id, update_callback)
return id
end

return box
68 changes: 68 additions & 0 deletions content/levels/useful_helpers/helpers/boxes/cannon_box.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
local box = require("/dynamic/helpers/boxes/box_template.lua")
local floating_message = require("/dynamic/helpers/floating_message.lua")
local player_helpers = require("/dynamic/helpers/player_helpers.lua")


local cannon_box = {}

function cannon_box.new(x, y, type)

local duration = 100

local frequency
local cannon_type
local frame
local explosion_color
local msg

if type == 0 then
frequency = pewpew.CannonFrequency.FREQ_15
cannon_type = pewpew.CannonType.TRIPLE
frame = 1
explosion_color = 0x00ffffff
msg = "Triple"
elseif type == 1 then
frequency = pewpew.CannonFrequency.FREQ_30
cannon_type = pewpew.CannonType.DOUBLE_SWIPE
frame = 2
explosion_color = 0xff8080ff
msg = "Double swipe"
elseif type == 2 then
frequency = pewpew.CannonFrequency.FREQ_3
cannon_type = pewpew.CannonType.HEMISPHERE
frame = 3
explosion_color = 0xffff00ff
msg = "Hemisphere"
elseif type == 3 then
frequency = pewpew.CannonFrequency.FREQ_15
cannon_type = pewpew.CannonType.SINGLE
frame = 4
explosion_color = 0x8020ffff
msg = "AK-286"
elseif type == 4 then
frequency = pewpew.CannonFrequency.FREQ_15
cannon_type = pewpew.CannonType.DOUBLE
frame = 1
explosion_color = 0xff0000ff
msg = "Double"
else
return
end

pewpew.play_sound("/dynamic/helpers/boxes/bonus_spawn.lua", 0, x, y)

local b = box.new(x, y, {"/dynamic/helpers/boxes/box_graphics.lua", 1}, {"/dynamic/helpers/boxes/inner_box_graphics.lua", frame},
function(player_id, entity_id)
pewpew.configure_player_ship_weapon(
entity_id, {frequency = frequency, cannon = cannon_type, duration = duration})
pewpew.play_sound("/dynamic/helpers/boxes/cannon_pickup_sound.lua", 0, x, y)
pewpew.create_explosion(x, y, 0xffffffff, 1fx, 20)
pewpew.create_explosion(x, y, explosion_color, 0.4000fx, 20)
floating_message.new(x, y, msg, 1.2048fx, explosion_color, 4)
end, 400)
player_helpers.add_arrow(b, 0xffffffff)

return b
end

return cannon_box
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require("/dynamic/helpers/sound_parser.lua")

sound = parseSound('https://pewpew.live/jfxr/index.html#%7B%22_version%22%3A1%2C%22_name%22%3A%22Powerup%2030%22%2C%22_locked%22%3A%5B%5D%2C%22sampleRate%22%3A44100%2C%22attack%22%3A0.008769257937429664%2C%22sustain%22%3A0.1%2C%22sustainPunch%22%3A70%2C%22decay%22%3A0.3797596652271855%2C%22tremoloDepth%22%3A0%2C%22tremoloFrequency%22%3A10%2C%22frequency%22%3A1500%2C%22frequencySweep%22%3A1200%2C%22frequencyDeltaSweep%22%3A1600%2C%22repeatFrequency%22%3A14.558822107330508%2C%22frequencyJump1Onset%22%3A33%2C%22frequencyJump1Amount%22%3A100%2C%22frequencyJump2Onset%22%3A100%2C%22frequencyJump2Amount%22%3A-35%2C%22harmonics%22%3A0%2C%22harmonicsFalloff%22%3A0.5%2C%22waveform%22%3A%22square%22%2C%22interpolateNoise%22%3Atrue%2C%22vibratoDepth%22%3A0%2C%22vibratoFrequency%22%3A10%2C%22squareDuty%22%3A65%2C%22squareDutySweep%22%3A20%2C%22flangerOffset%22%3A0%2C%22flangerOffsetSweep%22%3A0%2C%22bitCrush%22%3A16%2C%22bitCrushSweep%22%3A0%2C%22lowPassCutoff%22%3A22050%2C%22lowPassCutoffSweep%22%3A0%2C%22highPassCutoff%22%3A0%2C%22highPassCutoffSweep%22%3A0%2C%22compression%22%3A1%2C%22normalization%22%3Atrue%2C%22amplification%22%3A60%7D')

sounds = {
sound
}
52 changes: 52 additions & 0 deletions content/levels/useful_helpers/helpers/boxes/inner_box_graphics.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
local helpers = require("/dynamic/helpers/mesh_helpers.lua")

function create_shield_inner_mesh()
local mesh = {}
local a = 3
local b = 10
local c = 0xffff00ff
mesh.vertexes = {
{b, -a}, {b, a}, {a, a}, {a, b}, {-a, b}, {-a, a}, {-b, a}, {-b, -a}, {-a, -a}, {-a, -b},
{a, -b}, {a, -a},
}
mesh.segments = {{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0}}
mesh.colors = c
return mesh
end

function create_hemisphere_mesh()
local mesh = helpers.new_mesh()
local number_of_points = 10
local radius = 10
local compute = function(i)
local angle = (i - 1) * math.pi / number_of_points
return {math.cos(angle) * radius, -5 + math.sin(angle) * radius}, 0xffff00ff
end
helpers.add_computed_segments_to_mesh(mesh, number_of_points, compute, false)
return mesh
end

meshes = {
-- The + in the ShieldBox
create_shield_inner_mesh(), -- The ✚ in the health box
{
-- The + bullet in the shoot box
vertexes = {{-8, 0}, {8, 0}, {0, -8}, {0 ,8}, {0,0,-8}, {0,0,8}},
segments = {{0, 1}, {2,3}, {4,5}},
colors = 0x11ffeeff
},
{
-- The ◇ bullet in the shoot box
vertexes = {{8, 0}, {0, 6}, {-8, 0}, {0, -6}, {0, 0, 6}},
segments = {{0, 1, 2, 3, 0}, {0, 4, 2}},
colors = 0xff4040ff
},
create_hemisphere_mesh(), -- The hemisphere icon for shoot box
{
-- three horizontal lines in the shoot box
vertexes = {{-10, -6}, {10, -6}, {-10, 6}, {10, 6}, {-10, 0}, {10, 0}},
segments = {{0, 1}, {2, 3}, {4, 5}},
colors = 0x9030ffff
},
}

41 changes: 41 additions & 0 deletions content/levels/useful_helpers/helpers/boxes/shield_box.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
local box = require("/dynamic/helpers/boxes/box_template.lua")
local floating_message = require("/dynamic/helpers/floating_message.lua")
local player_helpers = require("/dynamic/helpers/player_helpers.lua")

local shield_box = {}

function shield_box.new(x, y, resurrection_weapon_config)
local b = box.new(x, y, {"/dynamic/helpers/boxes/box_graphics.lua", 0}, {"/dynamic/helpers/boxes/inner_box_graphics.lua", 0}, function(player_id, ship_id)
pewpew.play_sound("/dynamic/helpers/boxes/shield_pickup_sound.lua", 0, x, y)
pewpew.create_explosion(x, y, 0xffff00ff, 0.4000fx, 40)

if pewpew.get_number_of_players() > 1 then -- try to resurrect
local players_that_lost = {}
for i = 0, pewpew.get_number_of_players() - 1 do
if i ~= player_id then
local conf = pewpew.get_player_configuration(i)
if conf.has_lost == true then
players_that_lost[#players_that_lost + 1] = i
end
end
end
if #players_that_lost > 0 then -- pick one player to resurrect
local index = fmath.random_int(1, #players_that_lost)
local player_id = players_that_lost[index]
local ship = player_helpers.new_player_ship(position[1], position[2], 0)
pewpew.configure_player_ship_weapon(ship, resurrection_weapon_config)
pewpew.configure_player(player_id, {has_lost = false})
pewpew.make_player_ship_transparent(ship, 120)
local new_message = floating_message.new(x, y, "reinstantiation", 2fx, 0xffff00ff, 3)
new_message.dz = 10
new_message.dalpha = 3
return
end
end
local conf = pewpew.get_player_configuration(player_id)
pewpew.configure_player(player_id, {shield = conf.shield + 1})
-- local new_message = floating_message.new(x, y, "Shield +1", 1.5, 0xffff00ff, 3)
end, 400)
end

return shield_box
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require("/dynamic/helpers/sound_parser.lua")

sound = parseSound('https://pewpew.live/jfxr/index.html#%7B%22_version%22%3A1%2C%22_name%22%3A%22Powerup%2057%22%2C%22_locked%22%3A%5B%5D%2C%22sampleRate%22%3A44100%2C%22attack%22%3A0%2C%22sustain%22%3A0.09%2C%22sustainPunch%22%3A40%2C%22decay%22%3A0.15%2C%22tremoloDepth%22%3A0%2C%22tremoloFrequency%22%3A10%2C%22frequency%22%3A1800%2C%22frequencySweep%22%3A800%2C%22frequencyDeltaSweep%22%3A1500%2C%22repeatFrequency%22%3A0%2C%22frequencyJump1Onset%22%3A33%2C%22frequencyJump1Amount%22%3A0%2C%22frequencyJump2Onset%22%3A66%2C%22frequencyJump2Amount%22%3A0%2C%22harmonics%22%3A0%2C%22harmonicsFalloff%22%3A0.5%2C%22waveform%22%3A%22whistle%22%2C%22interpolateNoise%22%3Atrue%2C%22vibratoDepth%22%3A0%2C%22vibratoFrequency%22%3A10%2C%22squareDuty%22%3A95%2C%22squareDutySweep%22%3A25%2C%22flangerOffset%22%3A0%2C%22flangerOffsetSweep%22%3A0%2C%22bitCrush%22%3A16%2C%22bitCrushSweep%22%3A0%2C%22lowPassCutoff%22%3A22050%2C%22lowPassCutoffSweep%22%3A0%2C%22highPassCutoff%22%3A0%2C%22highPassCutoffSweep%22%3A0%2C%22compression%22%3A1%2C%22normalization%22%3Atrue%2C%22amplification%22%3A70%7D')

sounds = {
sound
}
31 changes: 31 additions & 0 deletions content/levels/useful_helpers/helpers/color_helpers.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
local helper = {}

--- Returns a integer encoding a color.
-- @params r,g,b,a (integers in the [0,255] range): the red, green, blue, and alpha components of the color.
function helper.make_color(r, g, b, a)
local color = r * 256 + g
color = color * 256 + b
color = color * 256 + a
return color
end

-- Returns an integer encoding a color
-- @params color: an existing 32 bit color.
-- @params new_alpha: the alpha value of the new color.
function helper.make_color_with_alpha(color, new_alpha)
local alpha = color % 256
color = color - alpha + new_alpha
return color
end

-- Returns the color as a string that can be used to colorize text.
function helper.color_to_string(color)
local s = string.format("%x", color)
while string.len(s) < 8 do
s = "0" .. s
end
return "#" .. s
end

return helper

26 changes: 26 additions & 0 deletions content/levels/useful_helpers/helpers/floating_message.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
local color_helper = require("/dynamic/helpers/color_helpers.lua")

local floating_message = {}

function floating_message.new(x, y, text, scale, color, d_alpha)
local id = pewpew.new_customizable_entity(x, y)
local z = 0fx
local alpha = 255
pewpew.customizable_entity_set_mesh_scale(id, scale)


pewpew.entity_set_update_callback(id, function()
z = z + 20fx
local color = color_helper.make_color_with_alpha(color, alpha)
local color_s = color_helper.color_to_string(color)
pewpew.customizable_entity_set_string(id, color_s .. text)
pewpew.customizable_entity_set_mesh_z(id, z)
alpha = alpha - d_alpha
if alpha <= 0 then
pewpew.entity_destroy(id)
end
end)
return id
end

return floating_message
Loading

0 comments on commit d48ac4e

Please sign in to comment.