From 6b3ac755dec3f487251ec3cbe03740c2d80478bb Mon Sep 17 00:00:00 2001 From: Gordon Pedersen Date: Wed, 22 Mar 2023 15:05:49 +1100 Subject: [PATCH] More refactoring of sprite code. Also, adjusted some rubocop rules (with justifications) --- .rubocop.yml | 21 +++++++++ app/main.rb | 18 ++++++-- app/sprite.rb | 97 ++---------------------------------------- app/sprite_instance.rb | 68 +++++++++++++++++++++++++++++ sprites/_list.rb | 27 ++++++++++++ 5 files changed, 134 insertions(+), 97 deletions(-) create mode 100644 .rubocop.yml create mode 100644 app/sprite_instance.rb create mode 100644 sprites/_list.rb diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..9024484 --- /dev/null +++ b/.rubocop.yml @@ -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 \ No newline at end of file diff --git a/app/main.rb b/app/main.rb index 626b52a..8c4cd64 100644 --- a/app/main.rb +++ b/app/main.rb @@ -1,23 +1,33 @@ +# frozen_string_literal: true + +# first, third-party libraries 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/sprite.rb' require 'app/util.rb' +require 'app/sound.rb' require 'app/constants.rb' require 'app/menu.rb' require 'app/scene.rb' require 'app/game_setting.rb' -require 'app/sound.rb' require 'app/text.rb' +# then, the scenes 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 - +# finally, the main tick +# NOTE: add all other requires above this require 'app/tick.rb' diff --git a/app/sprite.rb b/app/sprite.rb index a2dccf3..4d6a1c7 100644 --- a/app/sprite.rb +++ b/app/sprite.rb @@ -1,95 +1,7 @@ +# frozen_string_literal: true + +# Module for managing and interacting with sprites. 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 def reset_all(args) SPRITES.each { |_, v| args.gtk.reset_sprite(v) } @@ -99,5 +11,4 @@ module Sprite SPRITES.fetch(key) end end - -end \ No newline at end of file +end diff --git a/app/sprite_instance.rb b/app/sprite_instance.rb new file mode 100644 index 0000000..e55a366 --- /dev/null +++ b/app/sprite_instance.rb @@ -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 diff --git a/sprites/_list.rb b/sprites/_list.rb new file mode 100644 index 0000000..12d3eae --- /dev/null +++ b/sprites/_list.rb @@ -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