--'******************************************************
--'*	Биндер объекта моста .
--'******************************************************

function bind(obj)
	obj:bind_object(bridge_binder(obj))
	db.storage[obj:id()] = { }
end

class "bridge_binder" (object_binder)
function bridge_binder:__init(obj) super(obj)
	self.bridge = obj
	local se_koleso = alife():story_object(516)
	self.se_koleso_id = se_koleso and se_koleso.id
--	printf("koleso = [%s]", tostring(self.se_koleso_id))
end
function bridge_binder:net_spawn(server_object)
	if not object_binder.net_spawn(self, server_object) then
		return false
	end
	db.add_bridge(self.bridge, self)
	printf("callback!!!!bridge!!!!")
	self.object:set_callback(callback.script_animation, self.animation_end_callback, self)
	self.bridge:get_physics_object():stop_anim()
	if self.koleso ~= nil then
		self.koleso:get_physics_object():stop_anim()
	end
	db.add_bridge(self.bridge, self)
	self.idle_snd = sound_object("device\\bridge\\bridge_move")
	self.down_snd = sound_object("device\\bridge\\bridge_down")
	self.start_snd = sound_object("device\\bridge\\bridge_start")
	self.stop_snd = sound_object("device\\bridge\\bridge_stop")
	self.idle = 50000
	return true
end

function bridge_binder:net_destroy()
	self.idle_snd:stop()
	self.down_snd:stop()
	self.start_snd:stop()
	self.stop_snd:stop()
	object_binder.net_destroy(self)
end


function bridge_binder:anim_forward()
	self.idle_snd:stop()
	self.bridge:get_physics_object():stop_anim()
	if self.koleso ~= nil then
		self.koleso:get_physics_object():run_anim_forward()
	end
	self.idle_end = self.idle + game.time()
	self.start_snd:play_at_pos( self.object,  self.object:position(), 0, sound_object.s3d)
	self.idle_snd:play_at_pos( self.object,  self.object:position(), 5, sound_object.s3d + sound_object.looped)
	self.bridge_idle = true
	self.bridge_anim_fwd = true
end

function bridge_binder:anim_backward()
	self.idle_snd:stop()
	self.bridge:get_physics_object():stop_anim()
	if self.koleso ~= nil then
		self.koleso:get_physics_object():run_anim_back()
	end
	self.idle_end = self.idle + game.time()
	self.start_snd:play_at_pos( self.object,  self.object:position(), 0, sound_object.s3d)
	self.idle_snd:play_at_pos( self.object,  self.object:position(), 5, sound_object.s3d + sound_object.looped)
	self.bridge_idle = true
	self.bridge_anim_fwd = false
end

function bridge_binder:anim_stop()
	self.bridge:get_physics_object():stop_anim()
	if self.koleso ~= nil then
		self.koleso:get_physics_object():stop_anim()
	end
	self.bridge_idle = false
	self.stop_snd:play_at_pos( self.object,  self.object:position(), 0, sound_object.s3d)
end


function bridge_binder:update(delta)
	object_binder.update(self, delta)
	if self.se_koleso_id ~= nil and (self.koleso == nil) and (db.storage[self.se_koleso_id] ~= nil) and (db.storage[self.se_koleso_id].object ~= nil) then
		self.koleso = db.storage[self.se_koleso_id].object
		printf("koleso est'")
		self.koleso:get_physics_object():stop_anim()
	end
    if self.anim_time then
        self.bridge:get_physics_object():anim_time_set(self.anim_time)
        self.anim_time = nil
    end
	if self.bridge_idle == true then
		if self.idle_end <= game.time() then
			if self.bridge_anim_fwd == true then
				self.bridge:get_physics_object():run_anim_forward()
			else
				self.bridge:get_physics_object():run_anim_back()
			end
		end
	else
		self.object:get_physics_object():stop_anim()
		if self.idle_snd ~= nil then
			self.idle_snd:stop()
		end
	end
end

function bridge_binder:animation_end_callback( is_end )
	printf("animation_callback!!!!!!bridge!!!!")
	if is_end then
		db.actor:give_info_portion("red_bridge_down_done")
		self.down_snd:play_at_pos( self.object,  self.object:position(), 0, sound_object.s3d)
		self.bridge_idle = false
		if self.koleso ~= nil then
			self.koleso:get_physics_object():stop_anim()
		end
	else
		db.actor:give_info_portion("red_bridge_actor_out")
		self.down_snd:play_at_pos( self.object,  self.object:position(), 0, sound_object.s3d)
		self.bridge_idle = false
		if self.koleso ~= nil then
			self.koleso:get_physics_object():stop_anim()
		end
	end
end
-- Standart function for save
function bridge_binder:net_save_relevant()
	return true
end
-- Saving crow
function bridge_binder:save(packet)
	set_save_marker(packet, "save", false, "bridge_binder")
	object_binder.save(self, packet)
	xr_logic.save_obj(self.object, packet)

	packet:w_bool(self.bridge_idle==true)
	packet:w_bool(self.bridge_anim_fwd==true)
	if self.idle_end then
		packet:w_u32(self.idle_end)
	else
		packet:w_u32(0)
	end
	if self.object and self.object:get_physics_object() and self.object:get_physics_object():anim_time_get() then
		packet:w_float(self.object:get_physics_object():anim_time_get())
	else
		packet:w_float(0)
	end
	set_save_marker(packet, "save", true, "bridge_binder")
end
-- Loading crow
function bridge_binder:load(packet)
	set_save_marker(packet, "load", false, "bridge_binder")
	object_binder.load(self, packet)
	xr_logic.load_obj(self.object, packet)
	self.bridge_idle = packet:r_bool()
	self.bridge_anim_fwd = packet:r_bool()
	self.idle_end = packet:r_u32()
	self.anim_time = packet:r_float()
	set_save_marker(packet, "load", true, "bridge_binder")
end