Comments and refactoring

This commit is contained in:
Jörn-Michael Miehe 2018-09-30 23:23:43 +02:00
parent 088f554820
commit 5655ebff79
3 changed files with 45 additions and 20 deletions

View file

@ -11,3 +11,9 @@ CONF =
anim: anim:
time: 300 time: 300
func: 'smootherStep' func: 'smootherStep'
layer:
background: 0
playmats: 1
cards: 2
focus: 3

View file

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

View file

@ -1,22 +1,25 @@
# free viewport
Crafty.viewport.clampToEntities = false Crafty.viewport.clampToEntities = false
Crafty.viewport.follow(Crafty.e('2D, Canvas') # attach viewport to an entity twice as big as a playmat
.attr { Crafty.viewport.follow(
x: 0 Crafty.e('2D, Canvas')
y: 0 .attr {
w: 1 * CONF.playmat.w w: 1 * CONF.playmat.w
h: 2 * CONF.playmat.h h: 2 * CONF.playmat.h
} }
.origin('center') .origin('center')
) )
# scale viewport automatically (show that new entity)
Crafty.bind 'ViewportResize', -> Crafty.bind 'ViewportResize', ->
sX = @viewport._width / 1600 sX = @viewport._width / (1 * CONF.playmat.w)
sY = @viewport._height / 1800 sY = @viewport._height / (2 * CONF.playmat.h)
@viewport.scale Math.min(sX, sY) @viewport.scale Math.min(sX, sY)
return return
# force scale adjustment
Crafty.trigger 'ViewportResize' Crafty.trigger 'ViewportResize'
# Testing playmat # Testing playmat