Create a sprite for the player

This commit is contained in:
Juhani Krekelä 2019-06-30 19:38:42 +03:00
parent f5ba1bdb17
commit 32a9d7627a
5 changed files with 58 additions and 32 deletions

View File

@ -6,7 +6,7 @@ NIX_TARBALL=eitmer-nix.tar.gz
all: $(LOVEFILE) $(WIN32_ZIPBALL) $(NIX_TARBALL) 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 ../$@ * cd bundle; zip -9 -r ../$@ *
$(WIN32_ZIPBALL): $(LOVEFILE) CC0 README $(WIN32_ZIPBALL): $(LOVEFILE) CC0 README

View File

@ -15,8 +15,12 @@ local player_x = nil
local player_y = nil local player_y = nil
local player_direction = nil local player_direction = nil
local hp = nil local hp = nil
local flash_player_white_counter = 0 local flash_player_crit_counter = 0
local flash_player_red_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 = {} local slimes = {}
@ -684,23 +688,25 @@ function step(direction)
killSlime() killSlime()
hp = hp - 1 hp = hp - 1
if hp == 0 then if hp == 0 then
flash_player_red_counter = 1 flash_player_damaged_counter = 1
else else
flash_player_red_counter = 0.2 flash_player_damaged_counter = 0.2
end end
elseif cavern[player_x][player_y] == tiletypes.slime then elseif cavern[player_x][player_y] == tiletypes.slime then
killSlime() killSlime()
if math.random() < 0.2 then if math.random() < 0.2 then
flash_player_white_counter = 0.2 flash_player_crit_counter = 0.2
else else
hp = hp - 1 hp = hp - 1
if hp == 0 then if hp == 0 then
flash_player_red_counter = 1 flash_player_damaged_counter = 1
else else
flash_player_red_counter = 0.2 flash_player_damaged_counter = 0.2
end end
end end
end end
player_animation_flip = not player_animation_flip
end end
end end
@ -897,28 +903,44 @@ function drawPlayer()
local x_offset = (screen_width - cavern.width * scale) / 2 local x_offset = (screen_width - cavern.width * scale) / 2
local y_offset = (screen_height - cavern.height * 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 x = (player_x - 1) * scale + x_offset
local player_head_center_y = (player_y - 1) * scale + y_offset + 0.5 * scale local y = (player_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)
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 player_body_x, player_body_y = getBodyLocation() local sprite = nil
local player_body_x = (player_body_x - 1) * scale + x_offset if flash_player_damaged_counter > 0 then
local player_body_y = (player_body_y - 1) * scale + y_offset sprite = player_sprite_damaged
if flash_player_red_counter > 0 then elseif flash_player_crit_counter > 0 then
love.graphics.setColor(1, 0, 0) sprite = player_sprite_crit
elseif flash_player_white_counter > 0 then
love.graphics.setColor(1, 1, 1)
else 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 end
love.graphics.rectangle('fill', player_body_x, player_body_y, scale, scale)
end end
function drawHP() function drawHP()
@ -1055,6 +1077,10 @@ function love.load()
newGame() newGame()
win_image = love.graphics.newImage("win_image.png") 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 end
function love.update(dt) function love.update(dt)
@ -1065,12 +1091,12 @@ function love.update(dt)
end end
end end
if flash_player_white_counter > 0 then if flash_player_crit_counter > 0 then
flash_player_white_counter = flash_player_white_counter - dt flash_player_crit_counter = flash_player_crit_counter - dt
end end
if flash_player_red_counter > 0 then if flash_player_damaged_counter > 0 then
flash_player_red_counter = flash_player_red_counter - dt flash_player_damaged_counter = flash_player_damaged_counter - dt
if hp == 0 and flash_player_red_counter <= 0 then if hp == 0 and flash_player_damaged_counter <= 0 and game_mode ~= gamemodes.won then
game_mode = gamemodes.lost game_mode = gamemodes.lost
end end
end end

BIN
bundle/sprite.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 255 B

BIN
bundle/sprite_crit.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 235 B

BIN
bundle/sprite_damaged.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 212 B