forked from ippa/chingu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
example25_fibers_state_machine.rb
167 lines (135 loc) · 3.49 KB
/
example25_fibers_state_machine.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env ruby
require 'rubygems'
require File.join(File.dirname($0), "..", "lib", "chingu")
include Gosu
include Chingu
Velocity = Struct.new(:x, :y, :z)
#
# Press 'E' when demo is running to edit the playfield!
#
class Game < Chingu::Window
def initialize
p RUBY_VERSION
super(1000,600)
end
def setup
retrofy
switch_game_state(Example25)
end
end
class Example25 < GameState
def setup
self.input = { :escape => :exit, :e => :edit }
load_game_objects
@droid = Droid.create(:x => 100, :y => 400)
end
def edit
push_game_state(GameStates::Edit.new(:grid => [32,32], :classes => [Block]))
end
def update
super
$window.caption = "x/y: #{@droid.x.to_i}/#{@droid.y.to_i} - FPS: #{$window.fps}"
end
def draw
super
#fill_rect(Rect.new(0,400,$window.width, 200), Color::White)
end
end
#
# DROID
#
class Droid < Chingu::GameObject
trait :bounding_box, :scale => 0.80
traits :timer, :collision_detection , :timer, :velocity
attr_reader :jumpign
def setup
self.input = { [:holding_left, :holding_a] => :holding_left,
[:holding_right, :holding_d] => :holding_right,
[:up, :w] => :jump,
}
# Load the full animation from tile-file media/droid.bmp
@animations = Chingu::Animation.new(:file => "droid_11x15.bmp")
@animations.frame_names = { :scan => 0..5, :up => 6..7, :down => 8..9, :left => 10..11, :right => 12..13 }
@animation = @animations[:scan]
@speed = 3
@jumping = false
self.zorder = 300
self.factor = 3
self.acceleration_y = 0.5
self.max_velocity = 10
self.rotation_center = :bottom_center
@fiber = Fiber.new do |command|
puts "got #{command}"; loop { v = Fiber.yield(); puts "got #{v}" }
end
@fiber.resume :one
@fiber.resume :two
#exit
#fiber = Fiber.new do |command|
#while true
# Fiber.yield
#end
#velocity ||= Velocity.new
#if command == :left
# move(-@speed, 0)
# @animation = @animations[:left]
#elsif command == :right
# move(@speed, 0)
# @animation = @animations[:right]
#end
#velocity
#end
update
end
def holding_left
@fiber.resume :left
end
def holding_right
@fiber.resume :right
end
def jump
return if @jumping
@jumping = true
self.velocity_y = -10
@animation = @animations[:up]
end
def move(x,y)
self.x += x
self.each_collision(Block) do |me, stone_wall|
self.x = previous_x
break
end
self.y += y
end
def update
@fiber.resume :jump# if holding?(:a)
@image = @animation.next
self.each_collision(Block) do |me, stone_wall|
if self.velocity_y < 0 # Hitting the ceiling
me.y = stone_wall.bb.bottom + me.image.height * self.factor_y
self.velocity_y = 0
else # Land on ground
@jumping = false
me.y = stone_wall.bb.top-1
end
end
@animation = @animations[:scan] unless moved?
end
end
#
# BLOCK, our basic level building block
#
class Block < GameObject
trait :bounding_box, :debug => false
trait :collision_detection
def self.solid
all.select { |block| block.alpha == 255 }
end
def self.inside_viewport
all.select { |block| block.game_state.viewport.inside?(block) }
end
def setup
@image = Image["black_block.png"]
@color = Color.new(0xff808080)
end
end
Game.new.show