mirror of
				https://github.com/ctruLua/ctruLua.git
				synced 2025-10-27 16:39:29 +00:00 
			
		
		
		
	Added a sprite library (unfinished, but working)
This commit is contained in:
		
							parent
							
								
									6a26be4b43
								
							
						
					
					
						commit
						a4a6e09d74
					
				
					 2 changed files with 82 additions and 0 deletions
				
			
		
							
								
								
									
										72
									
								
								sdcard/3ds/ctruLua/libs/sprite.lua
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										72
									
								
								sdcard/3ds/ctruLua/libs/sprite.lua
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,72 @@ | |||
| local gfx = require("ctr.gfx") | ||||
| local ctr = require("ctr") | ||||
| local mod = {} | ||||
| 
 | ||||
| -- Module functions | ||||
| local function getBox(tx, ty, i, sx, sy) | ||||
|   --i = i - 1 | ||||
|   x = (i%tx) | ||||
|   y = math.floor(i/tx) | ||||
|    | ||||
| 	return (x*sx), (y*sy) | ||||
| end | ||||
| 
 | ||||
| -- Sprite object methods | ||||
| local function draw(self, x, y, rad) | ||||
|   if not self.animations[self.currentAnimation] then return 0 end | ||||
|   local frame = self.animations[self.currentAnimation].animation[self.currentFrame] | ||||
|   local tsx, tsy = self.texture:getSize() | ||||
|    | ||||
|   local sx, sy = getBox(tsx, tsy, frame, self.frameSizeX, self.frameSizeY) | ||||
|   gfx.text(200, 200, sx.."/"..sy) | ||||
| 	self.texture:drawPart(x, y, sx, sy, self.frameSizeX, self.frameSizeY, rad) | ||||
| 	 | ||||
| 	if (ctr.time()-self.frameTimer) >= self.animations[self.currentAnimation].delay then | ||||
| 	  self.currentFrame = (self.currentFrame+1) | ||||
| 	  self.frameTimer = ctr.time() | ||||
| 	  if self.currentFrame > #self.animations[self.currentAnimation].animation then | ||||
| 	    self.currentFrame = 1 | ||||
| 	  end | ||||
| 	end | ||||
| 	 | ||||
| 	return frame | ||||
| end | ||||
| 
 | ||||
| local function addAnimation(self, anim, delay) | ||||
| 	self.animations[#self.animations+1] = {animation=anim, delay=delay} | ||||
| 	return #self.animations | ||||
| end | ||||
| 
 | ||||
| -- Set to 0 to hide the sprite | ||||
| local function setAnimation(self, anim) | ||||
| 	self.currentAnimation = anim | ||||
| 	self.currentFrame = 1 | ||||
| 	if not self.animations[anim] then | ||||
| 		return false | ||||
| 	end | ||||
| 	return true | ||||
| end | ||||
| 
 | ||||
| local function resetTimer(self) | ||||
| 	self.frameTimer = ctr.time() | ||||
| end | ||||
| 
 | ||||
| -- Sprite object constructor | ||||
| function mod.new(texture, fsx, fsy) | ||||
| 	return { | ||||
| 		texture = texture, | ||||
| 		frameSizeX = fsx, | ||||
| 		frameSizeY = fsy, | ||||
| 		animations = {}, | ||||
| 		currentAnimation = 0, | ||||
| 		currentFrame = 1, | ||||
| 		frameTimer = 0, | ||||
| 		 | ||||
| 		draw = draw, | ||||
| 		addAnimation = addAnimation, | ||||
| 		setAnimation = setAnimation, | ||||
| 		resetTimer = resetTimer, | ||||
| 	} | ||||
| end | ||||
| 
 | ||||
| return mod | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Firew0lf
						Firew0lf