-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcharacter.rb
More file actions
164 lines (131 loc) · 3.6 KB
/
character.rb
File metadata and controls
164 lines (131 loc) · 3.6 KB
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
# coding: UTF-8
require 'dxruby'
require_relative './layer'
require_relative './wave'
require_relative './animation'
class Character < AnimeSprite
def self.set_wave(wave)
@@wave = wave
nil
end
def self.set_base(image_ary)
@@base = image_ary.dup
end
def self.set_background(image)
@@background = image
end
def self.wave
@@wave
end
def self.base
@@base
end
def self.background
@@background
end
def self.draw(*args)
args.each do |obj|
if Array === obj
self.draw(*obj) unless obj.empty?
else
obj.draw_base if obj.respond_to?(:draw_base)
obj.draw_body if obj.respond_to?(:draw_body)
obj.draw_back if obj.respond_to?(:draw_back)
end
end
nil
end
class CharacterBase < AnimeSprite
def initialize(type = :high)
case type
when :up
@type = type
@scale = -1
self.z = Layer[:character_base_up]
when :down
@type = type
@scale = 1
self.z = Layer[:character_base_down]
self.angle = 180
else
raise TypeError, "the type has set wrong (`#{type}' for :up or :down)"
end
super()
ary = self.animation_image = Character.base
add_animation(:usual, 60.div(ary.size), Array.new(ary.size){|i| i})
start_animation(:usual)
end
def update(dx = 0, dy = 0)
self.x = (w = Character.wave).x + w.width - self.image.width / 2 + dx
self.y = w.y + w.height / 2 + w.result(:pixel) * @scale - self.image.height / 2 + dy
update_animation
nil
end
end
class CharacterBackground < Sprite
def initialize(type)
super
self.image = Character.background
case type
when :up
@type = type
@scale = -1
self.z = Layer[:character_back_up]
when :down
@type = type
@scale = 1
self.z = Layer[:character_back_down]
else
raise TypeError, "the type has set wrong (`#{type}' for :up or :down)"
end
end
def update(dx = 0, dy = 0)
self.x = (w = Character.wave).x + w.width - self.image.width / 2 + dx
self.y = w.y + w.height / 2 + w.result(:pixel) * @scale - self.image.width / 2 + dy
end
end
def initialize(image, x_count = 1, y_count = 1, dx: 0, dy: 0, type: :up)
super()
@x_count = x_count
@y_count = y_count
@img = (Image === image ? image : Image.load(image))
@img = @img.sliceTiles(@x_count, @y_count)
@dx = dx
@dy = dy
case type
when :up
@type = type
@scale = -1
self.z = Layer[:character_body_up]
when :down
@type = type
@scale = 1
self.z = Layer[:character_body_down]
else
raise TypeError, "the type has set wrong (`#{type}' for :up or :down)"
end
@background = CharacterBackground.new(type)
@base = CharacterBase.new(type)
self.animation_image = @img
add_animation(:usual, 60.div(@x_count * @y_count), Array.new(@x_count * @y_count){|i| i})
start_animation(:usual)
self.collision = [w = self.image.width / 2, h = self.image.height / 2, (w + h) / 2]
end
def update
self.x = (w = Character.wave).x + w.width - self.image.width / 2 + @dx
self.y = w.y + w.height / 2 + w.result(:pixel) * @scale - self.image.width / 2 + @dy
@base.update(0, (- self.image.height / 2) * @scale)
@background.update(@dx,@dy)
self.update_animation
nil
end
def draw_body
draw
end
def draw_base
@base.draw
end
def draw_back
@background.draw
end
end