Mercurial > maze-src
comparison 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 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:1eef88068f9f |
|---|---|
| 1 require 'fileutils' | |
| 2 | |
| 3 imgfile = "download.png" | |
| 4 target_dir = "work" | |
| 5 | |
| 6 imgfile = ARGV[0] if ARGV.length >= 1 | |
| 7 target_dir = ARGV[1] if ARGV.length >= 2 | |
| 8 | |
| 9 puts "working on image: #{imgfile} going into #{target_dir}" | |
| 10 | |
| 11 # Does it exist? | |
| 12 abort("#{imgfile} does not exist. Giving up.") if not File.exist?(imgfile) | |
| 13 | |
| 14 # Cutting the file into long pieces. | |
| 15 abort("#{imgfile} could not be split up into lines") if not system("convert #{imgfile} -crop 832x64 +repage +adjoin row_%02d.png") | |
| 16 | |
| 17 # Relocating all pieces into their own directory. | |
| 18 # The following is the list: | |
| 19 # 0 - Spell cast up - scu | |
| 20 # 1 - Spell cast left - scl | |
| 21 # 2 - Spell cast down - scd | |
| 22 # 3 - Spell cast right - scr | |
| 23 # 4 - Thrust up - tru | |
| 24 # 5 - Thrust left - trl | |
| 25 # 6 - Thrust down - trd | |
| 26 # 7 - Thrust right - trr | |
| 27 # 8 - Walk up - wlu | |
| 28 # 9 - Walk left - wll | |
| 29 # 10 - Walk down - wld | |
| 30 # 11 - Walk right - wlr | |
| 31 # 12 - Slash up - slu | |
| 32 # 13 - Slash left - sll | |
| 33 # 14 - Slash down - sld | |
| 34 # 15 - Slash right - slr | |
| 35 # 16 - Shoot up - shu | |
| 36 # 17 - Shoot left - shl | |
| 37 # 18 - Shoot down - shd | |
| 38 # 19 - Shoot right - shr | |
| 39 # 20 - Hurt - hrt | |
| 40 | |
| 41 maps = { | |
| 42 "00" => {:name => "scu", :count => 7, :base_name => "sc", :direction => "u"}, | |
| 43 "01" => {:name => "scl", :count => 7, :base_name => "sc", :direction => "l"}, | |
| 44 "02" => {:name => "scd", :count => 7, :base_name => "sc", :direction => "d"}, | |
| 45 "03" => {:name => "scr", :count => 7, :base_name => "sc", :direction => "r"}, | |
| 46 "04" => {:name => "tru", :count => 8, :base_name => "tr", :direction => "u"}, | |
| 47 "05" => {:name => "trl", :count => 8, :base_name => "tr", :direction => "l"}, | |
| 48 "06" => {:name => "trd", :count => 8, :base_name => "tr", :direction => "d"}, | |
| 49 "07" => {:name => "trr", :count => 8, :base_name => "tr", :direction => "r"}, | |
| 50 "08" => {:name => "wlu", :count => 9, :base_name => "wl", :direction => "u"}, | |
| 51 "09" => {:name => "wll", :count => 9, :base_name => "wl", :direction => "l"}, | |
| 52 "10" => {:name => "wld", :count => 9, :base_name => "wl", :direction => "d"}, | |
| 53 "11" => {:name => "wlr", :count => 9, :base_name => "wl", :direction => "r"}, | |
| 54 "12" => {:name => "slu", :count => 6, :base_name => "sl", :direction => "u"}, | |
| 55 "13" => {:name => "sll", :count => 6, :base_name => "sl", :direction => "l"}, | |
| 56 "14" => {:name => "sld", :count => 6, :base_name => "sl", :direction => "d"}, | |
| 57 "15" => {:name => "slr", :count => 6, :base_name => "sl", :direction => "r"}, | |
| 58 "16" => {:name => "shu", :count =>13, :base_name => "sh", :direction => "u"}, | |
| 59 "17" => {:name => "shl", :count =>13, :base_name => "sh", :direction => "l"}, | |
| 60 "18" => {:name => "shd", :count =>13, :base_name => "sh", :direction => "d"}, | |
| 61 "19" => {:name => "shr", :count =>13, :base_name => "sh", :direction => "r"}, | |
| 62 "20" => {:name => "hrt", :count => 6, :base_name => "hr", :direction => "t"} | |
| 63 } | |
| 64 | |
| 65 | |
| 66 # Prepare the work directory | |
| 67 abort("Cannot create #{target_dir}") if not FileUtils::mkdir_p "#{target_dir}" | |
| 68 | |
| 69 maps.each do |key, value_map| | |
| 70 | |
| 71 # Create the directory where the file will go | |
| 72 current_target_dir = target_dir + "/" + value_map[:base_name] | |
| 73 abort("Cannot create #{current_target_dir} dir") if not FileUtils::mkdir_p "#{current_target_dir}" | |
| 74 | |
| 75 # The current file | |
| 76 current_file = "row_#{key}.png" | |
| 77 FileUtils.mv(current_file, "#{current_target_dir}") | |
| 78 | |
| 79 #Save current location | |
| 80 current_dir = Dir.pwd | |
| 81 | |
| 82 # Create the template of the outcoming files | |
| 83 target_filenames = value_map[:direction] + "_" + value_map[:base_name] + "_%01d.png" | |
| 84 # now go into the directory and chop up the images | |
| 85 Dir.chdir(current_target_dir) | |
| 86 abort("Cannot chop up #{current_file}") if not system("convert #{current_file} -crop 64x64 +repage +adjoin -scene 1 #{target_filenames}") | |
| 87 | |
| 88 # Now remove all files that are after count | |
| 89 (value_map[:count] + 1 .. 13).each do |num| | |
| 90 to_delete_filename = value_map[:direction] + "_" + value_map[:base_name] + "_" + num.to_s + ".png" | |
| 91 FileUtils.rm(to_delete_filename) | |
| 92 end | |
| 93 | |
| 94 # remove current file | |
| 95 FileUtils.rm(current_file) | |
| 96 | |
| 97 # And resize everything in the current directory to 32x32 | |
| 98 abort("Cannot mogrify") if not system("mogrify -geometry x32 *.png") | |
| 99 | |
| 100 # Let's go home, fetch the next file | |
| 101 Dir.chdir(current_dir) | |
| 102 end | |
| 103 | |
| 104 |
