valeatory

Ren'Py - Blinking and talking with AttributeImage

Jun 25th, 2017
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.25 KB | None | 0 0
  1. ## This is the entire script, but it also needs this file: https://github.com/renpy/ai/blob/master/game/ai.rpy
  2. ## It is expecting that you have certain files in your images folder - the current format requires:
  3. ## natalie_ body_casual.png
  4. ## natalie_ brow_angry.png
  5. ## natalie_ brow_neutral.png
  6. ## natalie_ brow_sad.png
  7. ## natalie_ eyes_closed.png
  8. ## natalie_ eyes_closing.png
  9. ## natalie_ eyes_open.png
  10. ## natalie_ eyes_resting.png
  11. ## natalie_ hair_normal_hair.png
  12. ## natalie_ head.png
  13. ## natalie_ mouth_closed.png
  14. ## natalie_ mouth_open.png
  15. ## natalie_ nose.png
  16.  
  17. ## The script of the game goes in this file.
  18. define n = Character('Natalie')
  19.  
  20. transform blinking_anim(norm_d, closing_d, blink_d):
  21.     norm_d
  22.     choice:#random pause
  23.         0.1
  24.     choice:
  25.         1
  26.     choice:
  27.         2
  28.     choice:
  29.         3
  30.     choice:
  31.         4
  32.     closing_d
  33.     pause .1
  34.     blink_d
  35.     pause .2
  36.     closing_d
  37.     pause .15
  38.     repeat
  39.  
  40. transform anim_talk_once(speak_d, norm_d, anim_time):
  41.     speak_d
  42.     pause anim_time*3/4
  43.     norm_d
  44.     pause anim_time
  45.  
  46. transform talking_anim(speak_d, norm_d):
  47.     choice:
  48.         anim_talk_once(speak_d, norm_d, .3)
  49.     choice:
  50.         anim_talk_once(speak_d, norm_d, .4)
  51.     repeat
  52.  
  53.  
  54.  
  55. #Callbacks for lip flap and dialogue color
  56. init python:
  57.     # This returns speaking art if the identified character is speaking, and done art if the
  58.     # character is not.
  59.     def while_speaking(ident, speak_d, done_d, st, at):
  60.         if _last_say_who == ident:
  61.             return talking_anim(speak_d, done_d), .5
  62.         else:
  63.             return done_d, None
  64.  
  65.     # Curried form of the above.
  66.     curried_while_speaking = renpy.curry(while_speaking)
  67.  
  68.     # Displays speaking when the identified character is speaking, and done otherwise.
  69.     def WhileSpeaking(name, speak_d, done_d=None):
  70.         if not done_d: done_d = speak_d
  71.         return DynamicDisplayable(curried_while_speaking(name, speak_d, done_d))
  72.  
  73.  
  74. #composite image stuff, not relevant to the ATL
  75. init python:
  76.     attr_types = [
  77.         ("body", ("casual",)),
  78.         ("head", ()),
  79.         ("brow", ("neutral", "sad", "angry")), #default selection first
  80.         ("eyes", ("open", "closed", "closing", "resting")),
  81.         ("nose", ()),
  82.         ("hair", ("normal_hair",)),
  83.         ("mouth", ("closed", "open")),
  84.     ]
  85.     def setup_attribute_image(img_name):
  86.         attr_list = []
  87.         for type_name, option_list in attr_types:
  88.             if len(option_list)==0: #always-on pieces (they will never have options)
  89.                 attr_list.append(Attribute(type_name, type_name, type_name, default=True))
  90.             else:
  91.                 for i, option in enumerate(option_list):
  92.                     if type_name == "eyes":
  93.                         attr_list.append(
  94.                             Attribute(
  95.                                 type_name, #no more than one of this same type may be visible at once
  96.                                 type_name+"_"+option, #the keyword to get this option with
  97.                                 #closed eyes don't blink
  98.                                 (type_name+"_"+option if option in ("closed", "resting")
  99.                                 else blinking_anim(
  100.                                     img_name+"_ "+type_name+"_"+option,
  101.                                     img_name+"_ "+type_name+"_closing",
  102.                                     img_name+"_ "+type_name+"_resting")), #the displayable
  103.                                 default=(i==0)) #the first one appears automatically
  104.                         )
  105.                     elif type_name == "mouth":
  106.                         attr_list.append(
  107.                             Attribute(
  108.                                 type_name, #no more than one of this same type may be visible at once
  109.                                 type_name+"_"+option, #the keyword to get this option with
  110.                                 WhileSpeaking("n",
  111.                                     img_name+"_ "+type_name+"_open", #todo: closed/open from keyword
  112.                                     img_name+"_ "+type_name+"_"+option), #the displayable
  113.                                 default=(i==0)) #the first one appears automatically
  114.                         )
  115.                     else:
  116.                         attr_list.append(
  117.                             Attribute(
  118.                                 type_name, #no more than one of this same type may be visible at once
  119.                                 option, #the keyword to get this option with
  120.                                 type_name+"_"+option, #the filename keywords
  121.                                 default=(i==0)) #the first one appears automatically
  122.                         )
  123.  
  124.         renpy.image(img_name, AttributeImage(attr_list,
  125.             image_format=img_name+"_ {image}",
  126.             at=Transform(yalign=0.0),
  127.         )
  128.     )
  129.  
  130.     setup_attribute_image("natalie")
  131.  
  132. label start:
  133.     scene bg room
  134.  
  135.     show natalie
  136.     "This is the default image."
  137.     n "I'm talking now."
  138.  
  139.     show natalie sad
  140.     n "Sadness."
  141.  
  142.     show natalie eyes_closed
  143.     n "Closed eyes."
  144.  
  145.     show natalie angry
  146.     n "Anger."
  147.  
  148.     show natalie neutral
  149.     n "...And neutral again."
  150.  
  151.     "just blink"
  152.  
  153.     ## This ends the game.
  154.  
  155.     return
Advertisement
Add Comment
Please, Sign In to add comment