Comments and refactoring
This commit is contained in:
parent
088f554820
commit
5655ebff79
3 changed files with 45 additions and 20 deletions
|
@ -11,3 +11,9 @@ CONF =
|
|||
anim:
|
||||
time: 300
|
||||
func: 'smootherStep'
|
||||
|
||||
layer:
|
||||
background: 0
|
||||
playmats: 1
|
||||
cards: 2
|
||||
focus: 3
|
||||
|
|
|
@ -1,19 +1,26 @@
|
|||
################
|
||||
# component name
|
||||
Crafty.c 'Card',
|
||||
# Mouse interactivity, Tween (animation smooth)
|
||||
required: '2D, Canvas, Mouse, Tween'
|
||||
|
||||
# initialize card
|
||||
init: ->
|
||||
@attr {
|
||||
w: 1 * CONF.card.w
|
||||
h: 1 * CONF.card.h
|
||||
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
|
||||
|
@ -21,12 +28,14 @@ Crafty.c 'Card',
|
|||
}, 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
|
||||
_rotation = @baseRot + 90 * @tapped
|
||||
|
||||
# animate to new rotation, @tapped has "int" value 0 or 1
|
||||
@tween {
|
||||
rotation: _rotation
|
||||
rotation: @baseRot + 90 * @tapped
|
||||
}, CONF.anim.time, CONF.anim.func
|
||||
return
|
||||
|
||||
|
@ -34,39 +43,46 @@ Crafty.c 'Card',
|
|||
@trigger 'Tap', !@tapped
|
||||
return
|
||||
|
||||
# bring card to focus layer and back
|
||||
MouseOver: ->
|
||||
@z_old = @_z
|
||||
@z = 1
|
||||
@z = CONF.layer.focus
|
||||
return
|
||||
|
||||
MouseOut: ->
|
||||
@z = @z_old
|
||||
delete @z_old
|
||||
@z = CONF.layer.cards
|
||||
return
|
||||
|
||||
|
||||
################
|
||||
# component name
|
||||
Crafty.c 'EnemyCard',
|
||||
# is a card
|
||||
required: 'Card'
|
||||
|
||||
# turned by default
|
||||
init: ->
|
||||
@baseRot = 180
|
||||
@rotation = @baseRot
|
||||
return
|
||||
|
||||
|
||||
################
|
||||
# component name
|
||||
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) ->
|
||||
|
|
|
@ -1,22 +1,25 @@
|
|||
# free viewport
|
||||
Crafty.viewport.clampToEntities = false
|
||||
|
||||
Crafty.viewport.follow(Crafty.e('2D, Canvas')
|
||||
.attr {
|
||||
x: 0
|
||||
y: 0
|
||||
w: 1 * CONF.playmat.w
|
||||
h: 2 * CONF.playmat.h
|
||||
}
|
||||
.origin('center')
|
||||
# attach viewport to an entity twice as big as a playmat
|
||||
Crafty.viewport.follow(
|
||||
Crafty.e('2D, Canvas')
|
||||
.attr {
|
||||
w: 1 * CONF.playmat.w
|
||||
h: 2 * CONF.playmat.h
|
||||
}
|
||||
.origin('center')
|
||||
)
|
||||
|
||||
# scale viewport automatically (show that new entity)
|
||||
Crafty.bind 'ViewportResize', ->
|
||||
sX = @viewport._width / 1600
|
||||
sY = @viewport._height / 1800
|
||||
sX = @viewport._width / (1 * CONF.playmat.w)
|
||||
sY = @viewport._height / (2 * CONF.playmat.h)
|
||||
|
||||
@viewport.scale Math.min(sX, sY)
|
||||
return
|
||||
|
||||
# force scale adjustment
|
||||
Crafty.trigger 'ViewportResize'
|
||||
|
||||
# Testing playmat
|
||||
|
|
Reference in a new issue