Created
December 29, 2020 06:45
-
-
Save NEO97online/6720992702e46bfbb52357b5c1a7c400 to your computer and use it in GitHub Desktop.
PlayerLegFSM
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 StateMachine | |
| enum LegState { | |
| IDLE, RUN, SNEAK, JUMP, FALL, DASH, SLIDE, WALLRUN, WALLJUMP | |
| } | |
| var dash_frames = 0 | |
| var dash_direction = Vector3.ZERO | |
| var slide_direction = Vector3.ZERO | |
| var walljump_frames = 0 | |
| var walljump_direction = Vector3.ZERO | |
| func _state_logic(state, event: int, delta: float): | |
| var move_vec = Vector3() | |
| if Input.is_action_pressed("move_forward"): | |
| move_vec += Vector3.FORWARD | |
| if Input.is_action_pressed("move_backward"): | |
| move_vec += Vector3.BACK | |
| if Input.is_action_pressed("move_left"): | |
| move_vec += Vector3.LEFT | |
| if Input.is_action_pressed("move_right"): | |
| move_vec += Vector3.RIGHT | |
| if event == StateEvent.PROCESS: | |
| parent.leg_state_text.text = "LegState: " + LegState.keys()[state] | |
| match state: | |
| LegState.IDLE: | |
| match event: | |
| StateEvent.ENTER: | |
| pass | |
| StateEvent.PROCESS: | |
| parent.velocity += Vector3.ZERO - parent.velocity * Vector3(parent.drag, 0, parent.drag) + parent.gravity * Vector3.DOWN * delta | |
| var grounded = parent.is_on_floor() | |
| if grounded: | |
| parent.velocity.y = -0.01 | |
| if Input.is_action_just_pressed("dash"): | |
| return LegState.DASH | |
| if Input.is_action_just_pressed("jump"): | |
| return LegState.JUMP | |
| if move_vec != Vector3.ZERO: | |
| return LegState.RUN | |
| StateEvent.EXIT: | |
| pass | |
| LegState.RUN: | |
| match event: | |
| StateEvent.ENTER: | |
| pass | |
| StateEvent.PROCESS: | |
| var cur_move_vec = move_vec | |
| if !parent.ignore_rotation: | |
| cur_move_vec = cur_move_vec.rotated(Vector3.UP, parent.rotation.y) | |
| parent.velocity += parent.move_accel * cur_move_vec.normalized() - parent.velocity * Vector3(parent.drag, 0, parent.drag) + parent.gravity * Vector3.DOWN * delta | |
| var grounded = parent.is_on_floor() | |
| if grounded: | |
| parent.velocity.y = -0.01 | |
| if Input.is_action_just_pressed("dash"): | |
| return LegState.DASH | |
| if Input.is_action_just_pressed("jump"): | |
| return LegState.JUMP | |
| if move_vec == Vector3.ZERO: | |
| return LegState.IDLE | |
| elif Input.is_action_pressed("slide"): | |
| return LegState.SLIDE | |
| StateEvent.EXIT: | |
| pass | |
| LegState.DASH: | |
| match event: | |
| StateEvent.ENTER: | |
| dash_frames = parent.dash_distance | |
| dash_direction = move_vec.rotated(Vector3.UP, parent.rotation.y) | |
| dash_direction.y = 0 | |
| StateEvent.PROCESS: | |
| parent.velocity = dash_direction * parent.dash_speed | |
| if dash_frames <= 0: | |
| return LegState.IDLE | |
| dash_frames -= 1 | |
| StateEvent.EXIT: | |
| pass | |
| LegState.SLIDE: | |
| match event: | |
| StateEvent.ENTER: | |
| if move_vec == Vector3.ZERO: | |
| slide_direction = Vector3.FORWARD.rotated(Vector3.UP, parent.rotation.y) | |
| slide_direction.y = 0 | |
| else: | |
| slide_direction = move_vec.rotated(Vector3.UP, parent.rotation.y) | |
| slide_direction.y = 0 | |
| parent.camera.transform.origin.y = parent.camerapoint_height - 0.5 | |
| StateEvent.PROCESS: | |
| parent.velocity += parent.slide_accel * slide_direction - parent.velocity * Vector3(parent.drag, 0, parent.drag) + parent.gravity * Vector3.DOWN * delta | |
| if Input.is_action_just_pressed("jump"): | |
| return LegState.JUMP | |
| if not Input.is_action_pressed("slide"): | |
| return LegState.IDLE | |
| StateEvent.EXIT: | |
| parent.character_mover.stop_slide() | |
| parent.camera.transform.origin.y = parent.camerapoint_height | |
| LegState.JUMP: | |
| match event: | |
| StateEvent.ENTER: | |
| parent.velocity.y = parent.jump_force | |
| parent.snap_vec = Vector3.ZERO | |
| StateEvent.PROCESS: | |
| return LegState.FALL | |
| StateEvent.EXIT: | |
| pass | |
| LegState.FALL: | |
| match event: | |
| StateEvent.ENTER: | |
| parent.snap_vec = Vector3.DOWN | |
| StateEvent.PROCESS: | |
| var cur_move_vec = move_vec | |
| if !parent.ignore_rotation: | |
| cur_move_vec = cur_move_vec.rotated(Vector3.UP, parent.rotation.y) | |
| var air_move_accel = 0.3 | |
| var drag = 0.02 | |
| parent.velocity += air_move_accel * cur_move_vec.normalized() - parent.velocity * Vector3(drag, 0, drag) | |
| parent.velocity.y -= parent.gravity * delta | |
| if Input.is_action_just_pressed("dash"): | |
| return LegState.DASH | |
| if parent.is_on_floor(): | |
| return LegState.IDLE | |
| if parent.is_on_wall(): | |
| return LegState.WALLRUN | |
| StateEvent.EXIT: | |
| parent.snap_vec = Vector3.ZERO | |
| LegState.WALLRUN: | |
| match event: | |
| StateEvent.ENTER: | |
| pass | |
| StateEvent.PROCESS: | |
| var cur_move_vec = move_vec | |
| if !parent.ignore_rotation: | |
| cur_move_vec = cur_move_vec.rotated(Vector3.UP, parent.rotation.y) | |
| parent.velocity += (parent.move_accel*2) * cur_move_vec.normalized() - parent.velocity * Vector3(parent.drag, 0, parent.drag) | |
| parent.velocity.y = 0 | |
| if not parent.is_on_wall(): | |
| return LegState.FALL | |
| if Input.is_action_just_pressed("jump"): | |
| return LegState.WALLJUMP | |
| StateEvent.EXIT: | |
| pass | |
| LegState.WALLJUMP: | |
| match event: | |
| StateEvent.ENTER: | |
| walljump_direction = parent.get_slide_collision(0).normal.round() | |
| parent.snap_vec = Vector3.ZERO | |
| var walljump_vec = walljump_direction * parent.walljump_force | |
| parent.velocity.x = walljump_vec.x | |
| parent.velocity.z = walljump_vec.z | |
| parent.velocity.y = parent.jump_force | |
| StateEvent.PROCESS: | |
| return LegState.FALL | |
| StateEvent.EXIT: | |
| pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment