Guest User

Untitled

a guest
Feb 26th, 2025
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends RigidBody2D
  2. class_name Critter
  3.  
  4. @export var stats:Dictionary ={
  5.     "energy":Stat.new(100,100),
  6.     "speed":Stat.new(5,100),
  7.     "vision":Stat.new(5,100),
  8.     "width":Stat.new(5,100),
  9.     "height":Stat.new(5,100),
  10.     "weight":Stat.new(5,100),
  11.     "reproductionChance":Stat.new(0.5,100),
  12.     "mutateChance":Stat.new(0.1,100),
  13.     "color":Stat.new(Color.RED)
  14. }
  15.  
  16. var collider: CollisionShape2D
  17. var destination:Vector2
  18.  
  19. #inherit stats
  20. func inheritStats(statDict:Dictionary):
  21.     stats = statDict.duplicate(true)
  22.  
  23. func mutate():
  24.     var mutations:float = stats["mutateChance"].value
  25.     var numbOfstats =stats.size()
  26.     var index = randi_range(0,numbOfstats-1)
  27.     for i in range(numbOfstats):
  28.         var stat:Stat = stats.values()[i].duplicate(true)
  29.         if mutations>randf():
  30.             mutations-=1
  31.             if stat.value is float or stat.value is int:
  32.                 stat.value += randi_range(-1,1)
  33.             elif  stat.value is Color:
  34.                 var newColor:Color = stat.value
  35.                 newColor.r += randi_range(-10,10)
  36.                 newColor.g += randi_range(-10,10)
  37.                 newColor.b += randi_range(-10,10)
  38.                 stat.value=newColor
  39.        
  40.         index+=1
  41.         if index >= numbOfstats:
  42.             index=0
  43.  
  44. func setup():
  45.     collider = $CollisionShape2D
  46.     contact_monitor =1
  47.     max_contacts_reported+=16
  48.     connect("body_entered", Callable(self, "_on_body_entered"))
  49.     connect("body_exited", Callable(self, "_on_body_exited"))
  50.    
  51.     collider.modulate =stats["color"].value
  52.     collider.scale.x=stats["width"].value
  53.     collider.scale.y=stats["height"].value
  54.     mass = stats["weight"].value
  55.    
  56.     for child in get_children():
  57.         if child has_method("activate")==true:
  58.             child.activate(self)
  59.  
  60. func _process(delta: float) -> void:
  61.     move(delta)
  62.  
  63. func  _physics_process(_delta: float) -> void:
  64.     physiscs_move()
  65.  
  66. #region movement
  67. var travelTime:float =0
  68.  
  69. func move(delta):
  70.     #timer
  71.     travelTime +=delta
  72.     if travelTime>=5:
  73.         travelTime-=5
  74.         #pick new destination
  75.         destination = position +  Vector2(randf_range(-stats["vision"].value,stats["vision"].value),(randf_range(-stats["vision"].value,stats["vision"].value)))
  76.  
  77. func physiscs_move():
  78.     #move
  79.     var antiGrav = -Vector2.UP*980
  80.     var dir =(destination-position).normalized()
  81.     var moveForce = dir*mass*stats["speed"].value
  82.     apply_force(moveForce*antiGrav)
  83. #endregion
  84.  
  85. #region reproduction
  86.  
  87. var partners: Array[Array]
  88.  
  89. #func _on_body_entered(body: Node) -> void:
  90.     #if body is Critter:
  91.         #partners.append(body.stats.duplicate(true))
  92.  
  93. func _on_body_exited(body: Node) -> void:
  94.     if partners.size()==0:return
  95.     if body is not Critter:return
  96.    
  97.     #partners.append(stats)
  98.     var children:int = int(stats["reproductionChance"].value)
  99.     if randf()> stats["reproductionChance"].value - children:
  100.         children+=1
  101.         stats["reproductionChance"].value-=1
  102.    
  103.     #for c in range(children):
  104.         #var baby_data:CritterData = CritterData.new()
  105.         #baby_data.inherit(parents)
  106.         #baby_data.mutate()
  107.     #data. partners.clear()
  108. #endregion
  109.  
Advertisement
Add Comment
Please, Sign In to add comment