Add deposit concept

This commit is contained in:
2019-10-12 18:00:53 +03:00
parent 658d3b7288
commit d3fe2ff23b
2 changed files with 78 additions and 0 deletions

40
src/game/deposit.cr Normal file
View File

@ -0,0 +1,40 @@
module Game
class Deposit
class Span
def initialize(@res : Resources::Type, @cap : Capacity)
end
getter res
getter cap
end
@cur : Capacity = 0
def initialize(@res : Resources::Type, @cap : Capacity)
@cur = @cap
end
def initialize(@res : Resources::Type, @cap : Capacity, @cur : Capacity)
end
getter res
getter cap
getter cur
def inc(span : Span)
check_res span.res
@cur = Math.min(@cap, @cur + span.cap)
end
def dec(span : Span)
check_res span.res
@cur = Math.max(0, @cur - span.cap)
end
private def check_res(other_res : Resources::Type)
if @res != other_res
raise ResourceMismatch.new
end
end
end
end