 class Tank extends MovableBattleFieldObject
   accept: (battle_field_object) ->
     (battle_field_object instanceof Missile) and (battle_field_object.parent is this)
+
   life: 1
   set_life: (@life) ->
-  die: ->
-    @set_life(0)
-  is_dead: ->
-    @life <= 0
-
+  dead: -> @life <= 0
+  power: 1
   level: 1
-  set_level: (@level) ->
-
-  power: 1
-  set_power: (@power) ->
+  max_missile: 1
+  missiles: []
+  update_level: (@level) ->
+    switch @level
+      when 1
+        @power = 1
+        @max_missile = 1
+      when 2
+        @power = 2
+        @max_missile = 2
 
   ship: false
-  set_on_ship: (@ship) ->
+  update_ship: (@ship) ->
 
   guard: false
-  set_on_guard: (@guard) ->
-
-  max_missile: 5
-  missiles: []
+  update_guard: (@guard) ->
+
   fire: () ->
-    missile = @battle_field.add_missile(this)
-    @missiles.push(missile)
-  handle_fire: (command) ->
-    switch(command.type)
-      when "fire"
-        @fire() if _.size(@missiles) < @max_missile
+    if _.size(@missiles) < @max_missile
+      missile = @battle_field.add_missile(this)
+      @missiles.push(missile)
 
   integration: (delta_time) ->
     super(delta_time)
-    @handle_fire(command) for command in @commands
-
-  missile_area: () -> @view.missile_area()
+    @fire() for command in _.select(@commands, (command) -> command.type == "fire")
+
+  missile_born_area: () -> @view.missile_born_area()
+
   delete_missile: (missile) ->
     @missiles = _.without(@missiles, missile)
 


  accept: (other_unit) -> @model.accept(other_unit.model)

  update: () ->

  fight_missile: (missile, destroy_area) -> 0

class MovableMapUnit2D extends MapUnit2D
  origin_x: 'center'

    @area.y2 += offset
    @update_area(@area)

class MapUnit2DForMissile extends MovableMapUnit2D
  current_frames: () -> [{x: 250, y: 330}]
  move: (offset) ->
    can_move = super(offset)
    @attack() unless can_move
    can_move
  attack: () ->
    # if collide with other object, then explode
    destroy_area = @destroy_area()
    # START HERE
    return @model.destroy() if @map.out_of_bound(destroy_area)
    hit_map_units = @map.units_at(destroy_area)
    _.each(hit_map_units, (unit) =>
      @model.energy -= unit.defend(@model, destroy_area)
    )
    @model.destroy() if @model.energy <= 0
  destroy_area: ->
    switch @direction
      when 0
        new MapArea2D(@area.x1 - @default_width/4, @area.y1 - @default_height/4, @area.x2 + @default_width/4, @area.y1)
      when 90
        new MapArea2D(@area.x2, @area.y1 - @default_height/4, @area.x2 + @default_width/4, @area.y2 + @default_height/4)
      when 180
        new MapArea2D(@area.x1 - @default_width/4, @area.y2, @area.x2 + @default_width/4, @area.y2 + @default_height/4)
      when 270
        new MapArea2D(@area.x1 - @default_width/4, @area.y1 - @default_height/4, @area.x1, @area.y2 + @default_height/4)
  destroy_display: () ->
    @display_object.width = 2 * @display_object.width
    @display_object.height = 2 * @display_object.height
