script me a lua script for a gun
Sure! Here's a simple Lua script for a gun:
-- Define gun properties
local gun = {
damage = 10,
ammo = 30,
maxAmmo = 30,
reloadTime = 2,
fireRate = 0.1,
cooldown = 0
}
-- Function to fire the gun
function gun:fire()
if self.ammo > 0 and self.cooldown <= 0 then
print("Bang! Damage:", self.damage)
self.ammo = self.ammo - 1
self.cooldown = self.fireRate
elseif self.ammo == 0 then
print("Out of ammo! Reloading...")
self:reload()
else
print("Gun is still on cooldown!")
end
end
-- Function to reload the gun
function gun:reload()
print("Reloading...")
-- Simulating reload time
os.execute("sleep " .. self.reloadTime)
self.ammo = self.maxAmmo
print("Reload complete! Ammo:", self.ammo)
end
-- Function to update the cooldown
function gun:update(dt)
self.cooldown = self.cooldown - dt
if self.cooldown < 0 then
self.cooldown = 0
end
end
-- Usage example
gun:fire() -- Bang! Damage: 10
gun:fire() -- Bang! Damage: 10
gun:fire() -- Bang! Damage: 10
gun:fire() -- Out of ammo! Reloading...
-- Reloading...
-- Reload complete! Ammo: 30
gun:fire() -- Bang! Damage: 10
This script defines a gun object with properties like damage, ammo, reload time, and fire rate. It includes functions to fire the gun, reload it, and update the cooldown. When you call the fire() function, it will check if there is ammo, if the gun is not on cooldown, and then simulate firing the gun. If there's no ammo, it will reload the gun
原文地址: https://www.cveoy.top/t/topic/ir1J 著作权归作者所有。请勿转载和采集!