117 lines
3.3 KiB
Ruby
117 lines
3.3 KiB
Ruby
App.run 720, 480, "Button Test", fps: 60
|
|
|
|
main_scene = Scene.new
|
|
|
|
default_font = Font.new "assets/fonts/charybdis.ttf", 24, pixel_art: true
|
|
|
|
bg_image1 = Image.load "assets/images/menu_bg.png", pixel_art: true
|
|
bg_image2 = Image.load "assets/images/game_over_dead_bg.png", pixel_art: true
|
|
|
|
tile_set = Image.load_set "assets/images/walls.png", 20, 20, padding: {x: 0, y: 20}, offset: {x: 0, y: 0}, pixel_art: true
|
|
# tile_set is an array of images, so we can create an animation from it directly or use the images separately
|
|
pp tile_set, "Tile set size: #{tile_set.size}"
|
|
tile_animation = Animation.new tile_set, fps: 10
|
|
|
|
bg_animation = Animation.new [bg_image1, bg_image2], fps: 5
|
|
|
|
bg_image1_a = Animation.from(bg_image1)
|
|
|
|
App.language = :en
|
|
App.localize(:en, :btn_text, "Click %{name}!")
|
|
|
|
text = ElementText.new :btn_text, default_font, 0xFFFF00, {name: "Me"}, position: {x: 50, y: 50}, z: 1, click_mode: :block, alpha: 1
|
|
|
|
rect = ElementRect.new 100, 25, 0xFF0000, position: {x: 50, y: 50}, z: 0, rotation: 60, scale: { x: 1, y: 1 }, alpha: 0.8, click_mode: :block
|
|
|
|
image_element = ElementImage.new tile_animation, position: {x: 0, y: 0}, scale: { x: 20, y: 20 }, z: -1, click_mode: :block, alpha: 1
|
|
|
|
image1 = Image.new 50, 50, pixel_art: true
|
|
|
|
image2_animation = Animation.from(image1)
|
|
|
|
image_element2 = ElementImage.new image2_animation, position: {x: 200, y: 200}, z: 0, click_mode: :block, alpha: 1
|
|
|
|
pp text, rect, image_element, image_element2
|
|
|
|
end_scene = Scene.new
|
|
end_scene << rect
|
|
|
|
text.click do
|
|
#App.switch_to end_scene
|
|
end
|
|
|
|
image_element.click do
|
|
pp "Image clicked"
|
|
end
|
|
|
|
rect.click do
|
|
pp "Rect clicked", rect.color.to_s(16)
|
|
|
|
rect.color = rect.color == 0xFF0000 ?
|
|
0x00FF00 :
|
|
0xFF0000
|
|
|
|
pp text.params[:name]
|
|
text.params[:name] = text.params[:name] == "Me" ?
|
|
"You" :
|
|
"Me"
|
|
end
|
|
|
|
App.map_key :mouse_left, :action_name
|
|
|
|
start = Time.now
|
|
c_ = 0.0
|
|
c_c = 0
|
|
|
|
App.update do |delta_time|
|
|
c_ += delta_time
|
|
c_c += 1
|
|
|
|
# puts "Delta time: #{delta_time}s"
|
|
|
|
red_pixel = [0xFF, 0, 0, 255] # ARGB
|
|
green_pixel = [0xFF, 0, 255, 0] # ARGB
|
|
c_p = ((c_c / 10) % 2) == 1 ? red_pixel : green_pixel
|
|
raw_bytes = (c_p * (50 * 50)).pack('C*')
|
|
# this is not the most efficient way to create a raw byte array, but it works for demonstration purposes
|
|
# you can use ` commands to get image data from an external source or generate it procedurally
|
|
bg_image1.update(0, 0, 50, 50, raw_bytes)
|
|
image1.update(0, 0, 50, 50, raw_bytes)
|
|
|
|
el = App.scene.elements_at(App.mouse_position)
|
|
|
|
pp App.text_input if App.text_input != ""
|
|
|
|
next unless App.pressed?(:action_name)
|
|
|
|
pp "#{App.pressed?(:action_name)}, #{App.down?(:action_name)}, #{App.released?(:action_name)}"
|
|
|
|
for elem in el
|
|
if elem == image_element
|
|
pp "Down over image element"
|
|
elsif elem == rect
|
|
pp "Down over rect element"
|
|
elsif elem == text
|
|
pp "Down over text element"
|
|
end
|
|
dy = rand(-10..10)
|
|
dx = rand(-10..10)
|
|
elem.position.y += dy
|
|
elem.position.x += dx
|
|
end
|
|
end
|
|
|
|
main_scene << text
|
|
main_scene << rect
|
|
main_scene << image_element
|
|
main_scene << image_element2
|
|
|
|
App.start main_scene
|
|
|
|
time = Time.now - start
|
|
|
|
p "Average delta time: #{c_ / c_c}s"
|
|
p "Average FPS (real): #{c_c / time}s"
|
|
p "Average FPS (from dt): #{c_c / c_}"
|
|
p "Time since start (real): #{time}s"
|
|
p "Time since start (from dt): #{c_}s" |