Created
November 30, 2017 00:03
-
-
Save MarianoGnu/6913118b7d380e640eb80e5346ee7002 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends Node | |
export(flag) var behaviours; | |
const GO_TO_CHECKPOINT = 1; | |
const EVADE = 2; | |
const EVADE_WALLS = 4; | |
const PICKUP_PERK = 8; | |
const ADVANCE_STEP_DISTANCE = 8200.0; | |
export(float) var go_to_chkp_strenght | |
var next_checkpoint = 0 setget _recalculate_path; | |
var loop_count = 0; | |
var vehicle_node; | |
#Used for Go_to_checkpoint | |
var path; | |
var nav_helper; | |
#Used for Evade | |
var near_bodies = []; | |
#Used for Evade_walls | |
var raycasters = []; | |
func _ready(): | |
nav_helper = get_node("/root/Node/Navigation2D"); | |
vehicle_node = get_node(".."); | |
var rc = get_node("RayCast1"); | |
if (rc != null): | |
rc.add_exception(vehicle_node); | |
raycasters.append(rc); | |
rc = get_node("RayCast2"); | |
if (rc != null): | |
rc.add_exception(vehicle_node); | |
raycasters.append(rc); | |
rc = get_node("RayCast3"); | |
if (rc != null): | |
rc.add_exception(vehicle_node); | |
raycasters.append(rc); | |
self.next_checkpoint = 1; | |
func game_start(): | |
set_fixed_process(true); | |
func _recalculate_path(val): | |
next_checkpoint = val; | |
path = nav_helper.get_path_to_checkpoint(vehicle_node.get_pos(), next_checkpoint); | |
func _fixed_process(delta): | |
var force = calculate_force(); | |
if (force.y > 0.1): | |
vehicle_node.movement_axis = -1.0; | |
elif (force.y < -0.1): | |
vehicle_node.movement_axis = 1.0; | |
else: | |
vehicle_node.movement_axis = 0.0; | |
if (force.x > 0.1): | |
vehicle_node.torque = 1.0; | |
elif (force.x < -0.1): | |
vehicle_node.torque = -1.0; | |
else: | |
vehicle_node.torque = 0.0; | |
func calculate_force(): | |
var force = Vector2(); | |
if (behaviours & GO_TO_CHECKPOINT): | |
force += _go_to_checkpoint(); | |
# force = force.normalized(); | |
if (behaviours & EVADE): | |
force += _evade(); | |
# force = force.normalized(); | |
if (behaviours & EVADE_WALLS): | |
force += _evade_walls(); | |
# force = force.normalized(); | |
if (behaviours & PICKUP_PERK): | |
force += _pickup_perk(); | |
# force = force.normalized(); | |
return force.normalized(); | |
func _go_to_checkpoint(): | |
if (path == null || path.size() == 0): | |
return Vector2(); | |
var step = Vector2(); | |
step = (path[0] - vehicle_node.get_pos()) | |
if step.length_squared() < ADVANCE_STEP_DISTANCE and path.size() > 1: | |
path.remove(0); | |
var m = Matrix32(-vehicle_node.get_rot(), Vector2()); | |
step = m * step; | |
return step; | |
func _evade(): | |
return Vector2(); | |
func _evade_walls(): | |
var rc = RayCast2D.new(); | |
var pos = Vector2(); | |
pos = vehicle_node.get_pos(); | |
var nearest_dist = 999999.0; | |
var magnitude = 0.0; | |
var direction = Vector2(); | |
for rc in raycasters: | |
if rc.is_colliding(): | |
if (pos.distance_squared_to(rc.get_collision_point()) < nearest_dist): | |
nearest_dist = pos.distance_squared_to(rc.get_collision_point()); | |
direction = rc.get_collision_normal(); | |
magnitude = rc.get_cast_to().length_squared() / nearest_dist; | |
return direction * magnitude; | |
func _pickup_perk(): | |
return Vector2(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment