view img_tool.rb @ 0:1eef88068f9f tip

initial commit of maze game source
author ferencd
date Sun, 15 Sep 2019 11:46:47 +0200
parents
children
line wrap: on
line source
require 'fileutils'

imgfile = "download.png"
target_dir = "work"

imgfile = ARGV[0] if ARGV.length >= 1
target_dir = ARGV[1] if ARGV.length >= 2

puts "working on image: #{imgfile} going into #{target_dir}"

# Does it exist?
abort("#{imgfile} does not exist. Giving up.") if not File.exist?(imgfile)

# Cutting the file into long pieces.
abort("#{imgfile} could not be split up into lines") if not system("convert #{imgfile} -crop 832x64  +repage  +adjoin  row_%02d.png")

# Relocating all pieces into their own directory.
# The following is the list:
# 0  - Spell cast up      - scu
# 1  - Spell cast left    - scl
# 2  - Spell cast down    - scd
# 3  - Spell cast right   -	scr
# 4  - Thrust up          - tru
# 5  - Thrust left        - trl
# 6  - Thrust down        - trd
# 7  - Thrust right       - trr
# 8  - Walk up            - wlu
# 9  - Walk left          - wll
# 10 - Walk down          - wld
# 11 - Walk right         - wlr
# 12 - Slash up           - slu
# 13 - Slash left         - sll
# 14 - Slash down         - sld
# 15 - Slash right        - slr
# 16 - Shoot up           - shu
# 17 - Shoot left         - shl
# 18 - Shoot down         - shd
# 19 - Shoot right        - shr
# 20 - Hurt               - hrt

maps = 	{
	    "00" => {:name => "scu", :count => 7, :base_name => "sc", :direction => "u"},
	    "01" => {:name => "scl", :count => 7, :base_name => "sc", :direction => "l"},
	    "02" => {:name => "scd", :count => 7, :base_name => "sc", :direction => "d"},
	    "03" => {:name => "scr", :count => 7, :base_name => "sc", :direction => "r"},
	    "04" => {:name => "tru", :count => 8, :base_name => "tr", :direction => "u"},
	    "05" => {:name => "trl", :count => 8, :base_name => "tr", :direction => "l"},
	    "06" => {:name => "trd", :count => 8, :base_name => "tr", :direction => "d"},
	    "07" => {:name => "trr", :count => 8, :base_name => "tr", :direction => "r"},
		  "08" => {:name => "wlu", :count => 9, :base_name => "wl", :direction => "u"},
		  "09" => {:name => "wll", :count => 9, :base_name => "wl", :direction => "l"},
			"10" => {:name => "wld", :count => 9, :base_name => "wl", :direction => "d"},
			"11" => {:name => "wlr", :count => 9, :base_name => "wl", :direction => "r"},
			"12" => {:name => "slu", :count => 6, :base_name => "sl", :direction => "u"},
			"13" => {:name => "sll", :count => 6, :base_name => "sl", :direction => "l"},
			"14" => {:name => "sld", :count => 6, :base_name => "sl", :direction => "d"},
			"15" => {:name => "slr", :count => 6, :base_name => "sl", :direction => "r"},
			"16" => {:name => "shu", :count =>13, :base_name => "sh", :direction => "u"},
			"17" => {:name => "shl", :count =>13, :base_name => "sh", :direction => "l"},
			"18" => {:name => "shd", :count =>13, :base_name => "sh", :direction => "d"},
			"19" => {:name => "shr", :count =>13, :base_name => "sh", :direction => "r"},
			"20" => {:name => "hrt", :count => 6, :base_name => "hr", :direction => "t"}
		}


# Prepare the work directory
abort("Cannot create #{target_dir}") if not FileUtils::mkdir_p "#{target_dir}"

maps.each do |key, value_map|
	
	# Create the directory where the file will go
	current_target_dir = target_dir + "/" + value_map[:base_name]
	abort("Cannot create #{current_target_dir} dir") if not FileUtils::mkdir_p "#{current_target_dir}"

	# The current file
	current_file = "row_#{key}.png"
	FileUtils.mv(current_file, "#{current_target_dir}")

	#Save current location
	current_dir = Dir.pwd

	# Create the template of the outcoming files
	target_filenames = value_map[:direction] + "_" + value_map[:base_name] + "_%01d.png"
	# now go into the directory and chop up the images
	Dir.chdir(current_target_dir)
	abort("Cannot chop up #{current_file}") if not system("convert #{current_file} -crop 64x64  +repage  +adjoin -scene 1 #{target_filenames}")

	# Now remove all files that are after count
	(value_map[:count] + 1 .. 13).each do |num|
		to_delete_filename = value_map[:direction] + "_" + value_map[:base_name] + "_" + num.to_s + ".png"
		FileUtils.rm(to_delete_filename)
	end

	# remove current file
	FileUtils.rm(current_file)

	# And resize everything in the current directory to 32x32
	abort("Cannot mogrify") if not system("mogrify -geometry x32 *.png")
	
	# Let's go home, fetch the next file
	Dir.chdir(current_dir)
end