Nyaruko69

Untitled

May 1st, 2020
181
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.  
  127. @[email protected]/@bitmap.height
  128. @animationFrames=@totalFrames*@frames
  129. # calculates total number of frames
  130. @loop_points=[0,@totalFrames]
  131. # first value is start, second is end
  132.  
  133. @actualBitmap=Bitmap.new(@width,@height)
  134. @actualBitmap.clear
  135. @actualBitmap.stretch_blt(Rect.new(0,0,@width,@height),@bitmap,Rect.new(@currentIndex*(@width/@scale),0,@width/@scale,@height/@scale))
  136. end
  137. alias initialize_elite initialize unless self.method_defined?(:initialize_elite)
  138.  
  139. def length; @totalFrames; end
  140. def disposed?; @actualBitmap.disposed?; end
  141. def dispose; @actualBitmap.dispose; end
  142. def copy; @actualBitmap.clone; end
  143. def bitmap; @actualBitmap; end
  144. def bitmap=(val); @actualBitmap=val; end
  145. def each; end
  146. def alterBitmap(index); return @strip[index]; end
  147.  
  148. def prepareStrip
  149. @strip=[]
  150. for i in 0...@totalFrames
  151. bitmap=Bitmap.new(@width,@height)
  152. bitmap.stretch_blt(Rect.new(0,0,@width,@height),@bitmapFile,Rect.new((@width/@scale)*i,0,@width/@scale,@height/@scale))
  153. @strip.push(bitmap)
  154. end
  155. end
  156. def compileStrip
  157. @bitmap.clear
  158. @bitmap.stretch_blt(Rect.new((@width/@scale)*i,0,@width/@scale,@height/@scale),@strip[i],Rect.new(0,0,@width,@height))
  159. end
  160. end
  161.  
  162. def reverse
  163. if @direction > 0
  164. @direction=-1
  165. elsif @direction < 0
  166. @direction=+1
  167. end
  168. end
  169.  
  170. def setLoop(start, finish)
  171. @loop_points=[start,finish]
  172. end
  173.  
  174. def setSpeed(value)
  175. @speed=value
  176. end
  177.  
  178. def toFrame(frame)
  179. if frame.is_a?(String)
  180. if frame=="last"
  181. frame=@totalFrames-1
  182. else
  183. frame=0
  184. end
  185. end
  186. frame=@totalFrames if frame > @totalFrames
  187. frame=0 if frame < 0
  188. @currentIndex=frame
  189. @actualBitmap.clear
  190. @actualBitmap.stretch_blt(Rect.new(0,0,@width,@height),@bitmap,Rect.new(@currentIndex*(@width/@scale),0,@width/@scale,@height/@scale))
  191. end
  192.  
  193. def play
  194. return if @currentIndex >= @loop_points[1]-1
  195. self.update
  196. end
  197.  
  198. def finished?
  199. return (@currentIndex==@totalFrames-1)
  200. end
  201.  
  202. def update
  203. return false if @actualBitmap.disposed?
  204. return false if @speed < 1
  205. case @speed
  206. # frame skip
  207. when 1
  208. @frames=2
  209. when 2
  210. @frames=4
  211. when 3
  212. @frames=5
  213. end
  214. @frame+=1
  215. if @frame >= @frames
  216. # processes animation speed
  217. @currentIndex+=@direction
  218. @currentIndex=@loop_points[0] if @currentIndex >=@loop_points[1]
  219. @currentIndex=@loop_points[1]-1 if @currentIndex < @loop_points[0]
  220. @frame=0
  221. end
  222. @actualBitmap.clear
  223. @actualBitmap.stretch_blt(Rect.new(0,0,@width,@height),@bitmap,Rect.new(@currentIndex*(@width/@scale),0,@width/@scale,@height/@scale))
  224. # updates the actual bitmap
  225. end
  226. alias update_elite update unless self.method_defined?(:update_elite)
  227.  
  228. # returns bitmap to original state
  229. def deanimate
  230. @frame=0
  231. @currentIndex=0
  232. @actualBitmap.clear
  233. @actualBitmap.stretch_blt(Rect.new(0,0,@width,@height),@bitmap,Rect.new(@currentIndex*(@width/@scale),0,@width/@scale,@height/@scale))
  234. end
  235. end
  236. #===============================================================================
  237. # New Sprite class to utilize the animated bitmap wrappers
  238. #===============================================================================
  239. class BitmapWrapperSprite < Sprite
  240.  
  241. def setBitmap(file,scale=POKEMONSPRITESCALE)
  242. @animatedBitmap = AnimatedBitmapWrapper.new(file,scale)
  243. self.bitmap = @animatedBitmap.bitmap.clone
  244. end
  245.  
  246. def setSpeciesBitmap(species,female=false,form=0,shiny=false,shadow=false,back=false,egg=false)
  247. if species > 0
  248. pokemon = PokeBattle_Pokemon.new(species,5)
  249. @animatedBitmap = pbLoadPokemonBitmapSpecies(pokemon, species, back)
  250. else
  251. @animatedBitmap = AnimatedBitmapWrapper.new("Graphics/Battlers/000")
  252. end
  253. self.bitmap = @animatedBitmap.bitmap.clone
  254. end
  255.  
  256. def play
  257. @animatedBitmap.play
  258. self.bitmap = @animatedBitmap.bitmap.clone
  259. end
  260.  
  261. def finished?; return @animatedBitmap.finished?; end
  262. def animatedBitmap; return @animatedBitmap; end
  263.  
  264. alias update_wrapper update unless self.method_defined?(:update_wrapper)
  265. def update
  266. update_wrapper
  267. return if @animatedBitmap.nil?
  268. @animatedBitmap.update
  269. self.bitmap = @animatedBitmap.bitmap.clone
  270. end
  271.  
  272. end
  273.  
  274. class AnimatedSpriteWrapper < BitmapWrapperSprite; end
  275. #===============================================================================
  276. # Aliases old PokemonBitmap generating functions and creates new ones,
  277. # utilizing the new BitmapWrapper
  278. #===============================================================================
  279. alias pbLoadPokemonBitmap_ebs pbLoadPokemonBitmap unless defined?(:pbLoadPokemonBitmap_ebs)
  280. def pbLoadPokemonBitmap(pokemon, back=false,scale=POKEMONSPRITESCALE)
  281. return pbLoadPokemonBitmapSpecies(pokemon,pokemon.species,back,scale)
  282. end
  283.  
  284. # Note: Returns an AnimatedBitmap, not a Bitmap
  285. alias pbLoadPokemonBitmapSpecies_ebs pbLoadPokemonBitmapSpecies unless defined?(:pbLoadPokemonBitmapSpecies_ebs)
  286. def pbLoadPokemonBitmapSpecies(pokemon, species, back=false, scale=POKEMONSPRITESCALE)
  287. ret=nil
  288. pokemon = pokemon.pokemon if pokemon.respond_to?(:pokemon)
  289. if pokemon.isEgg?
  290. bitmapFileName=sprintf("Graphics/Battlers/Eggs/%s",getConstantName(PBSpecies,species)) rescue nil
  291. if !pbResolveBitmap(bitmapFileName)
  292. bitmapFileName=sprintf("Graphics/Battlers/Eggs/%03d",species)
  293. if !pbResolveBitmap(bitmapFileName)
  294. bitmapFileName=sprintf("Graphics/Battlers/Eggs/000")
  295. end
  296. end
  297. bitmapFileName=pbResolveBitmap(bitmapFileName)
  298. else
  299. bitmapFileName=pbCheckPokemonBitmapFiles([species,back,
  300. (pokemon.isFemale?),
  301. pokemon.isShiny?,
  302. (pokemon.form rescue 0),
  303. (pokemon.isShadow? rescue false)])
  304. end
  305. raise missingPokeSpriteError(pokemon,back) if bitmapFileName.nil?
  306. animatedBitmap=AnimatedBitmapWrapper.new(bitmapFileName,scale) if bitmapFileName
  307. ret=animatedBitmap if bitmapFileName
  308. # Full compatibility with the alterBitmap methods is maintained
  309. # but unless the alterBitmap method gets rewritten and sprite animations get
  310. # hardcoded in the system, the bitmap alterations will not function properly
  311. # as they will not account for the sprite animation itself
  312.  
  313. # alterBitmap methods for static sprites will work just fine
  314. alterBitmap=(MultipleForms.getFunction(species,"alterBitmap") rescue nil) if !pokemon.isEgg? && animatedBitmap && animatedBitmap.totalFrames==1 # remove this totalFrames clause to allow for dynamic sprites too
  315. if bitmapFileName && alterBitmap
  316. animatedBitmap.prepareStrip
  317. for i in 0...animatedBitmap.totalFrames
  318. alterBitmap.call(pokemon,animatedBitmap.alterBitmap(i))
  319. end
  320. animatedBitmap.compileStrip
  321. ret=animatedBitmap
  322. end
  323. return ret
  324. end
  325.  
  326. # Note: Returns an AnimatedBitmap, not a Bitmap
  327. def pbLoadSpeciesBitmap(species,female=false,form=0,shiny=false,shadow=false,back=false,egg=false,scale=POKEMONSPRITESCALE)
  328. ret = nil
  329. if egg
  330. bitmapFileName=sprintf("Graphics/Battlers/Eggs/%s",getConstantName(PBSpecies,species)) rescue nil
  331. if !pbResolveBitmap(bitmapFileName)
  332. bitmapFileName=sprintf("Graphics/Battlers/Eggs/%03d",species)
  333. if !pbResolveBitmap(bitmapFileName)
  334. bitmapFileName=sprintf("Graphics/Battlers/Eggs/000")
  335. end
  336. end
  337. bitmapFileName=pbResolveBitmap(bitmapFileName)
  338. else
  339. bitmapFileName = pbCheckPokemonBitmapFiles([species,back,female,shiny,form,shadow])
  340. end
  341. if bitmapFileName
  342. ret = AnimatedBitmapWrapper.new(bitmapFileName,scale)
  343. end
  344. return ret
  345. end
  346.  
  347. # returns error message upon missing sprites
  348. def missingPokeSpriteError(pokemon,back)
  349. error_b = back ? "Back" : "Front"
  350. error_b += "Shiny" if pokemon.isShiny?
  351. error_b += "/Female/" if pokemon.isFemale?
  352. error_b += " shadow" if pokemon.isShadow?
  353. error_b += " form #{pokemon.form} " if pokemon.form > 0
  354. return "Woops, looks like you're missing the #{error_b} sprite for #{PBSpecies.getName(pokemon.species)}!"
  355. end
  356.  
  357. # new methods of handing Pokemon sprite name references
  358. def pbCheckPokemonBitmapFiles(params)
  359. species = params[0]
  360. back = params[1]
  361. factors = []
  362. factors.push([5,params[5],false]) if params[5] && params[5]!=false # shadow
  363. factors.push([2,params[2],false]) if params[2] && params[2]!=false # gender
  364. factors.push([3,params[3],false]) if params[3] && params[3]!=false # shiny
  365. factors.push([4,params[4].to_s,""]) if params[4] && params[4].to_s!="" && params[4].to_s!="0" # form
  366. tshadow = false
  367. tgender = false
  368. tshiny = false
  369. tform = ""
  370. for i in 0...2**factors.length
  371. for j in 0...factors.length
  372. case factors[j][0]
  373. when 2 # gender
  374. tgender = ((i/(2**j))%2==0) ? factors[j][1] : factors[j][2]
  375. when 3 # shiny
  376. tshiny = ((i/(2**j))%2==0) ? factors[j][1] : factors[j][2]
  377. when 4 # form
  378. tform = ((i/(2**j))%2==0) ? factors[j][1] : factors[j][2]
  379. when 5 # shadow
  380. tshadow = ((i/(2**j))%2==0) ? factors[j][1] : factors[j][2]
  381. end
  382. end
  383. folder = "Graphics/Battlers/"
  384. if tshiny && back
  385. folder += "BackShiny/"
  386. elsif tshiny
  387. folder += "FrontShiny/"
  388. elsif back
  389. folder += "Back/"
  390. else
  391. folder += "Front/"
  392. end
  393. folder += "Female/" if tgender
  394. bitmapFileName = sprintf("#{folder}%s%s%s",getConstantName(PBSpecies,species),(tform!="" ? "_"+tform : ""),tshadow ? "_shadow" : "") rescue nil
  395. ret = pbResolveBitmap(bitmapFileName)
  396. return ret if ret
  397. bitmapFileName = sprintf("#{folder}%03d%s%s",species,(tform!="" ? "_"+tform : ""),tshadow ? "_shadow" : "")
  398. ret = pbResolveBitmap(bitmapFileName)
  399. return ret if ret
  400. end
  401. return nil
  402. end
  403.  
  404. def pbPokemonBitmapFile(species, shiny, back=false)
  405. folder = "Graphics/Battlers/"
  406. if shiny && back
  407. folder += "BackShiny/"
  408. elsif shiny
  409. folder += "FrontShiny/"
  410. elsif back
  411. folder += "Back/"
  412. else
  413. folder += "Front/"
  414. end
  415. name = sprintf("#{folder}%s",getConstantName(PBSpecies,species)) rescue nil
  416. ret = pbResolveBitmap(name)
  417. return ret if ret
  418. name = sprintf("#{folder}%03d",species)
  419. return pbResolveBitmap(name)
  420. end
Add Comment
Please, Sign In to add comment