Vore_Sandbox_Remaster/scripts/prey.gd

177 lines
5.3 KiB
GDScript

extends KinematicBody2D
class_name Prey
const DIGESTIONGRAD:Gradient = preload("res://other/digestion.tres")
var velocity = Vector2(0,0)
export var draggable = false
var healthmax:int = 1000
var health:int
var SPEED = 320
const JUMPF = -600
const GRAVITY = 30
var TINYWHEN:Vector2 = Vector2(0.3,0.3)# how small to get when shrunk
var collectsize:Vector2 = Vector2(0.3,0.3)# how small to get before can be collected
var squeeze:float = 0.7# how small to get before can be collected
# this walk var will deturmine where the rabbit wants to move.
# if 0, rabbit doesnt move
# if posative number, rabbit moves right and subtract one
# if negative number, rabbit moves left and subtract one
var walk = 0
var mysizeratio = 0 #this will be stored in the prey when eaten
var grabbed = false
onready var tween = $Tween
var inacid = false
onready var grab:Area2D = $grab
var launched = false #when true they dont move until they hit the ground
var preytype = "" #the type of prey from the preydata global dictionary.
var data:Dictionary
var spawn:Vector2
var dead:bool = false
# Called when the node enters the scene tree for the first time.
func _ready():
health = healthmax
spawn = position
add_to_group("prey")
if preytype.empty():#if no type is specified upon creation, pick random one
#get choices
var choices = Global.preydata.keys()
if choices.empty():
#if prey list is empty then just delete itself
queue_free()
return
#pick one
preytype = choices[rand_range(0,choices.size())]
#load data from prey data dictionary
data = Global.preydata.get(preytype,{})
#set the values using get() with defaults:
_load_data()
func _load_data():
# set the attributes from the data var
health = data.get("health",healthmax)
healthmax = data.get("health",healthmax)
$Collision.shape = data.get("shape",$Collision.shape)
$Collision.rotation_degrees = data.get("shaperot",0)
scale = data.get("size",scale)
TINYWHEN = data.get("shrunksize",TINYWHEN)
squeeze = data.get("squeeze",squeeze)
collectsize = data.get("collectsize",collectsize)
$flip/body.texture = data.get("SpriteMain",$flip/body.texture)
func _physics_process(delta):
#move when told to
if walk > 0 and not grabbed and not launched and not dead:
velocity.x = SPEED*scale.x
$AnimatedSprite.flip_h = false
$flip.scale.x = 1
walk -= 1
elif walk < 0 and not grabbed and not launched and not dead:
velocity.x = -SPEED*scale.x
$AnimatedSprite.flip_h = true
$flip.scale.x = -1
else:
pass
#$Sprite.play("idle")
#move it move it
velocity = move_and_slide(velocity,Vector2.UP)
if not launched:
velocity.x = 0
#velocity.x = lerp(velocity.x,0,0.2)
if is_on_wall():
# walk = 0
walk = -walk
#random moving
if int(rand_range(0,200)) == 0:
walk = int(rand_range(-100,200))
velocity.y += GRAVITY*scale.x
#random jumping
if int(rand_range(0,150)) == 0 and is_on_floor() and not grabbed and not dead:
velocity.y = JUMPF * scale.x
if position.y > 20000:
position = spawn
velocity.y = 0
if grabbed:
var new_pos:Vector2 = get_global_mouse_position()
# if not test_move(Transform2D(rotation,new_pos),Vector2.ZERO):
# if not move_and_collide(new_pos-position,true,true,false):
# position = new_pos
velocity = move_and_slide((new_pos-position)*30,Vector2.ZERO,false,4,0.7,true)
var squeezing:float = clamp(1 - position.distance_to(new_pos) / 1000,squeeze,1)
$Collision.scale = Vector2(squeezing,squeezing)
if inacid:
health -= 1
modulate = DIGESTIONGRAD.interpolate(1-float(health) / float(healthmax))
#modulate.g = float(health) / float(healthmax)
#modulate.b = float(health) / float(healthmax)
#modulate.r += 0.001
if health == 0:
if get_parent() is PreyGroup:
get_parent().emit_signal("a_prey_digested")
queue_free()
dead = (health < healthmax / 2)
if launched and is_on_floor():
launched = false
rotation_degrees = 0
#debegvb
#print_debug("to ",position)
#visible = true
func shrink():
tween.interpolate_property(self,"scale",scale,TINYWHEN,0.6,Tween.TRANS_LINEAR,Tween.EASE_OUT)
tween.start()
func _input_event(viewport, event, shape_idx):
#grab prey
if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and event.pressed:
grabbed = true
func _input(event):
#ungrab prey
if grabbed and event is InputEventMouseButton and event.button_index == BUTTON_LEFT and not event.pressed:
ungrab()
elif grabbed and event.is_action_pressed("rotate_CW"):
rotation_degrees += 15
elif grabbed and event.is_action_pressed("rotate_CCW"):
rotation_degrees -= 15
func ungrab():
grabbed = false
walk = 0
launched = true
$Collision.scale = Vector2.ONE
func set_simple(image):
#this function is unused now but keep the auto size code for later.
print($CollisionShape2D.shape.height)
#set the prey to a simple image and auto make the collision pill
if image is ImageTexture or true:
$flip/body.texture = image
print(image)
if image.get_height() > image.get_width():
$CollisionShape2D.rotation = 0
$CollisionShape2D.shape.radius = image.get_width() / 2
$CollisionShape2D.shape.height = image.get_height() - $CollisionShape2D.shape.radius*2
else:
$CollisionShape2D.shape.radius = image.get_height() / 2
$CollisionShape2D.shape.height = image.get_width() - $CollisionShape2D.shape.radius*2
func _on_grab_input_event(viewport, event, shape_idx):
_input_event(viewport, event, shape_idx)