Created
April 3, 2013 02:35
-
-
Save squarism/5298005 to your computer and use it in GitHub Desktop.
Simple Ruby Processing Collision Detection
http://squarism.com/2011/04/16/simple-ruby-processing-collision-detection/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'java' | |
require 'observer' | |
require 'thread' | |
require 'pp' | |
include_class 'java.util.Random' | |
# port of deadgrid_refactor | |
class DeadGrid | |
attr_accessor :block_size, :grid, :width, :height | |
def initialize | |
@block_size = $app.block_size | |
@width = $app.width / $app.block_size - 1 | |
@height = $app.height / $app.block_size - 1 | |
@grid = Array.new | |
0.upto(@width) do |i| | |
grid[i] = Array.new | |
0.upto(@height) {|j| @grid[i][j] = 0 } | |
end | |
end | |
def to_s | |
string = "" | |
string << "\nDeadGrid\n" | |
string << "-" * @width << "\n" | |
0.upto(@width) do |i| | |
0.upto(@height) {|j| string << grid[j][i].to_s } | |
string << "\n" | |
end | |
string | |
end | |
def draw | |
$app.stroke(255,255,255,125) | |
$app.stroke_weight(1) | |
0.upto(@width) do |i| | |
0.upto(@height) do |j| | |
x = i * @block_size | |
y = j * @block_size | |
if (@grid[i][j] != 0) | |
if (@grid[i][j] == 1) | |
$app.fill(255,0,0) | |
elsif (@grid[i][j] == 2) | |
$app.fill(120,200,50) | |
end | |
$app.rect(x,y,@block_size,@block_size) | |
end | |
end | |
end | |
end | |
def randomize | |
rand = Random.new | |
0.upto(@width) do |i| | |
0.upto(@height) do |j| | |
random_int = rand.nextInt(2) | |
@grid[i][j] = random_int | |
end | |
end | |
end | |
def can_move?(player, distance) | |
case player.direction | |
when 0 #left | |
check_x = player.x - distance | |
check_y = player.y | |
if player.x != 0 | |
return !is_there?(check_x, check_y) | |
else | |
return false | |
end | |
when 2 #right | |
check_x = player.x + distance | |
check_y = player.y | |
if player.x < grid.size | |
return !is_there?(check_x, check_y) | |
else | |
return false | |
end | |
when 1 #up | |
check_x = player.x | |
check_y = player.y - distance | |
if player.y > 0 | |
return !is_there?(check_x, check_y) | |
else | |
return false | |
end | |
when 3 #down | |
check_x = player.x | |
check_y = player.y + distance | |
if player.y <= grid.size | |
return !is_there?(check_x, check_y) | |
else | |
return false | |
end | |
end | |
end | |
def is_there?(x,y) | |
begin | |
return grid[x][y] != 0 | |
rescue NoMethodError | |
return true | |
end | |
end | |
end | |
class Block | |
attr_accessor :block_size, :x, :y, :width, :height, :direction | |
def initialize(x,y) | |
@block_size = $app.block_size | |
@x = x | |
@y = y | |
@width = @block_size | |
@height = @block_size | |
@direction = 0 | |
end | |
def draw | |
$app.fill(55,0,255) | |
$app.rect(@x * @block_size, @y * @block_size, @width, @height) | |
end | |
end | |
# figures out which block to highlight on grid when player can't move | |
class BlockedHandler | |
def update(player, deadgrid) | |
case player.direction | |
when 0 | |
if deadgrid.grid[player.x - 1][player.y] == 1 | |
deadgrid.grid[player.x - 1][player.y] = 2; | |
end | |
when 1 | |
if deadgrid.grid[player.x][player.y - 1] == 1 | |
deadgrid.grid[player.x][player.y - 1] = 2; | |
end | |
when 2 | |
if deadgrid.grid[player.x + 1][player.y] == 1 | |
deadgrid.grid[player.x + 1][player.y] = 2; | |
end | |
when 3 | |
if deadgrid.grid[player.x][player.y + 1] == 1 | |
deadgrid.grid[player.x][player.y + 1] = 2; | |
end | |
end | |
end | |
end | |
class MySketch < Processing::App | |
include Observable | |
attr_accessor :deadgrid, :player, :block_size | |
def setup | |
@block_size = 16 | |
@deadgrid = DeadGrid.new | |
@player = Block.new 7,7 | |
puts @deadgrid | |
@blocked_state = Array.new | |
self.add_observer(BlockedHandler.new) | |
end | |
def draw | |
self.update | |
background 0 | |
self.draw_grid_lines | |
@deadgrid.draw | |
@player.draw | |
end | |
def update | |
@blocked_state.each {|s| s = false} | |
end | |
def draw_grid_lines | |
x,y = 0 | |
strokeWeight 1 | |
stroke(40) | |
# for each block in deadgrid, draw lines | |
0.upto(@deadgrid.width) do |i| | |
x = i * @block_size | |
# vertical line | |
line(x, 0, x, $app.height) | |
0.upto(@deadgrid.height) do |j| | |
y = j * @block_size | |
# horizontal line | |
line(0, y, $app.width, y) | |
end | |
end | |
end | |
# 38up 39right 40down 37left | |
def key_pressed | |
case key_code | |
when 32 # space | |
puts 'Randomize.' | |
@deadgrid.randomize | |
# if grid spawns under player, remove it | |
if (@deadgrid.grid[@player.x][@player.y] != 0) | |
@deadgrid.grid[@player.x][@player.y] = 0 | |
end | |
when 37 # left | |
@player.direction = 0 | |
# check bounds, old java code uses OutOfBoundsException trick | |
if player.x > 0 | |
if (@deadgrid.can_move?(@player, 1)) && @player.x > 0 | |
@player.x = @player.x - 1 | |
else | |
changed | |
notify_observers(@player, @deadgrid) | |
end | |
end | |
when 39 # right | |
@player.direction = 2 | |
# check bounds | |
if player.x < @deadgrid.width | |
if (@deadgrid.can_move?(@player, 1)) | |
@player.x = @player.x + 1 | |
else | |
changed | |
notify_observers(@player, @deadgrid) | |
end | |
end | |
when 38 # up | |
@player.direction = 1 | |
# check bounds | |
if player.y > 0 | |
if (@deadgrid.can_move?(@player, 1)) | |
@player.y = @player.y - 1 | |
else | |
changed | |
notify_observers(@player, @deadgrid) | |
end | |
end | |
when 40 # down | |
@player.direction = 3 | |
if player.y < @deadgrid.height | |
if (@deadgrid.can_move?(@player, 1)) | |
@player.y = @player.y + 1 | |
else | |
changed | |
notify_observers(@player, @deadgrid) | |
end | |
end | |
end | |
end | |
end | |
MySketch.new :title => "DeadGrid", :width => 256, :height => 256, :full_screen => false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment