Beginning refactor.
Starting with Sprites. Still don't quite know my way around Ruby yet, so it's a learning experience.
This commit is contained in:
parent
1c863f9090
commit
eef53251d1
5 changed files with 139 additions and 68 deletions
32
app/main.rb
32
app/main.rb
|
@ -1,21 +1,23 @@
|
||||||
require "app/input.rb"
|
require 'lib/coalesce.rb'
|
||||||
require "app/sprite.rb"
|
|
||||||
require "app/util.rb"
|
|
||||||
|
|
||||||
require "app/constants.rb"
|
require 'app/input.rb'
|
||||||
require "app/menu.rb"
|
require 'app/sprite.rb'
|
||||||
require "app/scene.rb"
|
require 'app/util.rb'
|
||||||
require "app/game_setting.rb"
|
|
||||||
require "app/sound.rb"
|
|
||||||
require "app/text.rb"
|
|
||||||
|
|
||||||
require "app/scenes/gameplay.rb"
|
require 'app/constants.rb'
|
||||||
require "app/scenes/main_menu.rb"
|
require 'app/menu.rb'
|
||||||
require "app/scenes/paused.rb"
|
require 'app/scene.rb'
|
||||||
require "app/scenes/settings.rb"
|
require 'app/game_setting.rb'
|
||||||
|
require 'app/sound.rb'
|
||||||
|
require 'app/text.rb'
|
||||||
|
|
||||||
require "app/scenes/cube_tube.rb"
|
require 'app/scenes/gameplay.rb'
|
||||||
|
require 'app/scenes/main_menu.rb'
|
||||||
|
require 'app/scenes/paused.rb'
|
||||||
|
require 'app/scenes/settings.rb'
|
||||||
|
|
||||||
|
require 'app/scenes/cube_tube.rb'
|
||||||
|
|
||||||
# NOTE: add all requires above this
|
# NOTE: add all requires above this
|
||||||
|
|
||||||
require "app/tick.rb"
|
require 'app/tick.rb'
|
||||||
|
|
|
@ -80,9 +80,9 @@ class CubeTubeGame
|
||||||
|
|
||||||
@bg_x = 0
|
@bg_x = 0
|
||||||
|
|
||||||
each 0..@grid_w - 1 do |x|
|
(0..@grid_w - 1).each do |x|
|
||||||
@grid[x] = []
|
@grid[x] = []
|
||||||
each 0..@grid_h - 1 do |y|
|
(0..@grid_h - 1).each do |y|
|
||||||
@grid[x][y] = 0
|
@grid[x][y] = 0
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -118,36 +118,27 @@ class CubeTubeGame
|
||||||
@bg_x %= @bg_w
|
@bg_x %= @bg_w
|
||||||
end
|
end
|
||||||
|
|
||||||
@args.outputs.sprites << [@bg_x,0,@bg_w,720,Sprite.for(:tunnel)]
|
Sprite.for(:tunnel).render(@args, { x: @bg_x, y: 0, w: @bg_w, h: 720 })
|
||||||
@args.outputs.sprites << [@bg_x-@bg_w,0,@bg_w,720,Sprite.for(:tunnel)]
|
Sprite.for(:tunnel).render(@args, { x: @bg_x - @bg_w, y: 0, w: @bg_w, h: 720 })
|
||||||
|
|
||||||
@args.outputs.sprites << [
|
Sprite.for(:train).render(@args, { x: 0, y: @grid_x - 64 })
|
||||||
0,
|
|
||||||
@grid_x - 64,
|
|
||||||
1597,
|
|
||||||
540,
|
|
||||||
Sprite.for(:train)
|
|
||||||
]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_foreground
|
def render_foreground
|
||||||
@args.outputs.sprites << [
|
Sprite.for(:train_fore).render(@args, { x: 0, y: @grid_x - 64 })
|
||||||
0,
|
|
||||||
@grid_x - 64,
|
|
||||||
1597,
|
|
||||||
540,
|
|
||||||
Sprite.for(:train_fore)
|
|
||||||
]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# x and y are positions in the grid, not pixels
|
# x and y are positions in the grid, not pixels
|
||||||
def render_block x, y, color
|
def render_block x, y, color
|
||||||
@args.outputs.sprites << [
|
@sprite_index[color].render(
|
||||||
(1280 - @grid_y) - (y * @blocksize) - 6,
|
@args,
|
||||||
@grid_x + (x * @blocksize),
|
{
|
||||||
@blocksize + 6, @blocksize,
|
x: (1280 - @grid_y) - (y * @blocksize) - 6,
|
||||||
@sprite_index[color]
|
y: @grid_x + (x * @blocksize),
|
||||||
]
|
w: @blocksize + 6,
|
||||||
|
h: @blocksize
|
||||||
|
}
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_grid
|
def render_grid
|
||||||
|
@ -182,10 +173,7 @@ class CubeTubeGame
|
||||||
screen_w = 250
|
screen_w = 250
|
||||||
screen_h = 200
|
screen_h = 200
|
||||||
|
|
||||||
@args.outputs.sprites << [
|
Sprite.for(:screen).render(@args, { x: screen_x, y: screen_y + 10, w: screen_w, h: screen_h + 10 })
|
||||||
screen_x, screen_y + 10, screen_w, screen_h + 10,
|
|
||||||
Sprite.for(:screen)
|
|
||||||
]
|
|
||||||
|
|
||||||
next_piece = @line_clear_timer <= 0 ? @next_piece : @current_piece
|
next_piece = @line_clear_timer <= 0 ? @next_piece : @current_piece
|
||||||
# render_grid_border *@next_piece_box, 8
|
# render_grid_border *@next_piece_box, 8
|
||||||
|
@ -207,10 +195,7 @@ class CubeTubeGame
|
||||||
:screen_s4
|
:screen_s4
|
||||||
end
|
end
|
||||||
|
|
||||||
@args.outputs.sprites << [
|
Sprite.for(screen_s).render(@args, { x: screen_x, y: screen_y + 10, w: screen_w, h: screen_h + 10 })
|
||||||
screen_x, screen_y + 10, screen_w, screen_h + 10,
|
|
||||||
Sprite.for(screen_s)
|
|
||||||
]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_score
|
def render_score
|
||||||
|
|
|
@ -46,6 +46,5 @@ module Scene
|
||||||
|
|
||||||
args.outputs.labels << labels
|
args.outputs.labels << labels
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
107
app/sprite.rb
107
app/sprite.rb
|
@ -1,26 +1,93 @@
|
||||||
module Sprite
|
module Sprite
|
||||||
|
# Create type with ALL sprite properties AND primitive_marker
|
||||||
|
class SpriteInstance
|
||||||
|
attr_accessor :x, :y, :w, :h, :path, :angle, :a, :r, :g, :b,
|
||||||
|
:source_x, :source_y, :source_w, :source_h,
|
||||||
|
:tile_x, :tile_y, :tile_w, :tile_h,
|
||||||
|
:flip_horizontally, :flip_vertically,
|
||||||
|
:angle_anchor_x, :angle_anchor_y, :blendmode_enum
|
||||||
|
|
||||||
|
def primitive_marker
|
||||||
|
:sprite
|
||||||
|
end
|
||||||
|
|
||||||
|
def initialize opts
|
||||||
|
@x = opts[:x]
|
||||||
|
@y = opts[:y]
|
||||||
|
@w = opts[:w]
|
||||||
|
@h = opts[:h]
|
||||||
|
@path = opts[:path]
|
||||||
|
@angle = opts[:angle]
|
||||||
|
@a = opts[:a]
|
||||||
|
@r = opts[:r]
|
||||||
|
@g = opts[:g]
|
||||||
|
@b = opts[:b]
|
||||||
|
@source_x = opts[:source_x]
|
||||||
|
@source_y = opts[:source_y]
|
||||||
|
@source_w = opts[:source_w]
|
||||||
|
@source_h = opts[:source_h]
|
||||||
|
@tile_x = opts[:tile_x]
|
||||||
|
@tile_y = opts[:tile_y]
|
||||||
|
@tile_w = opts[:tile_w]
|
||||||
|
@tile_h = opts[:tile_h]
|
||||||
|
@flip_horizontally = opts[:flip_horizontally]
|
||||||
|
@flip_vertically = opts[:flip_vertically]
|
||||||
|
@angle_anchor_x = opts[:angle_anchor_x]
|
||||||
|
@angle_anchor_y = opts[:angle_anchor_y]
|
||||||
|
@blendmode_enum = opts[:blendmode_enum]
|
||||||
|
end
|
||||||
|
|
||||||
|
def render(args, opts = {})
|
||||||
|
args.outputs.sprites << [
|
||||||
|
opts[:x]._?(@x),
|
||||||
|
opts[:y]._?(@y),
|
||||||
|
opts[:w]._?(@w),
|
||||||
|
opts[:h]._?(@h),
|
||||||
|
opts[:path]._?(@path),
|
||||||
|
opts[:angle]._?(@angle),
|
||||||
|
opts[:a]._?(@a),
|
||||||
|
opts[:r]._?(@r),
|
||||||
|
opts[:g]._?(@g),
|
||||||
|
opts[:b]._?(@b),
|
||||||
|
opts[:source_x]._?(@source_x),
|
||||||
|
opts[:source_y]._?(@source_y),
|
||||||
|
opts[:source_w]._?(@source_w),
|
||||||
|
opts[:source_h]._?(@source_h),
|
||||||
|
opts[:tile_x]._?(@tile_x),
|
||||||
|
opts[:tile_y]._?(@tile_y),
|
||||||
|
opts[:tile_w]._?(@tile_w),
|
||||||
|
opts[:tile_h]._?(@tile_h),
|
||||||
|
opts[:flip_horizontally]._?(@flip_horizontally),
|
||||||
|
opts[:flip_vertically]._?(@flip_vertically),
|
||||||
|
opts[:angle_anchor_x]._?(@angle_anchor_x),
|
||||||
|
opts[:angle_anchor_y]._?(@angle_anchor_y),
|
||||||
|
opts[:blendmode_enum]._?(@blendmode_enum)
|
||||||
|
]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# annoying to track but useful for reloading with +i+ in debug mode; would be
|
# annoying to track but useful for reloading with +i+ in debug mode; would be
|
||||||
# nice to define a different way
|
# nice to define a different way
|
||||||
SPRITES = {
|
SPRITES = {
|
||||||
train: 'sprites/train-1.png',
|
train: SpriteInstance.new({ w: 1597, h: 540, path: 'sprites/train-1.png' }),
|
||||||
train_fore: 'sprites/train-2.png',
|
train_fore: SpriteInstance.new({ w: 1597, h: 540, path: 'sprites/train-2.png' }),
|
||||||
screen: 'sprites/screen.png',
|
screen: SpriteInstance.new({ w: 250, h: 210, path: 'sprites/screen.png' }),
|
||||||
screen_s1: 'sprites/screen-s1.png',
|
tunnel: SpriteInstance.new({ w: 267, h: 144, path: 'sprites/tunnel.png' }),
|
||||||
screen_s2: 'sprites/screen-s2.png',
|
pause: SpriteInstance.new({ w: 16, h: 16, path: 'sprites/pause.png' }),
|
||||||
screen_s3: 'sprites/screen-s3.png',
|
gray: SpriteInstance.new({ w: 176, h: 148, path: 'sprites/box/gray.png' }),
|
||||||
screen_s4: 'sprites/screen-s4.png',
|
black: SpriteInstance.new({ w: 176, h: 148, path: 'sprites/box/black.png' }),
|
||||||
tunnel: 'sprites/tunnel.png',
|
white: SpriteInstance.new({ w: 176, h: 148, path: 'sprites/box/white.png' }),
|
||||||
pause: 'sprites/pause.png',
|
red: SpriteInstance.new({ w: 176, h: 148, path: 'sprites/box/red.png' }),
|
||||||
gray: 'sprites/box/gray.png',
|
green: SpriteInstance.new({ w: 176, h: 148, path: 'sprites/box/green.png' }),
|
||||||
black: 'sprites/box/black.png',
|
blue: SpriteInstance.new({ w: 176, h: 148, path: 'sprites/box/blue.png' }),
|
||||||
white: 'sprites/box/white.png',
|
yellow: SpriteInstance.new({ w: 176, h: 148, path: 'sprites/box/yellow.png' }),
|
||||||
red: 'sprites/box/red.png',
|
indigo: SpriteInstance.new({ w: 176, h: 148, path: 'sprites/box/indigo.png' }),
|
||||||
green: 'sprites/box/green.png',
|
violet: SpriteInstance.new({ w: 176, h: 148, path: 'sprites/box/violet.png' }),
|
||||||
blue: 'sprites/box/blue.png',
|
orange: SpriteInstance.new({ w: 176, h: 148, path: 'sprites/box/orange.png' }),
|
||||||
yellow: 'sprites/box/yellow.png',
|
screen_s1: SpriteInstance.new({ w: 250, h: 210, path: 'sprites/screen-s1.png' }),
|
||||||
indigo: 'sprites/box/indigo.png',
|
screen_s2: SpriteInstance.new({ w: 250, h: 210, path: 'sprites/screen-s2.png' }),
|
||||||
violet: 'sprites/box/violet.png',
|
screen_s3: SpriteInstance.new({ w: 250, h: 210, path: 'sprites/screen-s3.png' }),
|
||||||
orange: 'sprites/box/orange.png'
|
screen_s4: SpriteInstance.new({ w: 250, h: 210, path: 'sprites/screen-s4.png' }),
|
||||||
}
|
}
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
|
@ -32,5 +99,5 @@ module Sprite
|
||||||
SPRITES.fetch(key)
|
SPRITES.fetch(key)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
|
end
|
18
lib/coalesce.rb
Normal file
18
lib/coalesce.rb
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
# Handy null coalesing operator, taken from
|
||||||
|
# https://github.com/kibiz0r/coalesce/blob/bc4389ae8a8bb456cd10ec8a3523df1fc775124f/lib/coalesce.rb
|
||||||
|
|
||||||
|
class Object
|
||||||
|
def _?(x = nil)
|
||||||
|
self
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class NilClass
|
||||||
|
def _?(x = nil)
|
||||||
|
if block_given?
|
||||||
|
yield
|
||||||
|
else
|
||||||
|
x
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue