add folders for dragonruby structure

master
Guido Schweizer 2022-06-02 15:00:48 +12:00
parent 64d70e1169
commit 57952937b2
9 changed files with 257 additions and 74 deletions

74
main.rb
View File

@ -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

148
sguis-conways/app/game.rb Normal file
View File

@ -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

81
sguis-conways/app/main.rb Normal file
View File

@ -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 <space> 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

View File

@ -0,0 +1 @@
Put level data and other txt files here.

View File

@ -0,0 +1 @@
Put your custom fonts here.

View File

@ -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

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.3 KiB

View File

@ -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=

View File

@ -0,0 +1 @@
Put your sounds here.