Created
August 26, 2008 06:07
-
-
Save atnan/7222 to your computer and use it in GitHub Desktop.
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 'rubygems' rescue nil | |
require 'gosu' | |
require 'chipmunk' | |
class Array | |
# e.g. [1,2,3].each_link yields [1,2], [2,3] | |
def each_link | |
prev = first | |
self[1, size].each do |item| | |
yield prev, item | |
prev = item | |
end | |
end | |
end | |
# A grappling hook | |
module Grapple | |
SCREEN_WIDTH = 800 | |
SCREEN_HEIGHT = 600 | |
INF = 1e100 | |
# The number of steps to process every Gosu update The Player ship can get | |
# going so fast as to "move through" a star without triggering a collision; | |
# an increased number of Chipmunk step calls per update will effectively | |
# avoid this issue | |
SUBSTEPS = 6 | |
class Rope | |
include CP | |
attr_reader :links | |
def initialize(window, space, options) | |
options ||= {} | |
@length = options[:length] || 100 | |
@window = window | |
@links = [] | |
@length.times do |n| | |
link = Body.new(1, 0.1) | |
seg = Shape::Circle.new(link, 0.1, Vec2.new(0.0, 0.0)) | |
seg.collision_type = :rope | |
seg.u = 0.5 | |
link.p = Vec2.new(n*5, 0) | |
space.add_body(link) | |
space.add_shape(seg) | |
@links << link | |
end | |
@links.each_link do |prev, link| | |
joint = Joint::Slide.new(prev, link, Vec2.new(0.0, 0.0), Vec2.new(1.0, 0.0), 0.1, 4.0) | |
space.add_joint(joint) | |
end | |
end | |
def draw | |
draw_gradient | |
end | |
def draw_segments | |
col1 = Gosu::Color.new(0xffff0000) | |
col2 = Gosu::Color.new(0xffffff00) | |
@links.each_link do |prev, link| | |
@window.draw_line(prev.p.x, prev.p.y, col1, link.p.x, link.p.y, col2) | |
end | |
end | |
def draw_gradient | |
col = Gosu::Color.new(0xffff0000) | |
i = 0 | |
@links.each_link do |prev, link| | |
col.green = (255*i / @links.size) | |
@window.draw_line(prev.p.x, prev.p.y, col, link.p.x, link.p.y, col) | |
i += 1 | |
end | |
end | |
end | |
class Ground | |
include CP | |
attr_reader :body | |
def initialize(window, space) | |
@window = window | |
@links = [] | |
@body = Body.new(INF, INF) | |
@p1, @p2 = Vec2.new(0, 500), Vec2.new(SCREEN_WIDTH, 440) | |
@width = 20 | |
@seg = Shape::Segment.new(@body, @p1, @p2, @width) | |
@seg.collision_type = :ground | |
@seg.u = 0.99 | |
space.add_static_shape(@seg) | |
end | |
def draw | |
col = Gosu::Color.new(0xff0000ff) | |
@window.draw_line(@p1.x, @p1.y - @width, col, @p2.x, @p2.y - @width, col) | |
end | |
end | |
class GameWindow < Gosu::Window | |
include CP | |
def initialize | |
super(SCREEN_WIDTH, SCREEN_HEIGHT, false, 16) | |
self.caption = "Grapple" | |
@space = Space.new | |
@space.damping = 0.8 | |
@space.gravity = Vec2.new(0, 10.0) | |
@dt = (1.0/60.0) | |
@ground = Ground.new(self, @space) | |
@rope = Rope.new(self, @space, :length => 15) | |
@rope.links.each_with_index{|link, i| link.p = Vec2.new(300 + 100*Math.sin(i.to_f * Math::PI*2 / @rope.links.size), 200) } | |
@hook = Body.new(100, 10) | |
joint = Joint::Pin.new(@hook, @rope.links.last, Vec2.new(0.0, 0.0), Vec2.new(0.0, 0.0)) | |
@space.add_body(@hook) | |
@space.add_joint(joint) | |
@hook.p = Vec2.new(350, 200) | |
attach = Joint::Slide.new(@ground.body, @rope.links.first, Vec2.new(300.0, 400.0), Vec2.new(0.0, 0.0), 0, 0) | |
@space.add_joint(attach) | |
@rope2 = Rope.new(self, @space, :length => 10) | |
hanging = Body.new(INF, INF) | |
joint = Joint::Pin.new(hanging, @rope2.links.first, Vec2.new(0.0, 0.0), Vec2.new(0.0, 0.0)) | |
@space.add_joint(joint) | |
@rope2.links.each_with_index{|link, i| link.p = Vec2.new(100 - 10*i, 200) } | |
hanging.p = Vec2.new(150, 0) | |
end | |
def update | |
SUBSTEPS.times do | |
# Check keyboard | |
if button_down? Gosu::Button::KbSpace | |
@hook.v = Vec2.new(10, -200) | |
end | |
#@rope.links.each{|link| link.reset_forces } | |
@space.step(@dt) | |
end | |
end | |
def draw | |
@rope.draw | |
@rope2.draw | |
@ground.draw | |
end | |
def button_down(id) | |
case id | |
when Gosu::Button::KbEscape, char_to_button_id('q') | |
close | |
end | |
end | |
end | |
end | |
if $0 == __FILE__ | |
window = Grapple::GameWindow.new | |
window.show | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment