Created
May 7, 2021 19:11
-
-
Save ChaelCodes/58586f31876fa67f7e9c5aa9563c5643 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Game | |
attr_gtk | |
def tick | |
defaults | |
render | |
input | |
calc | |
end | |
def defaults | |
# Plaeyr - Player name | |
state.player.x ||= 640 | |
state.player.y ||= 360 | |
state.player.w ||= 32 | |
state.player.h ||= 32 | |
state.player.speed ||= 8 | |
state.player.attacks ||= [] | |
state.level.walls ||= [] | |
state.level.width ||= (200 * 16) | |
state.level.height ||= (200 * 16) | |
state.level.spawn_points ||= [ | |
{ x: 630, y: 350, w: 32, h: 32, spawn_rate: 60, hp: 5, damage: 0 } | |
] | |
if state.level.walls.empty? | |
state.level.walls = [ | |
{ x: 0, y: state.level.height - 16, w: state.level.width, h: 16 }, | |
{ x: 0, y: 0, w: state.level.width, h: 16 }, | |
{ x: 0, y: 0, w: 16, h: state.level.height }, | |
{ x: state.level.width - 16, y: 0, w: 1 * 16, h: state.level.height } | |
] | |
end | |
end | |
def calc_wall_collisions | |
state.level.walls.each do |wall| | |
if wall.intersect_rect? state.player.as_hash.merge(x: state.player.future.x) | |
state.player.future.x = state.player.x | |
x_collide = true | |
end | |
if wall.intersect_rect? state.player.as_hash.merge(y: state.player.future.y) | |
state.player.future.y = state.player.y | |
y_collide = true | |
end | |
break if x_collide && y_collide | |
end | |
state.player.as_hash.merge!(state.player.future.as_hash) | |
end | |
def calc_camera | |
state.camera.target_location = camera_position | |
state.camera.current_location ||= state.camera.target_location | |
dx = state.camera.target_location.x - state.camera.current_location.x | |
dy = state.camera.target_location.y - state.camera.current_location.y | |
state.camera.current_location.x += dx * 0.10 * 0.10 | |
state.camera.current_location.y += dy * 0.10 * 0.10 | |
end | |
def calc_player_attacks | |
attacks = [] | |
state.player.attacks.each do |a| | |
next if state.level.walls.find do |wall| | |
wall.intersect_rect? a | |
end | |
spawn_point = state.level.spawn_points.find do |spawn_point| | |
spawn_point.intersect_rect? a | |
end | |
spawn_point[:damage] += 1 if spawn_point | |
next if spawn_point | |
attacks << a.merge(x: a.x + (a[:direction].x * a[:velocity]), | |
y: a.y + (a[:direction].y * a[:velocity])) | |
end | |
state.player.attacks = [*attacks] | |
end | |
def remove_damaged_spawn_points | |
state.level.spawn_points.reject! do |spawn_point| | |
spawn_point[:damage] >= spawn_point[:hp] | |
end | |
end | |
def calc | |
calc_wall_collisions | |
calc_camera | |
calc_player_attacks | |
remove_damaged_spawn_points | |
end | |
def input | |
if inputs.directional_vector | |
state.player.direction = inputs.directional_vector | |
state.player.future.y = player_speed(:y) | |
state.player.future.x = player_speed(:x) | |
end | |
attack if inputs.keyboard.space || inputs.keyboard.enter | |
if inputs.mouse.click | |
state.level.walls << { | |
x: sixteenify(inputs.mouse.click.x, camera_position.x), | |
y: sixteenify(inputs.mouse.click.y, camera_position.y), | |
w: 16, h: 16 } | |
end | |
end | |
def attack | |
state.player.attacks << { | |
x: state.player.x + state.player.w.half, | |
y: state.player.y + state.player.h.half, | |
w: 4, | |
h: 4, | |
direction: { x: state.player.direction.x, y: state.player.direction.y }, | |
velocity: 16 | |
} | |
end | |
def player_speed(direction) | |
state.player.send(direction) + inputs.directional_vector.send(direction) * state.player.speed | |
end | |
def sixteenify(num, camera_offset) | |
offset = num % 16 | |
num - offset - camera_offset | |
end | |
def camera_position | |
center_offset_x = 640 - state.player.x | |
center_offset_y = 360 - state.player.y | |
center_offset_x = center_offset_x.clamp(-1920, 0) | |
center_offset_y = center_offset_y.clamp(-2480, 0) | |
{ x: center_offset_x, y: center_offset_y } | |
end | |
def render_background | |
outputs.solids << { x: 0, y: 0, w: 1280, h: 720, r: 30, g: 180, b: 230, a: 100 } | |
if !state.background_rendered | |
outputs[:background_tiles].w = state.level.width | |
outputs[:background_tiles].h = state.level.height | |
outputs[:background_tiles].solids << 1000.map do | |
{ x: state.level.width.randomize(:ratio), | |
y: state.level.height.randomize(:ratio), | |
w: 3 + 3.randomize(:ratio), h: 3 + 3.randomize(:ratio), r: 255, g: 255, b: 255, a: 128 } | |
end | |
state.background_rendered = true | |
end | |
if state.background_rendered | |
outputs[:camera].sprites << { x: 0, y: 0, w: outputs[:camera].w, h: outputs[:camera].h, path: :background_tiles } | |
end | |
end | |
def render_set_pieces | |
render_attacks | |
render_player | |
outputs[:camera].labels << [640, 500, 'Gauntlet!', 5, 1] | |
outputs[:camera].sprites << state.level.walls.map do |w| | |
w.merge(path: 'sprites/square/red.png') | |
end | |
outputs[:camera].sprites << state.level.spawn_points.map do |w| | |
a = w.merge(path: 'sprites/square/green.png', w: 32, h: 32) | |
a.merge(r: 255, g: 0, b: 0) if w[:damage] > 0 | |
a | |
end | |
end | |
def render_attacks | |
state.player.attacks.each do |attack| | |
outputs[:camera].sprites << attack.to_hash.merge(path: 'sprites/circles/black.png') | |
end | |
end | |
def render_player | |
outputs[:camera].sprites << state.player.as_hash.merge(path: "sprites/squid_down_#{(args.state.tick_count.idiv(10) % 2) + 1}.png") | |
end | |
def render | |
outputs.debug << [30, 30.from_top, "#{inputs.mouse.point}"].label | |
outputs.debug << [30, 50.from_top, "#{inputs.directional_vector}"].label | |
outputs[:camera].w = state.level.width | |
outputs[:camera].h = state.level.height | |
render_background | |
render_set_pieces | |
outputs.sprites << { x: camera_position.x, | |
y: camera_position.y, | |
w: outputs[:camera].w, | |
h: outputs[:camera].h, path: :camera } | |
end | |
def output_walls | |
puts state.level.walls | |
end | |
def reset_walls | |
state.level.walls = [] | |
end | |
end | |
def tick args | |
$game ||= Game.new | |
$game.args = args and $game.tick | |
end | |
def open_game_dir | |
$gtk.exec "powershell \"ii .\"" | |
end | |
$gtk.reset | |
$game = nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Did a twitch stream yayyyy! here's the result of it starting from scratch: https://gist.github.com/amirrajan/7a7d4a194c05f0a78172c54a29499634