7日目

アニメーションを作る事と意識して書いてみた。
@cast,@movieでシナリオを表現できないかな?って試作です。
他にも方法があるかもしれないけど、まずは、これで。
Cellクラスを一括で管理できるようにPhaseクラスに@cell_list を置いた。
ここで、一工夫。
Cell.newをしてから@cell_listに登録するのじゃなくて、
Cell.newにPhaseを入れる事(Phasクラスの中でCell.new(self))で初期化の時に自動で登録できる。
ボケ防止だね。
bilt転送の方法の切り替えはTransformモジュールのインクルード(ここではextend)でする。
Cellのupdateは自動のプログラム
Phaseのupdateはコントロールのプログラム
て感じかな。


#!/usr/bin/env ruby

require 'rubygems'
require 'sdl'

module Items
module SpaceShip
def load_item
self.get_surface(50,50)
dot =[
[0,1,0,0,0,0,0,0,1,0],
[0,1,0,0,1,1,0,0,1,0],
[0,0,1,1,1,1,1,1,0,0],
[1,1,1,1,1,1,1,1,1,1],
[1,0,1,0,1,1,0,1,0,1],
[1,0,1,0,1,1,0,1,0,1],
[1,1,1,1,1,1,1,1,1,1],
[0,0,1,1,1,1,1,1,0,0],
[0,1,1,0,0,0,0,1,1,0],
[1,0,0,0,0,0,0,0,0,1]
]
for i in 0..9
for j in 0..9
@surface.fill_rect(j * 5,i * 5,5,5,[46,139,87]) if dot[i][j] == 1
end
end
@surface.set_color_key(SDL::SRCCOLORKEY,@surface.get_pixel(0,0))
end
end
module Face
def load_item
self.get_surface(50,50)
self.set_font("VL-Gothic-Regular.ttf",20)
@method = {
0 => proc{@font.draw_blended_utf8(@surface,"^o^",8,14,1,1,1)},
1 => proc{@font.draw_blended_utf8(@surface,"T_T",8,14,1,1,1)},
2 => proc{@font.draw_blended_utf8(@surface,">_<",8,14,1,1,1)}
}
@time = SDL.get_ticks
@idx = 0
end
def update
@angle -= 1
@surface.draw_circle(25,25,23,[244,164,96],true)
if SDL.get_ticks - @time > 1000
@idx += 1
@time = SDL.get_ticks
@idx = 0 if @idx == 3
end
@method[@idx].call
@surface.set_color_key(SDL::SRCCOLORKEY,@surface.get_pixel(0,0))
end
end
module CrescentMoon
def load_item
self.get_surface(50,50)
@surface.draw_circle(25,25,18,[255,50,0],true)
@surface.draw_circle(20,30,18,@surface.get_pixel(0,0),true)
@surface.set_color_key(SDL::SRCCOLORKEY,@surface.get_pixel(0,0))
end
def update
@angle += 1
end
end
end

module Transform
def blit
SDL::Surface.transform_blit(@surface,@home.screen,@angle,@xscale,@yscale,@px,@py,@qx,@qy,SDL::TRANSFORM_SAFE) if @enabled
end
end

class Cell
attr_accessor :x,:y,:angle,:px,:py,:qx,:qy,:enabled
def initialize(phase)
@home = phase
@home.cell_list << self
@x,@y = 0,0
@angle,@xscale,@yscale,@px,@py,@qx,@qy = 0,1.0,1.0,0,0,0,0
@enabled = true
end
def get_surface(w,h)
@w,@h = w,h
@surface = SDL::Surface.new(SDL::SWSURFACE,@w,@h,@home.screen)
end
def set_font(ttf_font_file,size)
@font = SDL::TTF.open(ttf_font_file,size)
@font_color = :black
@text = " "
end
def render_font(text,r,g,b)
@surface = @font.render_blended_utf8(text,r,g,b)
@w,@h = @surface.w,@surface.h
end
def update
end
def blit
SDL::Surface.blit(@surface,0,0,@w,@h,@home.screen,@x,@y) if @enabled
end
def each
yield(self)
end
end

class Phase
attr_accessor :screen,:cell_list
def initialize(screen)
@screen = screen
@w,@h = @screen.w,@screen.h
@cell_list = []
@cast = {
:crescent_moon =>
Cell.new(self).each do |item|
item.extend(Transform)
item.extend(Items::CrescentMoon)
item.load_item
item.x,item.y = 280,220
item.px,item.py,item.qx,item.qy = 100,100,225,225
item
end,
:ship =>
Cell.new(self).each do |item|
item.extend(Items::SpaceShip)
item.load_item
item.load_item
item
end,
:face =>
Cell.new(self).each do |item|
item.extend(Transform)
item.extend(Items::Face)
item.load_item
item.px,item.py,item.qx,item.qy = 25,25,220,220
item
end,
:font =>
Cell.new(self).each do |item|
item.set_font("VL-Gothic-Regular.ttf",30)
item.render_font("Thank you",255,255,0)
item.x,item.y = 150,150
item.enabled = false
item
end
}
@cue = :scene0
@movie = {
:scene0 =>
proc{
@cast[:ship].each do |item|
item.x,item.y = 10,10
end
@cast[:font].enabled = false
@cue = :scene1
},
:scene1 =>
proc{
@cast[:ship].each do |item|
item.x += 1
item.y += 1
@cue = :scene2 if item.x > 195
end
},
:scene2 =>
proc{
@cast[:font].enabled = true
@time = SDL.get_ticks if @time.nil?
if SDL.get_ticks - @time > 1000
@time = nil
@cue = :scene0
end
}
}
end

def update
@screen.fill_rect(0,0,@screen.w,@screen.h,[10,10,120])
@movie[@cue].call
@cell_list.each do|cell|
cell.update
cell.blit
end
end
def run
loop do
while event=SDL::Event.poll
case event
when SDL::Event::Quit
exit
when SDL::Event::KeyDown
exit if event.sym == SDL::Key::ESCAPE
end
end
SDL.delay(20)
self.update
@screen.update_rect(0,0,0,0)
end
end
end

SDL.init( SDL::INIT_VIDEO )
SDL::TTF.init
SDL::Mouse.hide
screen = SDL::setVideoMode(400,400,16,SDL::SWSURFACE)
Phase.new(screen).run
[PR]
by gaziya | 2010-08-28 10:05