This repository has been archived on 2024-04-29. You can view files and clone it, but cannot push or open issues or pull requests.
node-fftcg/client/scripts/20-component-Card.coffee
2018-09-30 23:48:24 +02:00

95 lines
1.7 KiB
CoffeeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

################
# Card
################
Crafty.c 'Card',
# Mouse interactivity, Tween (animation smooth)
required: '2D, Canvas, Mouse, Tween'
# initialize card
init: ->
@attr {
z: CONF.layer.cards
w: CONF.card.w
h: CONF.card.h
tapped: false
}
@origin 'center'
return
# on destroy (TODO/example)
remove: ->
Crafty.log 'Card was removed!'
return
# smooth placement
place: (px, py) ->
@tween {
x: px
y: py
}, CONF.anim.time, CONF.anim.func
events:
# Tap or untap, pass true iff you want to tap
Tap: (newState) ->
# store new state in property as boolean value
@tapped = !!newState
# animate to new rotation, @tapped has "int" value 0 or 1
@tween {
rotation: @baseRot + 90 * @tapped
}, CONF.anim.time, CONF.anim.func
return
ToggleTap: ->
@trigger 'Tap', !@tapped
return
# bring card to focus layer and back
MouseOver: ->
@z = CONF.layer.focus
return
MouseOut: ->
@z = CONF.layer.cards
return
################
# EnemyCard
################
Crafty.c 'EnemyCard',
# is a card
required: 'Card'
# turned by default
init: ->
@baseRot = 180
@rotation = @baseRot
return
################
# AllyCard
################
Crafty.c 'AllyCard',
# is a card
required: 'Card, Draggable'
# straight by default
init: ->
@baseRot = 0
@rotation = @baseRot
return
events:
# un-/tappable
DoubleClick: ->
@trigger 'ToggleTap'
return
# snap to 100×100 grid (TODO)
StopDrag: ->
round100 = (v) ->
Math.round(v / 100) * 100
@place round100(@_x), round100(@_y)
return