More refactoring of sprite code.

Also, adjusted some rubocop rules (with justifications)
This commit is contained in:
Gordon Pedersen 2023-03-22 15:05:49 +11:00
parent 71df1d415c
commit 6b3ac755de
5 changed files with 134 additions and 97 deletions

21
.rubocop.yml Normal file
View file

@ -0,0 +1,21 @@
# the extension popped up a warning to include this:
AllCops:
NewCops: enable
# my functions have too many assignments, I get it.
# I have a lot to keep track of.
Metrics/AbcSize:
Enabled: false
# 10-line methods are /way/ too short
Metrics/MethodLength:
Max: 100
# Yeah, I've got constants with mutables in them. Doesn't bother me.
Style/MutableConstant:
Enabled: false
# I like my hash alignment neat.
Layout/HashAlignment:
EnforcedHashRocketStyle: table
EnforcedColonStyle: table
# DragonRuby required the file extension, therefore /not/ redundant.
RedundantFileExtensionInRequire:
Enabled: false

View file

@ -1,23 +1,33 @@
# frozen_string_literal: true
# first, third-party libraries
require 'lib/coalesce.rb' require 'lib/coalesce.rb'
# then, some basic classes required for lists of assets
require 'app/sprite_instance.rb'
# then, asset lists
require 'sprites/_list.rb'
# then, utility classes
require 'app/input.rb' require 'app/input.rb'
require 'app/sprite.rb' require 'app/sprite.rb'
require 'app/util.rb' require 'app/util.rb'
require 'app/sound.rb'
require 'app/constants.rb' require 'app/constants.rb'
require 'app/menu.rb' require 'app/menu.rb'
require 'app/scene.rb' require 'app/scene.rb'
require 'app/game_setting.rb' require 'app/game_setting.rb'
require 'app/sound.rb'
require 'app/text.rb' require 'app/text.rb'
# then, the scenes
require 'app/scenes/gameplay.rb' require 'app/scenes/gameplay.rb'
require 'app/scenes/main_menu.rb' require 'app/scenes/main_menu.rb'
require 'app/scenes/paused.rb' require 'app/scenes/paused.rb'
require 'app/scenes/settings.rb' require 'app/scenes/settings.rb'
require 'app/scenes/cube_tube.rb' require 'app/scenes/cube_tube.rb'
# NOTE: add all requires above this # finally, the main tick
# NOTE: add all other requires above this
require 'app/tick.rb' require 'app/tick.rb'

View file

@ -1,95 +1,7 @@
# frozen_string_literal: true
# Module for managing and interacting with sprites.
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
# nice to define a different way
SPRITES = {
train: SpriteInstance.new({ w: 1597, h: 540, path: 'sprites/train-1.png' }),
train_fore: SpriteInstance.new({ w: 1597, h: 540, path: 'sprites/train-2.png' }),
screen: SpriteInstance.new({ w: 250, h: 210, path: 'sprites/screen.png' }),
tunnel: SpriteInstance.new({ w: 267, h: 144, path: 'sprites/tunnel.png' }),
pause: SpriteInstance.new({ w: 16, h: 16, path: 'sprites/pause.png' }),
gray: SpriteInstance.new({ w: 176, h: 148, path: 'sprites/box/gray.png' }),
black: SpriteInstance.new({ w: 176, h: 148, path: 'sprites/box/black.png' }),
white: SpriteInstance.new({ w: 176, h: 148, path: 'sprites/box/white.png' }),
red: SpriteInstance.new({ w: 176, h: 148, path: 'sprites/box/red.png' }),
green: SpriteInstance.new({ w: 176, h: 148, path: 'sprites/box/green.png' }),
blue: SpriteInstance.new({ w: 176, h: 148, path: 'sprites/box/blue.png' }),
yellow: SpriteInstance.new({ w: 176, h: 148, path: 'sprites/box/yellow.png' }),
indigo: SpriteInstance.new({ w: 176, h: 148, path: 'sprites/box/indigo.png' }),
violet: SpriteInstance.new({ w: 176, h: 148, path: 'sprites/box/violet.png' }),
orange: SpriteInstance.new({ w: 176, h: 148, path: 'sprites/box/orange.png' }),
screen_s1: SpriteInstance.new({ w: 250, h: 210, path: 'sprites/screen-s1.png' }),
screen_s2: SpriteInstance.new({ w: 250, h: 210, path: 'sprites/screen-s2.png' }),
screen_s3: SpriteInstance.new({ w: 250, h: 210, path: 'sprites/screen-s3.png' }),
screen_s4: SpriteInstance.new({ w: 250, h: 210, path: 'sprites/screen-s4.png' }),
}
class << self class << self
def reset_all(args) def reset_all(args)
SPRITES.each { |_, v| args.gtk.reset_sprite(v) } SPRITES.each { |_, v| args.gtk.reset_sprite(v) }
@ -99,5 +11,4 @@ module Sprite
SPRITES.fetch(key) SPRITES.fetch(key)
end end
end end
end end

68
app/sprite_instance.rb Normal file
View file

@ -0,0 +1,68 @@
# frozen_string_literal: true
# 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

27
sprites/_list.rb Normal file
View file

@ -0,0 +1,27 @@
# frozen_string_literal: true
module Sprite
# annoying to track but useful for reloading with +i+ in debug mode; would be
# nice to define a different way
SPRITES = {
train: SpriteInstance.new({ w: 1597, h: 540, path: 'sprites/train-1.png' }),
train_fore: SpriteInstance.new({ w: 1597, h: 540, path: 'sprites/train-2.png' }),
screen: SpriteInstance.new({ w: 250, h: 210, path: 'sprites/screen.png' }),
tunnel: SpriteInstance.new({ w: 267, h: 144, path: 'sprites/tunnel.png' }),
pause: SpriteInstance.new({ w: 16, h: 16, path: 'sprites/pause.png' }),
gray: SpriteInstance.new({ w: 176, h: 148, path: 'sprites/box/gray.png' }),
black: SpriteInstance.new({ w: 176, h: 148, path: 'sprites/box/black.png' }),
white: SpriteInstance.new({ w: 176, h: 148, path: 'sprites/box/white.png' }),
red: SpriteInstance.new({ w: 176, h: 148, path: 'sprites/box/red.png' }),
green: SpriteInstance.new({ w: 176, h: 148, path: 'sprites/box/green.png' }),
blue: SpriteInstance.new({ w: 176, h: 148, path: 'sprites/box/blue.png' }),
yellow: SpriteInstance.new({ w: 176, h: 148, path: 'sprites/box/yellow.png' }),
indigo: SpriteInstance.new({ w: 176, h: 148, path: 'sprites/box/indigo.png' }),
violet: SpriteInstance.new({ w: 176, h: 148, path: 'sprites/box/violet.png' }),
orange: SpriteInstance.new({ w: 176, h: 148, path: 'sprites/box/orange.png' }),
screen_s1: SpriteInstance.new({ w: 250, h: 210, path: 'sprites/screen-s1.png' }),
screen_s2: SpriteInstance.new({ w: 250, h: 210, path: 'sprites/screen-s2.png' }),
screen_s3: SpriteInstance.new({ w: 250, h: 210, path: 'sprites/screen-s3.png' }),
screen_s4: SpriteInstance.new({ w: 250, h: 210, path: 'sprites/screen-s4.png' }),
}
end