Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Native Lua #3

Open
RobLoach opened this issue Sep 19, 2019 · 8 comments
Open

Native Lua #3

RobLoach opened this issue Sep 19, 2019 · 8 comments

Comments

@RobLoach
Copy link
Owner

No description provided.

@StinkerB06
Copy link

@ghost
Copy link

ghost commented Nov 19, 2020

I am working on this lol...

@ghost
Copy link

ghost commented Nov 19, 2020

Hey @RobLoach, Here is Lua port i did...

-- author: Rabia Alhaffar
-- desc: Benchmarking tool to see how many bunnies can fly around the screen, using Lua.
-- input: gamepad
-- script: lua
-- version: 1.0.1

screenWidth = 240
screenHeight = 136
toolbarHeight = 6
t = 0

Bunny = {}

function Bunny:new()
  setmetatable(Bunny, self)
  self.__index = self
	self.width = 26
	self.height = 32
	self.x = math.random(0, screenWidth - self.width)
	self.y = math.random(0, screenHeight - self.height)
	self.speedX = math.random(-100, 100) / 60
	self.speedY = math.random(-100, 100) / 60
	self.sprite = 1
	return Bunny
end

function Bunny:draw()
  spr(self.sprite, self.x, self.y, 1, 1, 0, 0, 4, 4)
end

function Bunny:update()
  self.x = self.x + self.speedX
	self.y = self.x + self.speedY
		
	if (self.x + self.width > screenWidth) then
		self.x = screenWidth - self.width
		self.speedX = self.speedX * -1
  end
	if (self.x < 0) then
		self.x = 0
		self.speedX = self.speedX * -1
	end
	if (self.y + self.height > screenHeight) then
		self.y = screenHeight - self.height
		self.speedY = self.speedY * -1
	end
	if (self.y < toolbarHeight) then
		self.y = toolbarHeight
		self.speedY = self.speedY * -1
	end
end

FPS = {}

function FPS:new()
  setmetatable(FPS, self)
		self.__index = self
		self.value = 0
		self.frames = 0
		self.lastTime = 0
		return FPS
end

function FPS:getValue()
  if (time() - self.lastTime <= 1000) then
		  self.frames = self.frames + 1
		else
		  self.value = self.frames
				self.frames = 0
				self.lastTime = time()
		end
		return self.value
end

fps = FPS:new()
bunnies = {}
table.insert(bunnies, Bunny:new())

function TIC()
  -- music
		if t == 0 then
		  music(0)
		end
		if t == 6*64*2.375 then
		  music(1)
		end
		t = t + 1
		
		-- Input
		if btn(0) then
		  for i = 1, 5 do
				  table.insert(bunnies, Bunny:new())
				end
		end
		if btn(1) then
		  for i = 1, 5 do
				  table.remove(bunnies, i)
				end
		end
		
		-- Update
		for i, item in pairs(bunnies) do
		  item:update()
		end
		
		-- Draw
		cls(15)
		for i, item in pairs(bunnies) do
		  item:draw()
		end
		
		rect(0, 0, screenWidth, toolbarHeight, 0)
	 print("Bunnies: " .. #bunnies, 1, 0, 11, false, 1, false)
	 print("FPS: " .. fps:getValue(), screenWidth / 2, 0, 11, false, 1, false)
end

But it's garbled sorry...

@RobLoach
Copy link
Owner Author

This is great, Rabios! Thanks so much! Would you like me to make the commit? If you push up a Pull Request, you'll end up getting author credits 😉

@ghost
Copy link

ghost commented Nov 19, 2020

@RobLoach Unfortunately i'm new to TIC-80 so i got stuck with injecting assets to the cartridge (Very sorry)

I know it's shame to ask for but mind if you made cartridge, Oh i should note that there is possibility where code maybe need to edit somewhere (I used this way of OOP in Lua which is metatables...)

But i included my name in code, So even if you commit at least keep my name on code and add your one if you don't mind...

Fun info: My name is Rabia lol...

@ghost
Copy link

ghost commented Nov 19, 2020

About fennel, I maybe need some time to learn about it, But maybe will be in this week!

@RobLoach
Copy link
Owner Author

Added it... There's something wrong with integer/float types.

@ghost
Copy link

ghost commented Nov 22, 2020

Hm...Isn't when downgrading bunnies count and reaches smaller than 0, Right...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants