Nyaruko69

Untitled

May 1st, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.84 KB | None | 0 0
  1. TRAINERSPRITESCALE = 2
  2. # used to scale the Pokemon bitmaps to 200%
  3. POKEMONSPRITESCALE = 2
  4. # used to scale the backsprite for battle perspective (200%)
  5. BACKSPRITESCALE = 2
  6.  
  7. class DynamicPokemonSprite
  8. attr_accessor :shadow
  9. attr_accessor :sprite
  10. attr_accessor :showshadow
  11. attr_accessor :status
  12. attr_accessor :hidden
  13. attr_accessor :fainted
  14. attr_accessor :anim
  15. attr_accessor :charged
  16. attr_accessor :isShadow
  17. attr_reader :loaded
  18. attr_reader :selected
  19. attr_reader :isSub
  20. attr_reader :viewport
  21. attr_reader :pulse
  22.  
  23. def initialize(doublebattle,index,viewport=nil)
  24. @viewport=viewport
  25. @metrics=load_data("Data/metrics.dat")
  26. @selected=0
  27. @frame=0
  28. @frame2=0
  29. @frame3=0
  30.  
  31. @status=0
  32. @loaded=false
  33. @charged=false
  34. @index=index
  35. @doublebattle=doublebattle
  36. @showshadow=true
  37. @altitude=30
  38. @yposition=30
  39. @shadow=Sprite.new(@viewport)
  40. @sprite=Sprite.new(@viewport)
  41. back=(@index%2==0)
  42. @substitute=AnimatedBitmapWrapper.new("Graphics/Battlers/"+(back ? "substitute_back" : "substitute"),POKEMONSPRITESCALE)
  43. @overlay=Sprite.new(@viewport)
  44. @isSub=false
  45. @lock=false
  46. @pokemon=nil
  47. @still=false
  48. @hidden=false
  49. @fainted=false
  50. @anim=false
  51. @isShadow=false
  52.  
  53. @fp = {}
  54.  
  55. @pulse = 8
  56. @k = 1
  57. end
  58.  
  59. def setPokemonBitmap(pokemon,back=false,species=nil)
  60. self.resetParticles
  61. return if !pokemon || pokemon.nil?
  62. @pokemon = pokemon
  63. @isShadow = true if @pokemon.isShadow?
  64. @altitude = @metrics[2][pokemon.species]
  65. if back
  66. @yposition = @metrics[0][pokemon.species]
  67. @altitude *= 0.5
  68. else
  69. @yposition = @metrics[1][pokemon.species]
  70. end
  71. scale = back ? BACKSPRITESCALE : POKEMONSPRITESCALE
  72. if !species.nil?
  73. @bitmap = pbLoadPokemonBitmapSpecies(pokemon,species,back,scale)
  74. else
  75. @bitmap = pbLoadPokemonBitmap(pokemon,back,scale)
  76. end
  77. @sprite.bitmap = @bitmap.bitmap.clone
  78. @shadow.bitmap = @bitmap.bitmap.clone
  79. @sprite.ox = @bitmap.width/2
  80. @sprite.oy = @bitmap.height
  81. @sprite.oy += @altitude
  82. @sprite.oy -= @yposition
  83. @sprite.oy -= pokemon.formOffsetY if pokemon.respond_to?(:formOffsetY)
  84.  
  85. @fainted = false
  86. @loaded = true
  87. @hidden = false
  88. self.visible = true
  89. @pulse = 8
  90. @k = 1
  91. self.formatShadow
  92. end
  93. end
  94. class AnimatedBitmapWrapper
  95. attr_reader :width
  96. attr_reader :height
  97. attr_reader :totalFrames
  98. attr_reader :animationFrames
  99. attr_reader :currentIndex
  100. attr_accessor :scale
  101.  
  102. def initialize(file,scale=2)
  103. raise "filename is nil" if file==nil
  104. raise ".gif files are not supported!" if File.extname(file)==".gif"
  105.  
  106. @scale = scale
  107. @width = 0
  108. @height = 0
  109. @frame = 0
  110. @frames = 2
  111. @direction = +1
  112. @animationFinish = false
  113. @totalFrames = 0
  114. @currentIndex = 0
  115. @speed = 1
  116. # 0 - not moving at all
  117. # 1 - normal speed
  118. # 2 - medium speed
  119. # 3 - slow speed
  120. bmp = BitmapCache.load_bitmap(file)
  121. #bmp = Bitmap.new(file)
  122. @bitmapFile=Bitmap.new(bmp.width,bmp.height); @bitmapFile.blt(0,0,bmp,Rect.new(0,0,bmp.width,bmp.height))
  123. # initializes full Pokemon bitmap
  124. @bitmap=Bitmap.new(@bitmapFile.width,@bitmapFile.height)
  125. @bitmap.blt(0,0,@bitmapFile,Rect.new(0,0,@bitmapFile.width,@bitmapFile.height))
  126. @width=@bitmapFile.height*@scale
  127. @height=@bitmap.height*@scale
  128.  
  129. @totalFrames=@bitmap.width/@bitmap.height
  130. @animationFrames=@totalFrames*@frames
  131. # calculates total number of frames
  132. @loop_points=[0,@totalFrames]
  133. # first value is start, second is end
  134.  
  135. @actualBitmap=Bitmap.new(@width,@height)
  136. @actualBitmap.clear
  137. @actualBitmap.stretch_blt(Rect.new(0,0,@width,@height),@bitmap,Rect.new(@currentIndex*(@width/@scale),0,@width/@scale,@height/@scale))
  138. end
  139. alias initialize_elite initialize unless self.method_defined?(:initialize_elite)
  140.  
  141. def length; @totalFrames; end
  142. def disposed?; @actualBitmap.disposed?; end
  143. def dispose; @actualBitmap.dispose; end
  144. def copy; @actualBitmap.clone; end
  145. def bitmap; @actualBitmap; end
  146. def bitmap=(val); @actualBitmap=val; end
  147. def each; end
  148. def alterBitmap(index); return @strip[index]; end
  149.  
  150. def prepareStrip
  151. @strip=[]
  152. for i in 0...@totalFrames
  153. bitmap=Bitmap.new(@width,@height)
  154. bitmap.stretch_blt(Rect.new(0,0,@width,@height),@bitmapFile,Rect.new((@width/@scale)*i,0,@width/@scale,@height/@scale))
  155. @strip.push(bitmap)
  156. end
  157. end
  158. def compileStrip
  159. @bitmap.clear
  160. for i in 0...@strip.length
  161. @bitmap.stretch_blt(Rect.new((@width/@scale)*i,0,@width/@scale,@height/@scale),@strip[i],Rect.new(0,0,@width,@height))
  162. end
  163. end
  164.  
  165. def reverse
  166. if @direction > 0
  167. @direction=-1
  168. elsif @direction < 0
  169. @direction=+1
  170. end
  171. end
  172.  
  173. def setLoop(start, finish)
  174. @loop_points=[start,finish]
  175. end
  176.  
  177. def setSpeed(value)
  178. @speed=value
  179. end
  180.  
  181. def toFrame(frame)
  182. if frame.is_a?(String)
  183. if frame=="last"
  184. frame=@totalFrames-1
  185. else
  186. frame=0
  187. end
  188. end
  189. frame=@totalFrames if frame > @totalFrames
  190. frame=0 if frame < 0
  191. @currentIndex=frame
  192. @actualBitmap.clear
  193. @actualBitmap.stretch_blt(Rect.new(0,0,@width,@height),@bitmap,Rect.new(@currentIndex*(@width/@scale),0,@width/@scale,@height/@scale))
  194. end
  195.  
  196. def play
  197. return if @currentIndex >= @loop_points[1]-1
  198. self.update
  199. end
  200.  
  201. def finished?
  202. return (@currentIndex==@totalFrames-1)
  203. end
  204.  
  205. def update
  206. return false if @actualBitmap.disposed?
  207. return false if @speed < 1
  208. case @speed
  209. # frame skip
  210. when 1
  211. @frames=2
  212. when 2
  213. @frames=4
  214. when 3
  215. @frames=5
  216. end
  217. @frame+=1
  218. if @frame >= @frames
  219. # processes animation speed
  220. @currentIndex+=@direction
  221. @currentIndex=@loop_points[0] if @currentIndex >=@loop_points[1]
  222. @currentIndex=@loop_points[1]-1 if @currentIndex < @loop_points[0]
  223. @frame=0
  224. end
  225. @actualBitmap.clear
  226. @actualBitmap.stretch_blt(Rect.new(0,0,@width,@height),@bitmap,Rect.new(@currentIndex*(@width/@scale),0,@width/@scale,@height/@scale))
  227. # updates the actual bitmap
  228. end
  229. alias update_elite update unless self.method_defined?(:update_elite)
  230.  
  231. # returns bitmap to original state
  232. def deanimate
  233. @frame=0
  234. @currentIndex=0
  235. @actualBitmap.clear
  236. @actualBitmap.stretch_blt(Rect.new(0,0,@width,@height),@bitmap,Rect.new(@currentIndex*(@width/@scale),0,@width/@scale,@height/@scale))
  237. end
  238. end
  239. #===============================================================================
  240. # New Sprite class to utilize the animated bitmap wrappers
  241. #===============================================================================
  242. class BitmapWrapperSprite < Sprite
  243.  
  244. def setBitmap(file,scale=POKEMONSPRITESCALE)
  245. @animatedBitmap = AnimatedBitmapWrapper.new(file,scale)
  246. self.bitmap = @animatedBitmap.bitmap.clone
  247. end
  248.  
  249. def setSpeciesBitmap(species,female=false,form=0,shiny=false,shadow=false,back=false,egg=false)
  250. if species > 0
  251. pokemon = PokeBattle_Pokemon.new(species,5)
  252. @animatedBitmap = pbLoadPokemonBitmapSpecies(pokemon, species, back)
  253. else
  254. @animatedBitmap = AnimatedBitmapWrapper.new("Graphics/Battlers/000")
  255. end
  256. self.bitmap = @animatedBitmap.bitmap.clone
  257. end
  258.  
  259. def play
  260. @animatedBitmap.play
  261. self.bitmap = @animatedBitmap.bitmap.clone
  262. end
  263.  
  264. def finished?; return @animatedBitmap.finished?; end
  265. def animatedBitmap; return @animatedBitmap; end
  266.  
  267. alias update_wrapper update unless self.method_defined?(:update_wrapper)
  268. def update
  269. update_wrapper
  270. return if @animatedBitmap.nil?
  271. @animatedBitmap.update
  272. self.bitmap = @animatedBitmap.bitmap.clone
  273. end
  274.  
  275. end
  276.  
  277. class AnimatedSpriteWrapper < BitmapWrapperSprite; end
  278. #===============================================================================
  279. # Aliases old PokemonBitmap generating functions and creates new ones,
  280. # utilizing the new BitmapWrapper
  281. #===============================================================================
  282. alias pbLoadPokemonBitmap_ebs pbLoadPokemonBitmap unless defined?(:pbLoadPokemonBitmap_ebs)
  283. def pbLoadPokemonBitmap(pokemon, back=false,scale=POKEMONSPRITESCALE)
  284. return pbLoadPokemonBitmapSpecies(pokemon,pokemon.species,back,scale)
  285. end
  286.  
  287. # Note: Returns an AnimatedBitmap, not a Bitmap
  288. alias pbLoadPokemonBitmapSpecies_ebs pbLoadPokemonBitmapSpecies unless defined?(:pbLoadPokemonBitmapSpecies_ebs)
  289. def pbLoadPokemonBitmapSpecies(pokemon, species, back=false, scale=POKEMONSPRITESCALE)
  290. ret=nil
  291. pokemon = pokemon.pokemon if pokemon.respond_to?(:pokemon)
  292. if pokemon.isEgg?
  293. bitmapFileName=sprintf("Graphics/Battlers/Eggs/%s",getConstantName(PBSpecies,species)) rescue nil
  294. if !pbResolveBitmap(bitmapFileName)
  295. bitmapFileName=sprintf("Graphics/Battlers/Eggs/%03d",species)
  296. if !pbResolveBitmap(bitmapFileName)
  297. bitmapFileName=sprintf("Graphics/Battlers/Eggs/000")
  298. end
  299. end
  300. bitmapFileName=pbResolveBitmap(bitmapFileName)
  301. else
  302. bitmapFileName=pbCheckPokemonBitmapFiles([species,back,
  303. (pokemon.isFemale?),
  304. pokemon.isShiny?,
  305. (pokemon.form rescue 0),
  306. (pokemon.isShadow? rescue false)])
  307. end
  308. raise missingPokeSpriteError(pokemon,back) if bitmapFileName.nil?
  309. animatedBitmap=AnimatedBitmapWrapper.new(bitmapFileName,scale) if bitmapFileName
  310. ret=animatedBitmap if bitmapFileName
  311. # Full compatibility with the alterBitmap methods is maintained
  312. # but unless the alterBitmap method gets rewritten and sprite animations get
  313. # hardcoded in the system, the bitmap alterations will not function properly
  314. # as they will not account for the sprite animation itself
  315.  
  316. # alterBitmap methods for static sprites will work just fine
  317. alterBitmap=(MultipleForms.getFunction(species,"alterBitmap") rescue nil) if !pokemon.isEgg? && animatedBitmap && animatedBitmap.totalFrames==1 # remove this totalFrames clause to allow for dynamic sprites too
  318. if bitmapFileName && alterBitmap
  319. animatedBitmap.prepareStrip
  320. for i in 0...animatedBitmap.totalFrames
  321. alterBitmap.call(pokemon,animatedBitmap.alterBitmap(i))
  322. end
  323. animatedBitmap.compileStrip
  324. ret=animatedBitmap
  325. end
  326. return ret
  327. end
  328.  
  329. # Note: Returns an AnimatedBitmap, not a Bitmap
  330. def pbLoadSpeciesBitmap(species,female=false,form=0,shiny=false,shadow=false,back=false,egg=false,scale=POKEMONSPRITESCALE)
  331. ret = nil
  332. if egg
  333. bitmapFileName=sprintf("Graphics/Battlers/Eggs/%s",getConstantName(PBSpecies,species)) rescue nil
  334. if !pbResolveBitmap(bitmapFileName)
  335. bitmapFileName=sprintf("Graphics/Battlers/Eggs/%03d",species)
  336. if !pbResolveBitmap(bitmapFileName)
  337. bitmapFileName=sprintf("Graphics/Battlers/Eggs/000")
  338. end
  339. end
  340. bitmapFileName=pbResolveBitmap(bitmapFileName)
  341. else
  342. bitmapFileName = pbCheckPokemonBitmapFiles([species,back,female,shiny,form,shadow])
  343. end
  344. if bitmapFileName
  345. ret = AnimatedBitmapWrapper.new(bitmapFileName,scale)
  346. end
  347. return ret
  348. end
  349.  
  350. # returns error message upon missing sprites
  351. def missingPokeSpriteError(pokemon,back)
  352. error_b = back ? "Back" : "Front"
  353. error_b += "Shiny" if pokemon.isShiny?
  354. error_b += "/Female/" if pokemon.isFemale?
  355. error_b += " shadow" if pokemon.isShadow?
  356. error_b += " form #{pokemon.form} " if pokemon.form > 0
  357. return "Woops, looks like you're missing the #{error_b} sprite for #{PBSpecies.getName(pokemon.species)}!"
  358. end
  359.  
  360. # new methods of handing Pokemon sprite name references
  361. def pbCheckPokemonBitmapFiles(params)
  362. species = params[0]
  363. back = params[1]
  364. factors = []
  365. factors.push([5,params[5],false]) if params[5] && params[5]!=false # shadow
  366. factors.push([2,params[2],false]) if params[2] && params[2]!=false # gender
  367. factors.push([3,params[3],false]) if params[3] && params[3]!=false # shiny
  368. factors.push([4,params[4].to_s,""]) if params[4] && params[4].to_s!="" && params[4].to_s!="0" # form
  369. tshadow = false
  370. tgender = false
  371. tshiny = false
  372. tform = ""
  373. for i in 0...2**factors.length
  374. for j in 0...factors.length
  375. case factors[j][0]
  376. when 2 # gender
  377. tgender = ((i/(2**j))%2==0) ? factors[j][1] : factors[j][2]
  378. when 3 # shiny
  379. tshiny = ((i/(2**j))%2==0) ? factors[j][1] : factors[j][2]
  380. when 4 # form
  381. tform = ((i/(2**j))%2==0) ? factors[j][1] : factors[j][2]
  382. when 5 # shadow
  383. tshadow = ((i/(2**j))%2==0) ? factors[j][1] : factors[j][2]
  384. end
  385. end
  386. folder = "Graphics/Battlers/"
  387. if tshiny && back
  388. folder += "BackShiny/"
  389. elsif tshiny
  390. folder += "FrontShiny/"
  391. elsif back
  392. folder += "Back/"
  393. else
  394. folder += "Front/"
  395. end
  396. folder += "Female/" if tgender
  397. bitmapFileName = sprintf("#{folder}%s%s%s",getConstantName(PBSpecies,species),(tform!="" ? "_"+tform : ""),tshadow ? "_shadow" : "") rescue nil
  398. ret = pbResolveBitmap(bitmapFileName)
  399. return ret if ret
  400. bitmapFileName = sprintf("#{folder}%03d%s%s",species,(tform!="" ? "_"+tform : ""),tshadow ? "_shadow" : "")
  401. ret = pbResolveBitmap(bitmapFileName)
  402. return ret if ret
  403. end
  404. return nil
  405. end
  406.  
  407. def pbPokemonBitmapFile(species, shiny, back=false)
  408. folder = "Graphics/Battlers/"
  409. if shiny && back
  410. folder += "BackShiny/"
  411. elsif shiny
  412. folder += "FrontShiny/"
  413. elsif back
  414. folder += "Back/"
  415. else
  416. folder += "Front/"
  417. end
  418. name = sprintf("#{folder}%s",getConstantName(PBSpecies,species)) rescue nil
  419. ret = pbResolveBitmap(name)
  420. return ret if ret
  421. name = sprintf("#{folder}%03d",species)
  422. return pbResolveBitmap(name)
  423. end
Add Comment
Please, Sign In to add comment