-- object_anim_control - Animation Control behavior -- (component of behavior_lib_1) -- Template attachment: any -- Created by: Patrick Ferland -- Created on: 10/29/2008 -- Revision History -- 10/29/2008 P.Ferland - New script -- 11/10/2008 P.Ferland - Removed Debug and added animate-on-use support -- Listens for these triggers: -- enter -- param_changed -- anim_pingpong_complete (from self) -- Broadcasts these triggers: -- no broadcast -- anim_pingpong_complete (to self) -- Constants ANIM_CONTROL_SCRIPT = '12071:51' DIRECTIONS = { forward = 0, backward = 1, } -- Script properties Define Properties() script_description = "Animation Control" script_long_description = "This behavior allows you to control the animations applied to objects." anim_style = 'loop' PersistProperty('anim_style') ExposeProperty('anim_style', 'How does the object animate normally?', 'list') anim_style_list = {'no_animation', 'loop', 'play_once', 'ping_pong'} anim_dir = 'forward' PersistProperty('anim_dir') ExposeProperty('anim_dir', 'In what direction does the object animate normally?', 'list') anim_dir_list = {'forward', 'backward'} anim_on_use = 'no_change' PersistProperty('anim_on_use') ExposeProperty('anim_on_use', 'How should the object animate when used by a player?', 'list') anim_on_use_list = {'no_change', 'toggle_loop', 'ping_pong', 'play_once'} -- "In-use" state info anim_active_state = 0 anim_pingpong_state = 0 anim_pingpong_callback = 0 end -- Commands -- no commands -- Triggers -- Animate on use, based on current settings Trigger use(user) if (self.anim_on_use == 'toggle_loop') then play_anim_use_toggle(self) elseif (self.anim_on_use == 'play_once') then play_anim_one_shot(self) elseif (self.anim_on_use == 'ping_pong') then play_anim_ping_pong(self) -- otherwise, assume "no change" and do nothing! end end -- System trigger: Fired when object enters any place Trigger enter() play_anim(self) end Trigger anim_pingpong_complete(caller) if (caller.id == self.id) then if (self.anim_pingpong_callback ~= 0) then self.anim_pingpong_callback = 0 end -- Toggle animation state (0 = first frame, 1 = last frame) self.anim_pingpong_state = 1 - self.anim_pingpong_state end end -- OX trigger: Replay animation when this object is configured Trigger param_changed(caller, user_id, script_id, value) if (script_id == ANIM_CONTROL_SCRIPT) then if (self.anim_style ~= 'no_animation') then self.anim_active_state = 1 -- animating object, start as animating else self.anim_active_state = 0 -- non-animating object, start dormant end if (self.anim_on_use == 'ping-pong') then if (self.anim_dir == 'forward') then self.anim_pingpong_state = 0 else self.anim_pingpong_state = 1 end end -- Set animation to the correct state play_anim(self) end end -- Local functions -- Toggle on/off function play_anim_use_toggle(self) -- Toggle animation state (0 = idle, 1 = animating) self.anim_active_state = 1 - self.anim_active_state if (self.anim_active_state == 1) then -- Active state of 1, so animate play_anim(self) else -- self.anim_active_state = 0, so go idle local sprite_id, frames, anim_time = get_sprite_info(self) if (self.anim_dir == 'forward') then self.spriteFrame = 1 else self.spriteFrame = frames end end end -- One-shot function play_anim_one_shot(self) local sprite_id, frames, anim_time = get_sprite_info(self) local dir = DIRECTIONS[self.anim_dir] PlaySpriteAnim(self, 1, frames, dir, 'play_once', anim_time) end -- Pingpong function play_anim_ping_pong(self) if (self.anim_pingpong_callback ~= 0) then return nil end local sprite_id, frames, anim_time = get_sprite_info(self) local dir = self.anim_pingpong_state PlaySpriteAnim(self, 1, frames, dir, 'play_once', anim_time) self.anim_pingpong_callback = SendTo(self, 'anim_pingpong_complete', anim_time, self) end -- Default animate the object based on the current settings function play_anim(self) local sprite_id, frames, anim_time = get_sprite_info(self) local dir = DIRECTIONS[self.anim_dir] if ( (self.anim_style ~= 'no_animation') or (self.anim_active_state == 1) ) then -- animating sprite PlaySpriteAnim(self, 1, frames, dir, self.anim_style, anim_time) else -- non-animating object if (dir == 0) then self.spriteFrame = 1 else -- dir = 1 (backwards) self.spriteFrame = frames end end end -- Get sprite ID, number of frames, and total animation time for current sprite function get_sprite_info(self) local sprite_id = self.spriteId local sprite = stylesheet.sprites[sprite_id] local frames = sprite.frames local anim_time = sprite.frames * sprite.interval return sprite_id, frames, anim_time end