Skip to content

Commit

Permalink
add animation
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesirius committed Jan 1, 2016
1 parent b3fcf76 commit b1c14df
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Money.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Money = {balance = 0}
Money = {balance = 100}

function Money:new(o)
o = o or {}
Expand Down
128 changes: 128 additions & 0 deletions anim.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
local anim = {
_VERSION = 'anim8 v2.2.0',
_DESCRIPTION = 'An animation library for LÖVE',
_URL = 'https://github.com/kikito/anim8',
_LICENSE = [[
MIT LICENSE
Copyright (c) 2011 Enrique García Cota
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]
}


local Animation = {}
local Animationmt = { __index = Animation }


local function parseDurations(durations, frameCount)
local result = {}
if type(durations) == 'number' then
for i=1,frameCount do result[i] = durations end
else
local min, max, step
for key,duration in pairs(durations) do
assert(type(duration) == 'number', "The value [" .. tostring(duration) .. "] should be a number")
min, max, step = parseInterval(key)
for i = min,max,step do result[i] = duration end
end
end

if #result < frameCount then
error("The durations table has length of " .. tostring(#result) .. ", but it should be >= " .. tostring(frameCount))
end

return result

end

local function parseIntervals(durations)
local result, time = {0},0
for i=1,#durations do
time = time + durations[i]
result[i+1] = time
end
return result, time
end



local function newAnimation(durations, onLoop, ...)
local images= {...}
local td = type(durations);
if (td ~= 'number' or durations <= 0) and td ~= 'table' then
error("durations must be a positive number. Was " .. tostring(durations) )
end
onLoop = onLoop or nop
durations = parseDurations(durations, 4)
local intervals, totalDuration = parseIntervals(durations)
return setmetatable({
frames = images,
durations = durations,
intervals = intervals,
totalDuration = totalDuration,
onLoop = onLoop,
timer = 0,
timer2 = 0,
position = 1,
status = "playing",
flippedH = false,
flippedV = false,
x = love.math.random(0, 800),
y = 584,
dx = 1

},
Animationmt
)

end


function Animation:draw()
local image = self.frames[self.position]
love.graphics.draw(image, self.x, self.y, r, sx, sy, ox, oy)
end


function Animation:update(dt)
if self.status ~= "playing" then return end
self.timer = self.timer + dt
self.timer2 = self.timer2 +dt

if self.timer > 1 then
self.x = self.x + self.dx
if self.position == 1 then
self.position = 2
else
self.position = 1
end
self.timer = 0
else
end

if self.timer2 > 3 then
self.dx = love.math.random(-7, 7)
self.timer2 = 0
else
end
end


anim.newAnimation = newAnimation

return anim
2 changes: 1 addition & 1 deletion conf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
function love.conf(t)
t.title = "Korean Junkyard Simulator"
t.author = ""
t.version = "0.9.1"
t.version = "0.10.0"
t.window.width = 800
t.window.height = 600
end
Binary file modified kjs.love
Binary file not shown.
18 changes: 16 additions & 2 deletions main.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "TEsound"
require "Money"
local anim = require"anim"

aa=love.filesystem.createDirectory("../KJS")
contents, size = love.filesystem.read("save.sav",100)
Expand All @@ -21,6 +22,9 @@ ocean=0
oceanPrice=100000000
space=0
spacePrice=1800000000

local grandma_anims = {}

function love.load()
TEsound.playLooping("resource/bgm/bgm.mp3", "bgm")
bgmOn = love.graphics.newImage("resource/image/bgm_on.png")
Expand All @@ -30,6 +34,8 @@ function love.load()
btn_save = love.graphics.newImage("resource/image/save.png")
btn_reset = love.graphics.newImage("resource/image/reset.png")
moneyImg=love.graphics.newImage("/resource/image/money_image.png")
grandma1=love.graphics.newImage("/resource/image/grandma1.png")
grandma2=love.graphics.newImage("/resource/image/grandma2.png")
love.graphics.setBackgroundColor(0,0,0)
love.graphics.setNewFont("/resource/font/RixHeadB.ttf",20)

Expand Down Expand Up @@ -63,11 +69,15 @@ function love.draw()
love.graphics.print(""..math.floor(oceanPrice),400,345)
love.graphics.print("우주 쓰레기 수거: "..space,600,320)
love.graphics.print(""..math.floor(spacePrice),600,345)


for key,value in pairs(grandma_anims) do
grandma_anims[key]:draw(400,584)
end
end

function love.mousepressed(x, y, button, istouch)
if button == 1 then
money:set(money:get()+100)
if x >= 750 and x <= 790 and y >= 10 and y <= 50 and toggleBGM then
toggleBGM=false
TEsound.stop("bgm")
Expand All @@ -88,7 +98,8 @@ function love.mousepressed(x, y, button, istouch)
earning=earning+10
grandmotherPrice=grandmotherPrice*1.1
grandmother=grandmother+1
love.graphics.draw(gradma1,750,10)
grandma_anim = anim.newAnimation(0.1,true,grandma1,grandma2)
table.insert(grandma_anims,grandma_anim)
end

if x>=400 and x<=570 and y>=190 and y<=235 and money:get()>=usedcollectorPrice then
Expand Down Expand Up @@ -151,4 +162,7 @@ end
function love.update(dt)
money:set(money:get()+earning*dt)
TEsound.cleanup()
for key,value in pairs(grandma_anims) do
grandma_anims[key]:update(dt)
end
end

0 comments on commit b1c14df

Please sign in to comment.