Browse Source

fancy gun counter

pixelbath 8 months ago
parent
commit
da72629178
1 changed files with 82 additions and 3 deletions
  1. 82 3
      einhander/einhander.lua

+ 82 - 3
einhander/einhander.lua

@@ -51,8 +51,6 @@ end
 particlePointer=1 -- points at the next particle to be spawned
 
 weaponBgSize=16--12
-weaponBgTimer=0
-weaponAmmo=3000
 
 p_bullets = {}
 p_big_bullets = {}
@@ -71,6 +69,81 @@ math.randomseed(seed)
 drk={[0]=0,0,1,2,3,6,7,15,0,8,9,10,13,14,15,0}
 
 
+-- entity interface:
+-- props: x, y, vx, vy, speed, t
+-- methods: update, draw, setPosition
+function createEntity(x, y, speed, t)
+    local tm = t or 0
+    local spd = speed or 0
+    return { x=x, y=y, vx=0, vy=0, speed=spd, t=tm }
+end
+
+local gunCounter = createEntity(50, 60)
+gunCounter.value = 3000
+gunCounter.p_val = 3000
+gunCounter.t_def = 5
+gunCounter.max_val = 3000
+gunCounter.swaps = {}
+
+function gunCounter:setValue(val)
+    self.value = val
+    self.p_val = val
+    self.max_val = val
+end
+
+function gunCounter:setPosition(x, y)
+    self.x = x
+    self.y = y
+end
+
+function gunCounter:dec()
+    if self.value > 0 then
+        self.p_val = self.value
+        self.value = self.value - 1  -- decrement
+        self.t = self.t_def          -- reset timer
+        
+        -- grab string differences and store indices in .swaps
+        local prev = string.format("%04d", self.p_val)
+        local cur = string.format("%04d", self.value)
+
+        self.swaps = {}
+        for i=1,4 do
+            if prev:sub(i,i) ~= cur:sub(i,i) then
+                self.swaps[i] = true
+            end
+        end
+    end
+end
+
+function gunCounter:update()
+end
+
+function gunCounter:draw()
+    self.t = self.t - 1
+    if self.t == 0 then
+        self.swaps = {}
+        self.p_val = self.value
+    end
+    local prev = string.format("%04d", self.p_val)
+    local cur = string.format("%04d", self.value)
+
+    local color = 12
+    if self.value / self.max_val < 0.1 then color = 4 end
+
+    -- clip this area
+    clip(self.x - 1, self.y - 2, 18, 10)
+    for i = 1,4 do
+        local numY = self.y
+        if self.t > 0 and self.swaps[i] then
+            numY = self.y - (self.t_def - self.t)
+            print(prev:sub(i, i), self.x + ((i-1)*4), numY + 6, color, true, 1, true)
+        end
+
+        print(cur:sub(i, i), self.x + ((i-1)*4), numY, color, true, 1, true)
+    end
+    clip()
+end
+
 function drawParallelogram(x, y, w, h, skewpx, color)
     tri(x+skewpx, y, x+w, y+h, x, y+h, color)
     tri(x+skewpx, y, x+w, y+h, x+w+skewpx, y, color)
@@ -292,9 +365,11 @@ function drawUI()
         local guntext = gunnames[player.gun_held][3]
         spr(lettersprite, 14, 118, 0, 1)
         print(guntext, 24, 122, 12, true, 1, true)
-        print(weaponAmmo, 12, 129, 12, true, 1, true)
+        print(gunCounter.value, 12, 129, 12, true, 1, true)
     end
 
+    gunCounter:draw()
+
     -- multiplier
     -- TODO: need to do multipliers over 10, so the X needs to move left when that happens
     spr(340, 164, 114, 0)
@@ -363,6 +438,8 @@ function update()
 	player.x = player.x + player.vx
 	player.y = player.y + player.vy
 
+    gunCounter:setPosition(player.x + 8, player.y + 22)
+
 	if player.x < 5 then player.x = 5 end
 	if player.x > 220 then player.x = 220 end
 	if player.y < 5 then player.y = 5 end
@@ -382,6 +459,7 @@ function update()
     if btn(5) then
         if player.gun_held == 1 then
             player.is_firing = 1
+            gunCounter:dec()
         end
         if player.gun_held == 2 then
             -- controlled by cooldown (or semi-auto)
@@ -390,6 +468,7 @@ function update()
             if player.big_gun_cooldown == 0 then
                 player.big_gun_cooldown = 30
                 player.is_firing = 1
+                gunCounter:dec()
             else
                 player.big_gun_cooldown = player.big_gun_cooldown - 1
                 player.is_firing = 0