add early coin random generation
This commit is contained in:
		
							parent
							
								
									c637fceca7
								
							
						
					
					
						commit
						12365663e2
					
				
					 1 changed files with 103 additions and 5 deletions
				
			
		
							
								
								
									
										108
									
								
								right_coin.lua
									
										
									
									
									
								
							
							
						
						
									
										108
									
								
								right_coin.lua
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -38,9 +38,94 @@ anis={
 | 
			
		|||
	idle={257},
 | 
			
		||||
	run={273,274},
 | 
			
		||||
	jump={289},
 | 
			
		||||
	fall={290}
 | 
			
		||||
	fall={290},
 | 
			
		||||
	coin={
 | 
			
		||||
		blue=  {321,321,322,322,323,323,324,324},
 | 
			
		||||
		red=   {337,337,338,338,339,339,340,340},
 | 
			
		||||
		yellow={353,353,354,354,355,355,356,356},
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function drawActor(self)
 | 
			
		||||
	self.spr=self.ani[(((t)//3)% #self.ani)+1]
 | 
			
		||||
		local flip
 | 
			
		||||
		if self.isFlipped then flip = 1 else flip = 0 end
 | 
			
		||||
		spr(self.spr, self.x, self.y, 1, 1, flip)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
colors={"blue","red","yellow"}
 | 
			
		||||
spawnLocs={
 | 
			
		||||
	{x=28*8,y=5*8},
 | 
			
		||||
	{x=12*8,y=11*8},
 | 
			
		||||
	{x=20*8,y=9*8},
 | 
			
		||||
	{x=7*8,y=13*8}
 | 
			
		||||
}
 | 
			
		||||
coins={}
 | 
			
		||||
do -- Coin
 | 
			
		||||
	Coin={
 | 
			
		||||
			color=colors[1],
 | 
			
		||||
			ani=anis.coin.red,
 | 
			
		||||
			spr=0,
 | 
			
		||||
			x=0,y=0,
 | 
			
		||||
			w=8,h=8,
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	function Coin:new(o)
 | 
			
		||||
		o = o or {}
 | 
			
		||||
		self.__index = self
 | 
			
		||||
		setmetatable(o, self)
 | 
			
		||||
		return o
 | 
			
		||||
	end
 | 
			
		||||
	
 | 
			
		||||
	function Coin:setColor(c)
 | 
			
		||||
		self.color = c
 | 
			
		||||
		if c=="red" then self.ani=anis.coin.red
 | 
			
		||||
		elseif c=="blue" then self.ani=anis.coin.blue
 | 
			
		||||
		elseif c=="yellow" then self.ani=anis.coin.yellow
 | 
			
		||||
		end
 | 
			
		||||
	end
 | 
			
		||||
	
 | 
			
		||||
	function Coin:draw()
 | 
			
		||||
		drawActor(self)
 | 
			
		||||
	end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function randomColor()
 | 
			
		||||
	return colors[math.random(#colors)]
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function coinAtLoc(loc)
 | 
			
		||||
  for i,v in pairs(coins) do
 | 
			
		||||
  	if v.x == loc.x and v.y == loc.y then return true end
 | 
			
		||||
  end
 | 
			
		||||
  return false
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function randomValidSpawnLoc()
 | 
			
		||||
	local loc
 | 
			
		||||
	repeat loc=spawnLocs[math.random(#spawnLocs)]
 | 
			
		||||
	until not coinAtLoc(loc)
 | 
			
		||||
	return loc
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function canAddCoin()
 | 
			
		||||
	return #coins < #spawnLocs
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function addRandomCoin()
 | 
			
		||||
	if not canAddCoin() then return end
 | 
			
		||||
	local newCoin=Coin:new()
 | 
			
		||||
	local loc=randomValidSpawnLoc()
 | 
			
		||||
	newCoin:setColor(randomColor())
 | 
			
		||||
	newCoin.x=loc.x
 | 
			
		||||
	newCoin.y=loc.y
 | 
			
		||||
	coins[#coins + 1]=newCoin
 | 
			
		||||
end
 | 
			
		||||
-- MOCK
 | 
			
		||||
addRandomCoin()
 | 
			
		||||
addRandomCoin()
 | 
			
		||||
-- /MOCK
 | 
			
		||||
 | 
			
		||||
do -- Player
 | 
			
		||||
	Player = {
 | 
			
		||||
		spr=0,
 | 
			
		||||
| 
						 | 
				
			
			@ -114,10 +199,7 @@ do -- Player
 | 
			
		|||
	end
 | 
			
		||||
	
 | 
			
		||||
	function Player:draw()
 | 
			
		||||
		self.spr=self.ani[(((t)//3)% #self.ani)+1]
 | 
			
		||||
		local flip
 | 
			
		||||
		if self.isFlipped then flip = 1 else flip = 0 end
 | 
			
		||||
		spr(self.spr, self.x, self.y, 1, 1, flip)
 | 
			
		||||
		drawActor(self)
 | 
			
		||||
	end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -145,6 +227,10 @@ end
 | 
			
		|||
function drawGame()
 | 
			
		||||
	map(0,0,30,17)
 | 
			
		||||
	p:draw()
 | 
			
		||||
	
 | 
			
		||||
	for i,v in pairs(coins) do
 | 
			
		||||
		v:draw()
 | 
			
		||||
	end 
 | 
			
		||||
 | 
			
		||||
	-- Life
 | 
			
		||||
	spr(83,0,0)
 | 
			
		||||
| 
						 | 
				
			
			@ -373,6 +459,18 @@ end
 | 
			
		|||
-- 018:00044000000cc000000cc000006666c000c66000000888800008008000880000
 | 
			
		||||
-- 033:00044000000cc0c0000cc060006666600066c600000888800888008000000080
 | 
			
		||||
-- 034:000440000c0cc0c0060cc0600666666000066600000888000080008008000008
 | 
			
		||||
-- 065:0088880008822880082882800828828008288280082882800882288000888800
 | 
			
		||||
-- 066:0008800000882200008828000088280000882800008828000088220000088000
 | 
			
		||||
-- 067:0008800000288200002882000028820000288200002882000028820000088000
 | 
			
		||||
-- 068:0008800000228800008288000082880000828800008288000022880000088000
 | 
			
		||||
-- 081:00cccc000cc66cc00c6cc6c00c6cc6c00c6cc6c00c6cc6c00cc66cc000cccc00
 | 
			
		||||
-- 082:000cc00000cc660000cc6c0000cc6c0000cc6c0000cc6c0000cc6600000cc000
 | 
			
		||||
-- 083:000cc000006cc600006cc600006cc600006cc600006cc600006cc600000cc000
 | 
			
		||||
-- 084:000cc0000066cc0000c6cc0000c6cc0000c6cc0000c6cc000066cc00000cc000
 | 
			
		||||
-- 097:00eeee000ee99ee00e9ee9e00e9ee9e00e9ee9e00e9ee9e00ee99ee000eeee00
 | 
			
		||||
-- 098:000ee00000ee990000ee9e0000ee9e0000ee9e0000ee9e0000ee9900000ee000
 | 
			
		||||
-- 099:000ee000009ee900009ee900009ee900009ee900009ee900009ee900000ee000
 | 
			
		||||
-- 100:000ee0000099ee0000e9ee0000e9ee0000e9ee0000e9ee000099ee00000ee000
 | 
			
		||||
-- </SPRITES>
 | 
			
		||||
 | 
			
		||||
-- <MAP>
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue