Report abuse

Copyright (C) 2006 by Han Dao


			
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#You can contract the author at wikipediankiba@gmail.com

#PROJECT: Dynpet
#DESCRIPTION: Dynpet is a computer program similiar to the Tamagotchi.
#FUNCTION OF THIS PART: Contain the Pet object
#TODO: Build a new complete Pet class here

class Pet
  include Rubygame::Sprites::Sprite
  def initialize
    super
    @rect =  Rubygame::Rect.new( 300 , 200 , 50 , 50 )
    #The inital image that is displayed when the game started up.
    @image = Rubygame::Image.load("data/pets/test1.png")
    @file
    @data
    #@animate contain data for rendering animation
    @anistring = 'left'
    @animate
    @counter = @cycle = 0
    @sequence = []
    @target = [333,333]
    @status = [false,false]
  end
  def move
    determine = ''
    ding = 0
    if @data['x'] > @target[0]
      @data['x'] -=1
      #left
      x = -1
      determine = 'left'
      ding += 1
    elsif @data['x'] < @target[0]
      @data['x'] +=1
      #right
      x = 1
      determine = 'right'
      ding += 1
    else
      x = 0
      @status[0] = true
    end
    if @data['y'] > @target[1]
      @data['y'] -= 1
      #up
      y = -1
      if ding == 1
        determine += '-up'
      else
        determine += 'up'
      end
    elsif @data['y'] < @target[1]
      @data['y'] +=1
      #down
      y = 1
      if ding == 1
        determine += '-down'
      else
        determine += 'down'
      end
    else
      y = 0
      @status[1] = true
    end
    seq_decide(determine)
    target_check
    update
  end
  def seq_decide mode
    case mode
      when 'left'
        #left
        animate_change(1)
      when 'right'
        #right
        animate_change(2)
      when 'down'
        #down
        animate_change(3)
      when 'up'
        #up
        animate_change(4)
      when 'left-down'
        #left-down
        animate_change(5)
      when 'right-down'
        animate_change(6)
      when 'left-up'
        animate_change(7)
      when 'right-up'
        animate_change(8)
    end
  end
  def update
    @rect.centerx = @data['x']
    @rect.centery = @data['y']
    @rect.centerx %= 800
    @rect.centery %= 600
  end
  def target_check
    add = 0
    @status.each do |done|
      if done == true
        add += 1
      end
    end
    @status[0] = false
    @status[1] = false
    if add == 2
      @target[0] = rand(801)
      @target[1] = rand(601)
    end
  end
  def data_rip
    @animate['file'].each do |file|
      @sequence << file
    end
  end
  def animate_change option , change = true
    case option
      when 1
        @anistring = 'left'
      when 2
        @anistring = 'right'
      when 3
        @anistring = 'down'
      when 4
        @anistring = 'up'
      when 5
        @anistring = 'left-down'
      when 6
        @anistring = 'right-down'
      when 7
        @anistring = 'left-up'
      when 8
        @anitstring = 'right-up'
    end
    if change == false
      @cycle = 0
    end
  end
  def animate rate
    #@counter and rate exist for the purpose of setting animation speed.
    if @counter == rate
      @counter = 0
      @image = Rubygame::Image.load(@sequence[@animate[@anistring][@cycle]])
      @cycle += 1
      if @cycle == 6
        @cycle = 0
      end
    end
    @counter +=1
  end
  def load data , main
    @data = data
    @file = Game_File.new(main)
    @animate = @file.yaml_read("data/pets/sprite.d_ani")
    data_rip
    update
  end
  def save
    @file.yaml_write @data , @data['filename']
  end
  def undraw(dest, background)
        background.blit(dest,@rect,@rect)
    end
end