how to implement sequence of animations in Ursina

240 Views Asked by At

I'm trying to implement a complex animation in my cow character in Ursina (i.e. composed by more than one animation). What I don't manage to achieve is to trigger a sequence of animations ecoded in states of the animator. In fact, I don't even know if this is the right strategy to pursue (i.e. is the animator needed? should it be embedded in the class?). If I use the Sequence (Func... Func... ) command, as in the example below, the complex animaton does not work. I tried by writing class methods that change the status of the animator, without success, it doesn't work. Also, I suspect that the use of animator in my script is not straightforward (as it is outside the scope of the class).

from ursina import *

# create a window
app = Ursina()

window.borderless = False
window.windowed_size = 0.3

window.update_aspect_ratio()

# camera.orthographic = True
# camera.fov = 15

# FENCE
background = Entity(model='quad', texture='assets/farm',scale=15, z=1, collider='box',  tag='background')
for i in range(4):
    fence1 = Entity(model='quad', texture='assets/fence1', scale=1, position=(-i, -i*0.42), collider='box', tag='fence1')
for i in range(4):
    fence2 = Entity(model='quad', texture='assets/fence2', scale=1, position=(1+i, -i*0.5), collider='box', tag='fence2')
for i in range(4):
    fence1 = Entity(model='quad', texture='assets/fence1', scale=1, position=(4-i, -5*0.42 -i*0.42), collider='box', tag='fence1')
for i in range(4):
    fence2 = Entity(model='quad', texture='assets/fence2', scale=1, position=(-4 + 1+i, -4*0.44 -i*0.5), collider='box', tag='fence2')

# COW

class Cow(Entity):
    def __init__(self):
        super().__init__()
        self.model='quad',
        self.position = (0,-2),
        self.tag='cow1'


    def eat(self):
        a.state = 'eat'
    def to_eat(self):
        a.state = 'to_eat'
    def off_eat(self):
        a.state = 'off_eat'

cow1 = Cow()


cow1_idle = Animation("assets/cow1/cow1_idle", parent = cow1, autoplay=True, scale=2)
cow1_walk = Animation("assets/cow1/cow1_walk", parent = cow1, autoplay=False, scale=2)
cow1_eat = Animation("assets/cow1/cow1_eat", parent = cow1, autoplay=False, scale=2)
cow1_to_eat = Animation("assets/cow1/cow1_to_eat", parent = cow1, autoplay=False, loop=False, scale=2)
cow1_off_eat = Animation("assets/cow1/cow1_off_eat", parent = cow1, autoplay=False, loop=False, scale=2)

a=Animator(
    animations= {'idle': cow1_idle,
                 'walk': cow1_walk,
                 'off_eat': cow1_off_eat,
                 'to_eat': cow1_to_eat,
                 'eat': cow1_eat,
    })

# set the initial state
a.state = 'idle'

s1 = Sequence(Func(cow1.to_eat, duration=1),Func(cow1.eat, duration=1), Func(cow1.off_eat, duration=1) )

# input handler
def input(key):
    if key == '1':
        a.state = 'idle'
    if key == '2':
        a.state = 'walk'
    if key == '3':
        a.state = 'to_eat'
    if key == '4':
        a.state = 'eat'
    if key == '5':
        a.state = 'off_eat'
    if key == '6':
        s1.start()

s1.start()

# start running the game
app.run()
0

There are 0 best solutions below