From 32a9d7627ac4b5bf4dca351597380391c7f3f654 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Sun, 30 Jun 2019 19:38:42 +0300 Subject: [PATCH] Create a sprite for the player --- Makefile | 2 +- bundle/main.lua | 88 ++++++++++++++++++++++++-------------- bundle/sprite.png | Bin 0 -> 255 bytes bundle/sprite_crit.png | Bin 0 -> 235 bytes bundle/sprite_damaged.png | Bin 0 -> 212 bytes 5 files changed, 58 insertions(+), 32 deletions(-) create mode 100644 bundle/sprite.png create mode 100644 bundle/sprite_crit.png create mode 100644 bundle/sprite_damaged.png diff --git a/Makefile b/Makefile index 3b14d32..c6f85dd 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ NIX_TARBALL=eitmer-nix.tar.gz all: $(LOVEFILE) $(WIN32_ZIPBALL) $(NIX_TARBALL) -$(LOVEFILE): bundle/main.lua bundle/conf.lua bundle/win_image.png +$(LOVEFILE): bundle/main.lua bundle/conf.lua bundle/win_image.png bundle/sprite.png bundle/sprite_crit.png bundle/sprite_damaged.png cd bundle; zip -9 -r ../$@ * $(WIN32_ZIPBALL): $(LOVEFILE) CC0 README diff --git a/bundle/main.lua b/bundle/main.lua index 531f094..884b292 100644 --- a/bundle/main.lua +++ b/bundle/main.lua @@ -15,8 +15,12 @@ local player_x = nil local player_y = nil local player_direction = nil local hp = nil -local flash_player_white_counter = 0 -local flash_player_red_counter = 0 +local flash_player_crit_counter = 0 +local flash_player_damaged_counter = 0 +local player_sprite = nil +local player_sprite_crit = nil +local player_sprite_damaged = nil +local player_animation_flip = false local slimes = {} @@ -684,23 +688,25 @@ function step(direction) killSlime() hp = hp - 1 if hp == 0 then - flash_player_red_counter = 1 + flash_player_damaged_counter = 1 else - flash_player_red_counter = 0.2 + flash_player_damaged_counter = 0.2 end elseif cavern[player_x][player_y] == tiletypes.slime then killSlime() if math.random() < 0.2 then - flash_player_white_counter = 0.2 + flash_player_crit_counter = 0.2 else hp = hp - 1 if hp == 0 then - flash_player_red_counter = 1 + flash_player_damaged_counter = 1 else - flash_player_red_counter = 0.2 + flash_player_damaged_counter = 0.2 end end end + + player_animation_flip = not player_animation_flip end end @@ -897,28 +903,44 @@ function drawPlayer() local x_offset = (screen_width - cavern.width * scale) / 2 local y_offset = (screen_height - cavern.height * scale) / 2 - local player_head_center_x = (player_x - 1) * scale + x_offset + 0.5 * scale - local player_head_center_y = (player_y - 1) * scale + y_offset + 0.5 * scale - if flash_player_red_counter > 0 then - love.graphics.setColor(1, 0, 0) - elseif flash_player_white_counter > 0 then - love.graphics.setColor(1, 1, 1) - else - love.graphics.setColor(0.6, 0.3, 0.2) - end - love.graphics.circle('fill', player_head_center_x, player_head_center_y, scale/2) + local x = (player_x - 1) * scale + x_offset + local y = (player_y - 1) * scale + y_offset - local player_body_x, player_body_y = getBodyLocation() - local player_body_x = (player_body_x - 1) * scale + x_offset - local player_body_y = (player_body_y - 1) * scale + y_offset - if flash_player_red_counter > 0 then - love.graphics.setColor(1, 0, 0) - elseif flash_player_white_counter > 0 then - love.graphics.setColor(1, 1, 1) + local sprite = nil + if flash_player_damaged_counter > 0 then + sprite = player_sprite_damaged + elseif flash_player_crit_counter > 0 then + sprite = player_sprite_crit else - love.graphics.setColor(0, 0, 1) + sprite = player_sprite + end + + local rotation = nil + if player_direction == directions.up then + rotation = 0 + x = x + scale/2 + y = y + scale + elseif player_direction == directions.left then + rotation = math.pi * 1.5 + x = x + scale + y = y + scale/2 + elseif player_direction == directions.down then + rotation = math.pi + x = x + scale/2 + elseif player_direction == directions.right then + rotation = math.pi * 0.5 + y = y + scale/2 + else + error("Player facing a direction without usable sprite") + end + + -- Our sprite image is drawn assuming 16 pixels per tile + love.graphics.setColor(1, 1, 1) + if not player_animation_flip then + love.graphics.draw(sprite, x, y, rotation, scale/16, scale/16, scale/2, scale) + else + love.graphics.draw(sprite, x, y, rotation, -scale/16, scale/16, scale/2, scale) end - love.graphics.rectangle('fill', player_body_x, player_body_y, scale, scale) end function drawHP() @@ -1055,6 +1077,10 @@ function love.load() newGame() win_image = love.graphics.newImage("win_image.png") + + player_sprite = love.graphics.newImage("sprite.png") + player_sprite_crit = love.graphics.newImage("sprite_crit.png") + player_sprite_damaged = love.graphics.newImage("sprite_damaged.png") end function love.update(dt) @@ -1065,12 +1091,12 @@ function love.update(dt) end end - if flash_player_white_counter > 0 then - flash_player_white_counter = flash_player_white_counter - dt + if flash_player_crit_counter > 0 then + flash_player_crit_counter = flash_player_crit_counter - dt end - if flash_player_red_counter > 0 then - flash_player_red_counter = flash_player_red_counter - dt - if hp == 0 and flash_player_red_counter <= 0 then + if flash_player_damaged_counter > 0 then + flash_player_damaged_counter = flash_player_damaged_counter - dt + if hp == 0 and flash_player_damaged_counter <= 0 and game_mode ~= gamemodes.won then game_mode = gamemodes.lost end end diff --git a/bundle/sprite.png b/bundle/sprite.png new file mode 100644 index 0000000000000000000000000000000000000000..a20408887bb97ef44d21330d498c1dda85c4938d GIT binary patch literal 255 zcmeAS@N?(olHy`uVBq!ia0vp^0zj<5!2~3yyw0Buq<8{+LR^9LOdn$ghX0c$O*+|h zn-R!g=jq}YVsUzH$U(st1CAI8>rK4BKTJ>hw3+|(j@6v<=PXoIxB}x66PUSY3*6bJ zYs}zwL8GKry5K0on&0uREDZUl*BF1fxl37QM|tr1Wu^aHZ(P@%s}`njxgN@xNA DWj}%WVsZLth^J7q0ng?y|Ltcp`yBJ#bLrd_b(w%gM_DfEg^CKE+F)?&9{ZYd zC8mVsOICg3{#~;z-^Ae7N`{Dax(%x@ZIjo#dp5J<^Vhv8zhn3r)D(B*TrA1F_&aHu zm(g#|vl=s6E^xi`XL!v%`AUpuPfbmct-+aCd;WCEJ8jQB3jZ><9v1%?`MR$yGIx8h i;k}EY-)BckU1#-P5UyOms^tsNl?ut literal 0 HcmV?d00001 diff --git a/bundle/sprite_damaged.png b/bundle/sprite_damaged.png new file mode 100644 index 0000000000000000000000000000000000000000..5a86db12db32acabee8118e4f678b9e2b89a062a GIT binary patch literal 212 zcmeAS@N?(olHy`uVBq!ia0vp^0zj<5!2~3yyw0Buq<8{+LR^9LOdn$ghX0c$O*+|h zn-R!w_H=O!u{eD+#8c>i0SD9n|LIjPeU^K!IK6aUt3pOc5hsH{hFJ8M^ah^$x}NX( zDh<+%TavvP-Z0I{w%MhUd}8D5-N%3V=uY^#<3#dB)xP3{*fYPF*eweL?)WoUHgH{+ zIOi_qBXT~*+F@(HQk3v>_LS-Vl6#LGv}IYfe!JCg^Qj^0xmRk)>RUXYB>{8}gQu&X J%Q~loCIEWsOb7q~ literal 0 HcmV?d00001