Module: Rubygame::Sprites::MovingSprite

Defined in:
lib/xgame.rb

Overview

This is a mixin module for sprites that move

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



127
128
129
# File 'lib/xgame.rb', line 127

def self.included(base)
	base.class_eval {alias_method :update_no_moving, :update; alias_method :update, :update_moving}
end

Instance Method Details

#apply_force(value) ⇒ Object

Apply a force to this sprite [vx, vy, max_applied (optional)]



120
121
122
123
124
125
# File 'lib/xgame.rb', line 120

def apply_force(value)
	@velocity[:left] += value[0]*-1 if value[0] < 0 and (!value[2][:left] or @velocity[:left] < value[2][:left])
	@velocity[:right] += value[0] if value[0] > 0 and (!value[2][:right] or @velocity[:right] < value[2][:right])
	@velocity[:up] += value[1]*-1 if value[1] < 0 and (!value[2][:up] or @velocity[:up] < value[2][:up])
	@velocity[:down] += value[1] if value[1] > 0 and (!value[2][:down] or @velocity[:down] < value[2][:down])
end

#go(v, duration = 0) ⇒ Object

Go in some direction [vx, vy]. If either is nil, motion on that axis will not be affected.



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
# File 'lib/xgame.rb', line 80

def go(v, duration=0)
	if v[0]
		if v[0] <= 0
			@animating[:left] = [duration, @velocity[:left]]
			@velocity[:left] = v[0]*-1
			@going[:left] = true
		end
		if v[0] >= 0
			@animating[:right] = [duration, @velocity[:right]]
			@velocity[:right] = v[0]
			@going[:right] = true
		end
	end
	if v[1]
		if v[1] <= 0
			@animating[:up] = [duration, @velocity[:up]]
			@velocity[:up] = v[1]*-1
			@going[:up] = true
		end
		if v[1] >= 0
			@animating[:down] = [duration, @velocity[:down]]
			@velocity[:down] = v[1]
			@going[:down] = true
		end
	end
end

#goingObject



71
72
73
# File 'lib/xgame.rb', line 71

def going
	@going
end

#hit(v, by = nil) ⇒ Object

Strike the sprite in a certain direction



115
116
117
# File 'lib/xgame.rb', line 115

def hit(v, by=nil)
	self.go(v,[2,(1/(XGame.framerate/1000.0))/(@speed/4)].max)
end

#initialize(*args) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/xgame.rb', line 58

def initialize(*args)
	super
	@velocity = {:left => 0, :right => 0, :up => 0, :down => 0} # These are the initial components of velocity
	@going = {:left => false, :right => false, :up => false, :down => false} # Are we "going" in these directions or just travelling?
	@reference = [0,0] # Moving frame of reference
	@animating = {:left => [0,0], :right => [0,0], :up => [0,0], :down => [0,0]}
	@speed = 50 # This can be overidden by implementations: it is how fast / massive the sprite is
end

#reference=(v) ⇒ Object



75
76
77
# File 'lib/xgame.rb', line 75

def reference=(v)
	@reference = v
end

#stop(direction, duration = 0) ⇒ Object

Stop some component of motion



108
109
110
111
112
# File 'lib/xgame.rb', line 108

def stop(direction, duration=0)
	@animating[direction] = [duration, @animating[direction][0] >= 1 ? @animating[direction][1] : @velocity[direction]]
	@velocity[direction] = 0
	@going[direction] = false
end

#update_moving(time) ⇒ Object



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
# File 'lib/xgame.rb', line 131

def update_moving(time)
	update_no_moving(time)

	if @animating[:left][0] > 0
		@animating[:left][0] -= 1
		@velocity[:left] = @animating[:left][1] if @animating[:left][0] < 1
	end
	if @animating[:right][0] > 0
		@animating[:right][0] -= 1
		@velocity[:right] = @animating[:right][1] if @animating[:right][0] < 1
	end
	if @animating[:up][0] > 0
		@animating[:up][0] -= 1
		@velocity[:up] = @animating[:up][1] if @animating[:up][0]  < 1
	end
	if @animating[:down][0] > 0
		@animating[:down][0] -= 1
		@velocity[:down] = @animating[:down][1] if @animating[:down][0] < 1
	end
	x,y = @rect.center
	base = @speed * time/1000.0

	@rect.centerx = x + (@reference[0] + velocity[0]) * base
	@rect.centery = y + (@reference[1] + velocity[1]) * base
	@reference = [0,0] # Frame of reference must be reset every frame
end

#velocityObject



67
68
69
# File 'lib/xgame.rb', line 67

def velocity
	[@velocity[:left]*-1 + @velocity[:right], @velocity[:up]*-1 + @velocity[:down]]
end