From 57952937b231256c1e493c064043984046d8a157 Mon Sep 17 00:00:00 2001 From: Guido Schweizer Date: Thu, 2 Jun 2022 15:00:48 +1200 Subject: [PATCH] add folders for dragonruby structure --- main.rb | 74 ------------ sguis-conways/app/game.rb | 148 +++++++++++++++++++++++ sguis-conways/app/main.rb | 81 +++++++++++++ sguis-conways/data/.gitkeep | 1 + sguis-conways/fonts/.gitkeep | 1 + sguis-conways/metadata/game_metadata.txt | 12 ++ sguis-conways/metadata/icon.png | Bin 0 -> 7496 bytes sguis-conways/metadata/ios_metadata.txt | 13 ++ sguis-conways/sounds/.gitkeep | 1 + 9 files changed, 257 insertions(+), 74 deletions(-) delete mode 100644 main.rb create mode 100644 sguis-conways/app/game.rb create mode 100644 sguis-conways/app/main.rb create mode 100644 sguis-conways/data/.gitkeep create mode 100644 sguis-conways/fonts/.gitkeep create mode 100644 sguis-conways/metadata/game_metadata.txt create mode 100644 sguis-conways/metadata/icon.png create mode 100644 sguis-conways/metadata/ios_metadata.txt create mode 100644 sguis-conways/sounds/.gitkeep diff --git a/main.rb b/main.rb deleted file mode 100644 index 3eee502..0000000 --- a/main.rb +++ /dev/null @@ -1,74 +0,0 @@ -require 'app/game.rb' - -MAX_X = 40 -MAX_Y = 24 - -BLOCK_SIZE = (1280/MAX_X).round - -INPUT = [ - [9,9], - [10,10], - [9,11], - [11,11], - [9,12], - [11,12], - [13,13], - [14,13], - [15,13], - [16,13], - [16,15], - [16,16], - [17,17], -] - -SPEED = 0.05 # every second -TICK_FOR_STEP = (SPEED * 60).round - - -def tick args - args.outputs.labels << [640, 500, 'Conways game of life', 5, 1] - # args.outputs.labels << [640, 460, 'Go to docs/docs.html and read it!', 5, 1] - # args.outputs.labels << [640, 420, 'Join the Discord! https://discord.dragonruby.org', 5, 1] - # args.outputs.sprites << [576, 280, 128, 101, 'dragonruby.png'] - game = args.state.game - if game.nil? - game = Game.new(INPUT) - args.state.game = game - end - - args.outputs.solids << [ - 0, - 0, - MAX_X*BLOCK_SIZE, - MAX_Y*BLOCK_SIZE, - 164, - 212, - 220, - 128] - - if args.state.tick_count % TICK_FOR_STEP == 0 - game.tick - end - - live_cells = game.live_cells - live_cells.each do |x,y| - args.outputs.solids << [ - x*BLOCK_SIZE, - y*BLOCK_SIZE, - BLOCK_SIZE, - BLOCK_SIZE, - 219, - 163, - 211, - 255] - end - - args.outputs.labels << [640, 600, "Round ##{game.tick_count}", 5, 1] - args.outputs.labels << [640, 700, "Tick count ##{args.state.tick_count}", 5, 1] - args.outputs.labels << [10, 710, "framerate: #{args.gtk.current_framerate.round}"] - - - # render - # X Y WIDTH HEIGHT RED GREEN BLUE ALPHA - -end \ No newline at end of file diff --git a/sguis-conways/app/game.rb b/sguis-conways/app/game.rb new file mode 100644 index 0000000..e8c922c --- /dev/null +++ b/sguis-conways/app/game.rb @@ -0,0 +1,148 @@ +class Game + attr_reader :live_cells, :tick_count + + def initialize(live_cells) + @tick_count = 0 + @live_cells = live_cells # Arr[ [x,y] ] + end + + def tick(args=nil) + calculate_next_gen + render(args) + @tick_count += 1 + end + + def trigger_cell(x,y) + cell = [x,y] + if live_cells.include? cell + live_cells.delete(cell) + else + live_cells << cell + end + end + + private + + attr_writer :live_cells, :tick_count + + def calculate_next_gen + next_gen_living_cells = [] + live_cell_map = convert_cells_to_map + + cells_to_check = calculate_cells_to_check + + cells_to_check.each do |x,y| + is_next_gen_living = check_cell(x,y,live_cell_map) + next_gen_living_cells << [x,y] if is_next_gen_living + end + @live_cells = next_gen_living_cells + end + + def check_cell(x,y,live_cell_map) + adjacent_living_cell_count = adjacent_living_cell_count(x,y,live_cell_map) + + return true if adjacent_living_cell_count == 3 + return true if adjacent_living_cell_count == 2 && is_living_cell?(x,y,live_cell_map) + + return false + end + + def is_living_cell?(x,y,live_cell_map) + live_cell_map.dig(x)&.dig(y) == true + end + + def convert_cells_to_map + map = {} + live_cells.each do |x,y| + map[x] ||= {} + map[x][y] = true + end + + map + end + + def calculate_cells_to_check + cells_to_check = live_cells.dup + + live_cells.each do |x,y| + cells_to_check += adjacent_cell_coordinates(x,y) + end + + cells_to_check.uniq + end + + def adjacent_cell_coordinates(x,y) + adjacent_cells = [] + + -1.upto(1).each do |i| + -1.upto(1).each do |j| + adjacent_cells << [x+i,y+j] + end + end + adjacent_cells -= [[x,y]] + + adjacent_cells + end + + def adjacent_living_cell_count(x,y,live_cell_map) + map = live_cell_map + count = 0 + -1.upto(1).each do |i| + -1.upto(1).each do |j| + next if i==0 && j==0 + + count += 1 if map.dig(x+i)&.dig(y+j) + end + end + + count + end + + def render(args) + # print_console + end + + def print_console + puts "--- Round #{tick_count} ---" + + max_x = live_cells.map{|x,y| x}.max + 5 + max_y = live_cells.map{|x,y| y}.max + 5 + + max_y.times do |y| + max_x.times do |x| + if live_cells.include? [x,y] + print "*" + else + print "." + end + end + print "\n" + end + end +end + +# console test + +# INPUT = [ +# [9,9], +# [10,10], +# [9,11], +# [11,11], +# [9,12], +# [11,12], +# [13,13], +# [14,13], +# [15,13], +# [16,13], +# [16,15], +# [16,16], +# [17,17], +# ] + +# game = Game.new(INPUT) + +# 100.times do |i| +# game.tick + +# sleep 0.2 +# end \ No newline at end of file diff --git a/sguis-conways/app/main.rb b/sguis-conways/app/main.rb new file mode 100644 index 0000000..a555b53 --- /dev/null +++ b/sguis-conways/app/main.rb @@ -0,0 +1,81 @@ +require 'app/game.rb' + +MAX_X = 64 +MAX_Y = 39 + +BLOCK_SIZE = (1280/MAX_X).round + +INPUT = [ + [31,23], + [31,24], + [32,23], + [31,25], + [33,24] +] + +SPEED = 0.05 # every second +TICK_FOR_STEP = (SPEED * 60).round + + +def tick args + # INIT + args.state.paused = true if args.state.paused.nil? + + game = args.state.game + if game.nil? + game = Game.new(INPUT) + args.state.game = game + end + + # HANDLE Inputs + if args.inputs.keyboard.key_up.space + args.state.paused = !args.state.paused + end + + # GAME LOOP + if !args.state.paused + # continue ticks if unpaused + if args.state.tick_count % TICK_FOR_STEP == 0 + game.tick + end + else + #paused + args.outputs.labels << [640, 40, "Game is paused. Press to continue", 4, 1] + + # allows adding new cells + if args.inputs.mouse.click + x = (args.inputs.mouse.click.point.x / BLOCK_SIZE).floor + y = (args.inputs.mouse.click.point.y / BLOCK_SIZE).floor + game.trigger_cell(x,y) + end + end + + # RENDER + args.outputs.labels << [1160, 710, "Round ##{game.tick_count}"] + #args.outputs.labels << [10, 710, "framerate: #{args.gtk.current_framerate.round}"] + args.outputs.labels << [640, 710, 'Conways game of life', 5, 1] + + # draw BG + args.outputs.solids << [ + 0, + 0, + MAX_X*BLOCK_SIZE, + MAX_Y*BLOCK_SIZE, + 164, + 212, + 220, + 128] + + # draw the cells + live_cells = game.live_cells + args.outputs.solids << live_cells.map do |x,y| + [x*BLOCK_SIZE, + y*BLOCK_SIZE, + BLOCK_SIZE, + BLOCK_SIZE, + 219, + 163, + 211, + 255] + end +end \ No newline at end of file diff --git a/sguis-conways/data/.gitkeep b/sguis-conways/data/.gitkeep new file mode 100644 index 0000000..c1ffee3 --- /dev/null +++ b/sguis-conways/data/.gitkeep @@ -0,0 +1 @@ +Put level data and other txt files here. \ No newline at end of file diff --git a/sguis-conways/fonts/.gitkeep b/sguis-conways/fonts/.gitkeep new file mode 100644 index 0000000..a03e35e --- /dev/null +++ b/sguis-conways/fonts/.gitkeep @@ -0,0 +1 @@ +Put your custom fonts here. \ No newline at end of file diff --git a/sguis-conways/metadata/game_metadata.txt b/sguis-conways/metadata/game_metadata.txt new file mode 100644 index 0000000..f00f06e --- /dev/null +++ b/sguis-conways/metadata/game_metadata.txt @@ -0,0 +1,12 @@ +devid=Squishibutz +devtitle=Squishibutz +gameid=sguis-conways +gametitle=Sgui's conways +version=0.1 +icon=metadata/icon.png + +# Uncomment the entry below to bytecode compile your Ruby code (Pro License Only) +#compile_ruby=false + +# Uncomment the entry below to specify the package name for your APK (Pro License Only) +#packageid=org.dev.gamename diff --git a/sguis-conways/metadata/icon.png b/sguis-conways/metadata/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..58eb3a16cf8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U3;ZsrE^?<unBy1t7h(T1Ljp|3b_5Qg z5an-9xMYb6j$TF2uYMVNA#w<{(jCqaiJ)s1_%=bA^?SANi+Mmc1$BoKqt|j@u21fH zISmgJsj+(ubde0vaP{}Ik(WNXN8Z*8YEFkCVeH0>T;2!Vv6K#M)p zgq5M3hnrj!MbPMuVxjYQ=j+RvE*nN%6hn0i=^mk;$W6+0Up^be!$ks?n*|`R!PW=W zYgZWcWi$_iM%lVfhk|$#&2-c$ba*!B2nI1L+I<2H{Yw2`Wk8OHGjsk0k>C*9@nNSn zeEqMnsSW=cLI#6Jz0use;k!TTFhr%w?&$~F&c5TeWZY6u=H`RI;1J??rrKs+ z#EP2acRC_JNoc07(|2gpdX%kdhCV+vVxGPC80IhuNI-T8vHS}CH*D+)005PGRj6wQ z0GJgIey|n?t9@>Ja_M{;*$6zU!Kd=LftovU#zNchcGAgs-^=4*an@7PE2-0+_C8#6 z$;%6BBe1a5d0Zn(goH_NT7MZk{s85ZJRh_P-&QWzezQEBm()nCS6&)MtF1d?Ca9y^ z&_425ECxK%5%OJKj??#}?~sf&g~{%Rx*X5{y$zz4`6_jLjzHbmt_xAv*<5uR3ReoJ z%kB3=csjQ}2|+u-T%x%Ucd5~)AR7^0W${lW)iXb(Xj=N;JS zj_}=Kq5uiBbR90HL6JqDH9kxN@&DTVzvaM`ia3he4-P_8cU+qu^sjL*VVp#kM=+wB zm6aRP*fh`MkWliq)twmhAWFoxRx~MLXE}HLWuy2CK_w|Rw_|)2zdDJcII7Y9W1>I+ u*Nhk>G_2D*QT>+PwSNJjGk8V- literal 0 HcmV?d00001 diff --git a/sguis-conways/metadata/ios_metadata.txt b/sguis-conways/metadata/ios_metadata.txt new file mode 100644 index 0000000..0ba387e --- /dev/null +++ b/sguis-conways/metadata/ios_metadata.txt @@ -0,0 +1,13 @@ +# ios_metadata.txt is used by the Pro version of DragonRuby Game Toolkit to create iOS apps. +# Information about the Pro version can be found at: http://dragonruby.org/toolkit/game#purchase + +# teamid needs to be set to your assigned Team Id which can be found at https://developer.apple.com/account/#/membership/ +teamid= +# appid needs to be set to your application identifier which can be found at https://developer.apple.com/account/resources/identifiers/list +appid= +# appname is the name you want to show up underneath the app icon on the device. Keep it under 10 characters. +appname= +# devcert is the certificate to use for development/deploying to your local device. This is the NAME of the certificate as it's displayed in Keychain Access. +devcert= +# prodcert is the certificate to use for distribution to the app store. This is the NAME of the certificate as it's displayed in Keychain Access. +prodcert= diff --git a/sguis-conways/sounds/.gitkeep b/sguis-conways/sounds/.gitkeep new file mode 100644 index 0000000..a99ec00 --- /dev/null +++ b/sguis-conways/sounds/.gitkeep @@ -0,0 +1 @@ +Put your sounds here. \ No newline at end of file