Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit b525966760675e9a62d35e09f89ce2e89ca4e768 @peterc committed Aug 23, 2011
Showing with 480 additions and 0 deletions.
  1. +17 −0 README.md
  2. +4 −0 escape.rb
  3. +40 −0 escape/art.rb
  4. BIN escape/assets/gui/logo.png
  5. BIN escape/assets/level/crypt.png
  6. BIN escape/assets/level/dungeon.png
  7. BIN escape/assets/level/ice.png
  8. BIN escape/assets/level/overworld.png
  9. BIN escape/assets/level/start.png
  10. BIN escape/assets/level/temple.png
  11. BIN escape/assets/snd/altar.wav
  12. BIN escape/assets/snd/bosskill.wav
  13. BIN escape/assets/snd/click.wav
  14. BIN escape/assets/snd/click2.wav
  15. BIN escape/assets/snd/crumble.wav
  16. BIN escape/assets/snd/cut.wav
  17. BIN escape/assets/snd/death.wav
  18. BIN escape/assets/snd/hit.wav
  19. BIN escape/assets/snd/hurt.wav
  20. BIN escape/assets/snd/hurt2.wav
  21. BIN escape/assets/snd/key.wav
  22. BIN escape/assets/snd/kill.wav
  23. BIN escape/assets/snd/ladder.wav
  24. BIN escape/assets/snd/pickup.wav
  25. BIN escape/assets/snd/potion.wav
  26. BIN escape/assets/snd/roll.wav
  27. BIN escape/assets/snd/shoot.wav
  28. BIN escape/assets/snd/slide.wav
  29. BIN escape/assets/snd/splash.wav
  30. BIN escape/assets/snd/thud.wav
  31. BIN escape/assets/snd/treasure.wav
  32. BIN escape/assets/tex/floors.png
  33. BIN escape/assets/tex/font.png
  34. BIN escape/assets/tex/gamepanel.png
  35. BIN escape/assets/tex/items.png
  36. BIN escape/assets/tex/sky.png
  37. BIN escape/assets/tex/sprites.png
  38. BIN escape/assets/tex/walls.png
  39. +147 −0 escape/component.rb
  40. +15 −0 escape/escape.rb
  41. +65 −0 escape/game.rb
  42. +53 −0 escape/gui/bitmap.rb
  43. +38 −0 escape/gui/screen.rb
  44. +43 −0 escape/input_handler.rb
  45. +4 −0 escape/menu/menu.rb
  46. +54 −0 escape/menu/title_menu.rb
@@ -0,0 +1,17 @@
+# Prelude of the Chambered (JRuby port)
+
+This is an in-progress JRuby port of notch's 48 hour "Ludum Dare" game competition entry, _Prelude of the Chambered_.
+
+Notch's Java code is pretty straightforward and I've been looking for an excuse to do a "real" project in JRuby for ages. This seemed an ideal opportunity.
+
+Porting is still in progress and I'm only as far as the main menu so far. But a lot of "support" code has already been ported so progress is going well.
+
+## Running
+
+To run this code, get JRuby installed (rvm install jruby?) and then just run (from the root folder):
+
+ ruby escape.rb
+
+## License
+
+Unsure as yet, not sure of the license for the original game.
@@ -0,0 +1,4 @@
+$: << File.expand_path(File.dirname(__FILE__))
+require 'escape/escape'
+
+Component.main
@@ -0,0 +1,40 @@
+java_import java.awt.image.BufferedImage
+java_import javax.imageio.ImageIO
+
+class Art < java.lang.Object
+ def self.load_bitmap(file_name)
+ url = java.net.URL.new("file://" + ASSETS_DIR + file_name) # nasty little hack due to borked get_resource (means applet won't be easy..)
+ img = ImageIO.read(url)
+
+ w = img.width
+ h = img.height
+
+ result = Bitmap.new(w, h)
+ result.pixels = img.getRGB(0, 0, w, h, nil, 0, w)
+
+ result.pixels.length.times do |i|
+ inp = result.pixels[i]
+
+ col = (inp & 0xf)
+ col = -1 if inp == 0xffff00ff
+ result.pixels[i] = col
+ end
+
+ result
+ end
+
+ def self.get_col(c)
+ r = (c >> 16) & 0xff
+ g = (c >> 8) & 0xff
+ b = (c) & 0xff
+
+ r = r * 0x55 / 0xff
+ g = g * 0x55 / 0xff
+ b = b * 0x55 / 0xff
+
+ r << 16 | g << 8 | b
+ end
+
+ FONT = load_bitmap("/tex/font.png")
+ LOGO = load_bitmap("/gui/logo.png")
+end
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,147 @@
+java_import java.awt.Canvas
+java_import java.awt.BorderLayout
+java_import java.awt.Dimension
+java_import javax.swing.JFrame
+java_import javax.swing.JPanel
+java_import java.awt.Toolkit
+java_import java.awt.image.BufferedImage
+java_import java.awt.image.BufferStrategy
+java_import java.awt.Point
+java_import java.lang.System
+
+class Component < Canvas
+ include java.lang.Runnable
+
+ SERIAL_VERSION_UID = 1
+ WIDTH = 160
+ HEIGHT = 120
+ SCALE = 4
+
+ attr_accessor :running
+
+ def initialize
+ super
+
+ size = Dimension.new(WIDTH * SCALE, HEIGHT * SCALE)
+ self.minimum_size = self.maximum_size = self.preferred_size = self.size = size
+
+ @game = Game.new
+ @screen = Screen.new(WIDTH, HEIGHT)
+ @img = BufferedImage.new(WIDTH, HEIGHT, BufferedImage::TYPE_INT_RGB)
+ @pixels = @img.raster.data_buffer.data
+ @input_handler = InputHandler.new
+ add_key_listener @input_handler
+ add_focus_listener @input_handler
+ add_mouse_listener @input_handler
+ add_mouse_motion_listener @input_handler
+ @empty_cursor = Toolkit.default_toolkit.create_custom_cursor(BufferedImage.new(16, 16, BufferedImage::TYPE_INT_ARGB), Point.new(0, 0), "empty")
+ @default_cursor = cursor
+ @running = false
+ end
+
+ def start
+ return if @running
+ @running = true
+ @thread = java.lang.Thread.new(self)
+ @thread.start
+ end
+
+ def stop
+ return unless @running
+ @running = false
+ begin
+ @thread.join
+ rescue => e
+ p e
+ end
+ end
+
+ def run
+ frames = 0
+ unprocessed_seconds = 0
+ last_time = System.nano_time
+ seconds_per_tick = 1 / 61.0
+ tick_count = 0
+
+ request_focus
+
+ while running
+ now = System.nano_time
+ passed_time = now - last_time
+ last_time = now
+ passed_time = 0 if passed_time < 0
+ passed_time = 100000000 if passed_time > 100000000
+
+ unprocessed_seconds += passed_time / 1000000000.0
+ ticked = false
+
+ while unprocessed_seconds > seconds_per_tick
+ tick
+ unprocessed_seconds -= seconds_per_tick
+ ticked = true
+
+ tick_count += 1
+ if tick_count % 60 == 0
+ System.out.println frames.to_s + " fps"
+ last_time += 1000
+ frames = 0
+ end
+ end
+
+ if ticked
+ render
+ frames += 1
+ else
+ begin
+ java.lang.Thread.sleep 1
+ rescue => e
+ p e
+ end
+ end
+ end
+ end
+
+ def tick
+ @game.tick(@input_handler.keys) if has_focus
+ end
+
+ def render
+ if has_focus != has_focus
+ has_focus = !had_focus
+ set_cursor had_focus ? @empty_cursor : @default_cursor
+ end
+
+ bs = get_buffer_strategy
+ unless bs
+ create_buffer_strategy 3
+ return
+ end
+
+ @screen.render(@game, has_focus)
+
+ 0.upto(WIDTH * HEIGHT - 1) do |i|
+ @pixels[i] = @screen.pixels[i]
+ end
+
+ g = bs.draw_graphics
+ g.fill_rect 0, 0, get_width, get_height
+ g.draw_image @img, 0, 0, WIDTH * SCALE, HEIGHT * SCALE, nil
+ g.dispose
+ bs.show
+ end
+
+ def self.main
+ game = new
+ frame = JFrame.new("Prelude of the Chambered!")
+ panel = JPanel.new(BorderLayout.new)
+ panel.add(game, BorderLayout::CENTER)
+ frame.content_pane = panel
+ frame.pack
+ frame.resizable = false
+ frame.location_relative_to = nil
+ frame.default_close_operation = JFrame::EXIT_ON_CLOSE
+ frame.visible = true
+ game.start
+ end
+end
+
@@ -0,0 +1,15 @@
+$: << File.expand_path(File.dirname(__FILE__))
+
+ASSETS_DIR = File.expand_path(File.dirname(__FILE__) + "/assets")
+
+require 'java'
+require 'input_handler'
+require 'component'
+require 'game'
+
+require 'gui/bitmap'
+require 'gui/screen'
+
+require 'menu/menu'
+require 'menu/title_menu'
+require 'art'
@@ -0,0 +1,65 @@
+java_import java.awt.event.KeyEvent
+
+class Game
+ attr_accessor :menu, :player, :time, :level
+
+ def initialize
+ @pause_time = 0
+ @time = 0
+ @player = nil
+ @menu = TitleMenu.new
+ end
+
+ def tick(keys)
+ if @pause_time > 0
+ @pauseTime -= 1
+ return
+ end
+
+ @time += 1
+
+ strafe = keys[KeyEvent::VK_CONTROL] || keys[KeyEvent::VK_ALT] || keys[KeyEvent::VK_ALT_GRAPH] || keys[KeyEvent::VK_SHIFT]
+
+ lk = keys[KeyEvent::VK_LEFT] || keys[KeyEvent::VK_NUMPAD4]
+ rk = keys[KeyEvent::VK_RIGHT] || keys[KeyEvent::VK_NUMPAD6]
+
+ up = keys[KeyEvent::VK_W] || keys[KeyEvent::VK_UP] || keys[KeyEvent::VK_NUMPAD8]
+ down = keys[KeyEvent::VK_S] || keys[KeyEvent::VK_DOWN] || keys[KeyEvent::VK_NUMPAD2]
+ left = keys[KeyEvent::VK_A] || (strafe && lk)
+ right = keys[KeyEvent::VK_D] || (strafe && rk)
+
+ turn_left = keys[KeyEvent::VK_Q] || (!strafe && lk)
+ turn_right = keys[KeyEvent::VK_E] || (!strafe && rk)
+
+ use = keys[KeyEvent::VK_SPACE]
+
+ 8.times do |i|
+ next unless keys[KeyEvent::VK_1 + i]
+ keys[KeyEvent::VK_1 + i] = false
+ @player.selected_slot = i
+ @player.item_use_time = 0
+ end
+
+ if keys[KeyEvent::VK_ESCAPE]
+ keys[KeyEvent::VK_ESCAPE] = false
+ set_menu PauseMenu.new unless menu
+ end
+
+ keys[KeyEvent::VK_SPACE] = false if use
+
+ if menu
+ keys[KeyEvent::VK_W] = keys[KeyEvent::VK_UP] = keys[KeyEvent::VK_NUMPAD8] = false
+ keys[KeyEvent::VK_S] = keys[KeyEvent::VK_DOWN] = keys[KeyEvent::VK_NUMPAD2] = false
+ keys[KeyEvent::VK_A] = false
+ keys[KeyEvent::VK_D] = false
+
+ menu.tick self, up, down, left, right, use
+ else
+ player.tick up, down, left, right, turn_left, turn_right
+ player.activate if use
+
+ level.tick
+ end
+ end
+
+end
@@ -0,0 +1,53 @@
+class Bitmap
+ attr_accessor :pixels
+ attr_reader :height, :width
+
+ CHARS = "" +
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ.,!?\"'/\\<>()[]{}" +
+ "abcdefghijklmnopqrstuvwxyz_ " +
+ "0123456789+-=*:;ÖÅÄå " +
+ "";
+
+ def initialize(width, height)
+ @width = width
+ @height = height
+ @pixels = Array.new(width * height, 0)
+ end
+
+ def fill(x0, y0, x1, y1, color)
+ y0.upto(y1 - 1) do |y|
+ x0.upto(x1 - 1) do |x|
+ @pixels[x + y * @width] = color
+ end
+ end
+ end
+
+ def draw_string(string, x, y, col)
+ string.chars.each_with_index do |char, i|
+ ch = CHARS.index(char)
+ next if ch < 0
+
+ xx = ch % 42
+ yy = ch / 42
+ draw_bitmap(Art::FONT, x + i * 6, y, xx * 6, yy * 8, 5, 8, col)
+ end
+ end
+
+ def draw_bitmap(bitmap, x_offs, y_offs, xo, yo, w, h, col)
+ h.times do |y|
+ y_pix = y + y_offs
+ next if (y_pix < 0 || y_pix >= @height)
+
+ w.times do |x|
+ x_pix = x + x_offs
+ next if (x_pix < 0 || x_pix >= @width)
+
+ src = bitmap.pixels[(x + xo) + (y + yo) * bitmap.width]
+
+ if src >= 0
+ @pixels[x_pix + y_pix * width] = src * col
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,38 @@
+class Screen < Bitmap
+ PANEL_HEIGHT = 29
+
+ def initialize(width, height)
+ super(width, height)
+ #@viewport = Bitmap3D.new(width, height - PANEL_HEIGHT)
+ end
+
+ def render(game, has_focus)
+ if game.level
+ # TODO
+ else
+ fill 0, 0, @width, @height, 0
+ end
+
+ if game.menu
+ @pixels.length.times do |i|
+ @pixels[i] = (@pixels[i] & 0xfcfcfc) >> 2
+ end
+ game.menu.render self
+ end
+
+ unless has_focus
+ @pixels.length.times do |i|
+ @pixels[i] = (@pixels[i] & 0xfcfcfc) >> 2
+ end
+
+ if java.lang.System.current_time_millis / 450 % 2 != 0
+ msg = "Click to focus!"
+ draw_string msg, (@width - msg.length() * 6) / 2, @height / 3 + 4, 0xffffff
+ end
+ end
+
+ #0.upto(@width * @height - 1) do |i|
+ # @pixels[i] = rand(2).zero? ? 0x00ff0000 : 0x0000ff00
+ #end
+ end
+end
Oops, something went wrong.

0 comments on commit b525966

Please sign in to comment.