root/lang/ruby/nario/nario/life.rb @ 17193

Revision 17193, 4.6 kB (checked in by authorNari, 5 years ago)

add nario directory

Line 
1require 'factor'
2
3module Life
4  class Life < Factor
5    attr_accessor :frame, :direction
6
7    def initialize(x, y, img)
8      super(x, y, img.w, img.h, img)
9      @slip_right_img = nil
10      @slip_left_img = nil
11      @jump_right_img = nil
12      @jump_left_img = nil
13      @animate_right = nil
14      @animate_left = nil
15      @dash_speed = 0
16      @jump_speed = 0
17      @fall_speed = 0
18      @is_fly = false
19      @direction = {:right => true, :left => false}
20    end
21
22
23    def put_screen(screen)
24      screen.put(@img, @x, @y) if @img
25    end
26
27    def dash(move_rate, animate_img, is_sp_update)
28      return if slip(move_rate)
29      set_direction(move_rate)
30      @dash_speed = move_rate if @dash_speed.zero?
31      @dash_speed += move_rate if is_sp_update
32      animate_dash(animate_img, @dash_speed)
33      x_move(@dash_speed)
34    end
35
36    def jump
37      unless @is_fly
38        @is_fly = true
39        change_img(@jump_left_img) if @direction[:left] and @jump_left_img
40        change_img(@jump_right_img) if @direction[:right] and @jump_right_img
41        agravity
42      else
43        @fall_speed -= 2 if (@frame % 4).zero? and @fall_speed < 0
44      end
45    end
46
47    def gravity
48      fall
49    end
50
51    def agravity
52        @fall_speed = -15
53        y_move(-2)
54    end
55
56    def frame_reset
57      @frame = 0
58    end
59
60    def force_notfall
61      @fall_speed = 0
62    end
63
64    def stand_up(floor)
65      @y = floor.y - @h - 1
66      @fall_speed = 0
67      @is_fly = false
68    end
69
70    def fall
71      @is_fly = true if @fall_speed.nonzero?
72      y_move(@fall_speed)
73      @fall_speed += 1 if @fall_speed < 15
74    end
75
76    def head_range
77      {:x_range => (@x+@w/6)..(@x+@w-@w/6), :y_range => @y..(@y+@h/6), :event => ("collide_#{extract_class_name(self)}_head".to_sym)}
78    end
79
80    def foot_range
81      {:x_range => (@x+@w/6)..(@x+@w-@w/6), :y_range => (@y+@h-@h/6)..(@y+@h), :event => ("collide_#{extract_class_name(self)}_foot".to_sym)}
82    end
83
84    def leftside_range
85      {:x_range => @x..(@x+@w/6), :y_range => (@y+@h/6)..(@y+@h-@h/6), :event => ("collide_#{extract_class_name(self)}_left".to_sym)}
86    end
87
88    def rightside_range
89      {:x_range => (@x+@w-@w/6)..(@x+@w), :y_range => (@y+@h/6)..(@y+@h-@h/6), :event => ("collide_#{extract_class_name(self)}_right".to_sym)}
90    end
91
92    #collision event
93    def collide_floor_head(f); stand_up(f); end
94    def collide_floor_left(f); @x = (f.x - @w); @dash_speed = 0; end
95    def collide_floor_right(f); @x = f.x + f.w; @dash_speed = 0; end
96    alias collide_weakblock_head collide_floor_head
97    alias collide_weakblock_left collide_floor_left
98    alias collide_weakblock_right collide_floor_right
99    alias collide_itembox_head collide_floor_head
100    alias collide_itembox_left collide_floor_left
101    alias collide_itembox_right collide_floor_right
102    alias collide_strongblock_head collide_floor_head
103    alias collide_strongblock_left collide_floor_left
104    alias collide_strongblock_right collide_floor_right
105    alias collide_pipe_head collide_floor_head
106    alias collide_pipe_left collide_floor_left
107    alias collide_pipe_right collide_floor_right
108
109
110    private
111    # frame % ? == 0;  get ? parameter
112    def get_dash_frame(dash_speed)
113      step_rate = 4
114      [[(4..7), 3], [(8..10), 2], [Range.new(11, 100, true), 1]].each{|i| step_rate = i.last if i.first.include? dash_speed.abs}
115      step_rate
116    end
117
118    def animate_dash(m_imgs, dash_speed)
119      return if @is_fly
120      animate(m_imgs, get_dash_frame(dash_speed))
121    end
122
123    def slip(move_rate)
124      # not turn?
125      return false unless (move_rate < 0 and @dash_speed > 0) or (move_rate > 0 and @dash_speed < 0)
126
127      #define slip_time. it's setting dash_speed
128      @slip_time = @dash_speed.abs unless @slip_time and @slip_time > 0
129
130      change_img(@slip_left_img) if @direction[:left] and !@is_fly
131      change_img(@slip_right_img) if @direction[:right] and !@is_fly
132      x_move(@dash_speed)
133      @dash_speed = 0 if (@slip_time -= 1).zero?
134      true
135    end
136
137    def set_direction(move_rate)
138      return if @is_fly
139      if move_rate >= 0
140        @direction[:right] = true
141        @direction[:left] = false
142      else
143        @direction[:right] = false
144        @direction[:left] = true
145      end
146    end
147
148    def force_jump
149      @is_fly = false
150      jump
151    end
152
153    def force_fall(w)
154      @fall_speed = -1
155      @y = w.y + w.h + 5
156    end
157  end
158
159  autoload :Nario, "life/nario"
160  autoload :Enemy, "life/enemy"
161  autoload :Kuribo, "life/kuribo"
162  autoload :NokoNoko, "life/nokonoko"
163  autoload :Carapace, "life/carapace"
164end
Note: See TracBrowser for help on using the browser.