mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 17:50:22 +00:00
Add support for the GDScript language
References: * https://github.com/okamstudio/godot/wiki/gdscript * http://www.godotengine.org/ Some projects using it: * https://github.com/okamstudio/godot * https://github.com/Qwertie-/Godot-games My motivation for adding it: To disambiguate these .gd files from GAP .gd files.
This commit is contained in:
@@ -785,6 +785,12 @@ GAS:
|
||||
- .s
|
||||
- .S
|
||||
|
||||
GDScript:
|
||||
type: programming
|
||||
lexer: Text only
|
||||
extensions:
|
||||
- .gd
|
||||
|
||||
GLSL:
|
||||
group: C
|
||||
type: programming
|
||||
|
||||
57
samples/GDScript/example.gd
Normal file
57
samples/GDScript/example.gd
Normal file
@@ -0,0 +1,57 @@
|
||||
# Taken from https://github.com/okamstudio/godot/wiki/gdscript
|
||||
# a file is a class!
|
||||
|
||||
# inheritance
|
||||
|
||||
extends BaseClass
|
||||
|
||||
# member variables
|
||||
|
||||
var a = 5
|
||||
var s = "Hello"
|
||||
var arr = [1, 2, 3]
|
||||
var dict = {"key":"value", 2:3}
|
||||
|
||||
# constants
|
||||
|
||||
const answer = 42
|
||||
const thename = "Charly"
|
||||
|
||||
# built-in vector types
|
||||
|
||||
var v2 = Vector2(1, 2)
|
||||
var v3 = Vector3(1, 2, 3)
|
||||
|
||||
# function
|
||||
|
||||
func some_function(param1, param2):
|
||||
var local_var = 5
|
||||
|
||||
if param1 < local_var:
|
||||
print(param1)
|
||||
elif param2 > 5:
|
||||
print(param2)
|
||||
else:
|
||||
print("fail!")
|
||||
|
||||
for i in range(20):
|
||||
print(i)
|
||||
|
||||
while(param2 != 0):
|
||||
param2 -= 1
|
||||
|
||||
var local_var2 = param1+3
|
||||
return local_var2
|
||||
|
||||
|
||||
# subclass
|
||||
|
||||
class Something:
|
||||
var a = 10
|
||||
|
||||
# constructor
|
||||
|
||||
func _init():
|
||||
print("constructed!")
|
||||
var lv = Something.new()
|
||||
print(lv.a)
|
||||
216
samples/GDScript/grid.gd
Normal file
216
samples/GDScript/grid.gd
Normal file
@@ -0,0 +1,216 @@
|
||||
|
||||
|
||||
extends Control
|
||||
|
||||
# Simple Tetris-like demo, (c) 2012 Juan Linietsky
|
||||
# Implemented by using a regular Control and drawing on it during the _draw() callback.
|
||||
# The drawing surface is updated only when changes happen (by calling update())
|
||||
|
||||
|
||||
var score = 0
|
||||
var score_label=null
|
||||
|
||||
const MAX_SHAPES = 7
|
||||
|
||||
var block = preload("block.png")
|
||||
|
||||
var block_colors=[
|
||||
Color(1,0.5,0.5),
|
||||
Color(0.5,1,0.5),
|
||||
Color(0.5,0.5,1),
|
||||
Color(0.8,0.4,0.8),
|
||||
Color(0.8,0.8,0.4),
|
||||
Color(0.4,0.8,0.8),
|
||||
Color(0.7,0.7,0.7)]
|
||||
|
||||
var block_shapes=[
|
||||
[ Vector2(0,-1),Vector2(0,0),Vector2(0,1),Vector2(0,2) ], # I
|
||||
[ Vector2(0,0),Vector2(1,0),Vector2(1,1),Vector2(0,1) ], # O
|
||||
[ Vector2(-1,1),Vector2(0,1),Vector2(0,0),Vector2(1,0) ], # S
|
||||
[ Vector2(1,1),Vector2(0,1),Vector2(0,0),Vector2(-1,0) ], # Z
|
||||
[ Vector2(-1,1),Vector2(-1,0),Vector2(0,0),Vector2(1,0) ], # L
|
||||
[ Vector2(1,1),Vector2(1,0),Vector2(0,0),Vector2(-1,0) ], # J
|
||||
[ Vector2(0,1),Vector2(1,0),Vector2(0,0),Vector2(-1,0) ]] # T
|
||||
|
||||
|
||||
var block_rotations=[
|
||||
Matrix32( Vector2(1,0),Vector2(0,1), Vector2() ),
|
||||
Matrix32( Vector2(0,1),Vector2(-1,0), Vector2() ),
|
||||
Matrix32( Vector2(-1,0),Vector2(0,-1), Vector2() ),
|
||||
Matrix32( Vector2(0,-1),Vector2(1,0), Vector2() )
|
||||
]
|
||||
|
||||
|
||||
var width=0
|
||||
var height=0
|
||||
|
||||
var cells={}
|
||||
|
||||
var piece_active=false
|
||||
var piece_shape=0
|
||||
var piece_pos=Vector2()
|
||||
var piece_rot=0
|
||||
|
||||
|
||||
func piece_cell_xform(p,er=0):
|
||||
var r = (4+er+piece_rot)%4
|
||||
return piece_pos+block_rotations[r].xform(p)
|
||||
|
||||
func _draw():
|
||||
|
||||
var sb = get_stylebox("bg","Tree") # use line edit bg
|
||||
draw_style_box(sb,Rect2(Vector2(),get_size()).grow(3))
|
||||
|
||||
var bs = block.get_size()
|
||||
for y in range(height):
|
||||
for x in range(width):
|
||||
if (Vector2(x,y) in cells):
|
||||
draw_texture_rect(block,Rect2(Vector2(x,y)*bs,bs),false,block_colors[cells[Vector2(x,y)]])
|
||||
|
||||
if (piece_active):
|
||||
|
||||
for c in block_shapes[piece_shape]:
|
||||
draw_texture_rect(block,Rect2(piece_cell_xform(c)*bs,bs),false,block_colors[piece_shape])
|
||||
|
||||
|
||||
func piece_check_fit(ofs,er=0):
|
||||
|
||||
for c in block_shapes[piece_shape]:
|
||||
var pos = piece_cell_xform(c,er)+ofs
|
||||
if (pos.x < 0):
|
||||
return false
|
||||
if (pos.y < 0):
|
||||
return false
|
||||
if (pos.x >= width):
|
||||
return false
|
||||
if (pos.y >= height):
|
||||
return false
|
||||
if (pos in cells):
|
||||
return false
|
||||
|
||||
return true
|
||||
|
||||
func new_piece():
|
||||
|
||||
piece_shape = randi() % MAX_SHAPES
|
||||
piece_pos = Vector2(width/2,0)
|
||||
piece_active=true
|
||||
piece_rot=0
|
||||
if (piece_shape==0):
|
||||
piece_pos.y+=1
|
||||
|
||||
if (not piece_check_fit(Vector2())):
|
||||
#game over
|
||||
#print("GAME OVER!")
|
||||
game_over()
|
||||
|
||||
update()
|
||||
|
||||
|
||||
func test_collapse_rows():
|
||||
var accum_down=0
|
||||
for i in range(height):
|
||||
var y = height - i - 1
|
||||
var collapse = true
|
||||
for x in range(width):
|
||||
if (Vector2(x,y) in cells):
|
||||
if (accum_down):
|
||||
cells[ Vector2(x,y+accum_down) ] = cells[Vector2(x,y)]
|
||||
else:
|
||||
collapse=false
|
||||
if (accum_down):
|
||||
cells.erase( Vector2(x,y+accum_down) )
|
||||
|
||||
if (collapse):
|
||||
accum_down+=1
|
||||
|
||||
|
||||
score+=accum_down*100
|
||||
score_label.set_text(str(score))
|
||||
|
||||
|
||||
func game_over():
|
||||
|
||||
piece_active=false
|
||||
get_node("gameover").set_text("Game Over")
|
||||
update()
|
||||
|
||||
|
||||
func restart_pressed():
|
||||
|
||||
score=0
|
||||
score_label.set_text("0")
|
||||
cells.clear()
|
||||
get_node("gameover").set_text("")
|
||||
piece_active=true
|
||||
update()
|
||||
|
||||
|
||||
|
||||
func piece_move_down():
|
||||
|
||||
if (!piece_active):
|
||||
return
|
||||
if (piece_check_fit(Vector2(0,1))):
|
||||
piece_pos.y+=1
|
||||
update()
|
||||
else:
|
||||
|
||||
for c in block_shapes[piece_shape]:
|
||||
var pos = piece_cell_xform(c)
|
||||
cells[pos]=piece_shape
|
||||
test_collapse_rows()
|
||||
new_piece()
|
||||
|
||||
|
||||
func piece_rotate():
|
||||
|
||||
var adv = 1
|
||||
if (not piece_check_fit(Vector2(),1)):
|
||||
return
|
||||
piece_rot = (piece_rot + adv) % 4
|
||||
update()
|
||||
|
||||
|
||||
|
||||
func _input(ie):
|
||||
|
||||
|
||||
if (not piece_active):
|
||||
return
|
||||
if (!ie.is_pressed()):
|
||||
return
|
||||
|
||||
if (ie.is_action("move_left")):
|
||||
if (piece_check_fit(Vector2(-1,0))):
|
||||
piece_pos.x-=1
|
||||
update()
|
||||
elif (ie.is_action("move_right")):
|
||||
if (piece_check_fit(Vector2(1,0))):
|
||||
piece_pos.x+=1
|
||||
update()
|
||||
elif (ie.is_action("move_down")):
|
||||
piece_move_down()
|
||||
elif (ie.is_action("rotate")):
|
||||
piece_rotate()
|
||||
|
||||
|
||||
func setup(w,h):
|
||||
width=w
|
||||
height=h
|
||||
set_size( Vector2(w,h)*block.get_size() )
|
||||
new_piece()
|
||||
get_node("timer").start()
|
||||
|
||||
|
||||
func _ready():
|
||||
# Initalization here
|
||||
|
||||
setup(10,20)
|
||||
score_label = get_node("../score")
|
||||
|
||||
set_process_input(true)
|
||||
|
||||
|
||||
|
||||
|
||||
243
samples/GDScript/player.gd
Normal file
243
samples/GDScript/player.gd
Normal file
@@ -0,0 +1,243 @@
|
||||
|
||||
extends RigidBody
|
||||
|
||||
# member variables here, example:
|
||||
# var a=2
|
||||
# var b="textvar"
|
||||
|
||||
#var dir=Vector3()
|
||||
|
||||
const ANIM_FLOOR = 0
|
||||
const ANIM_AIR_UP = 1
|
||||
const ANIM_AIR_DOWN = 2
|
||||
|
||||
const SHOOT_TIME = 1.5
|
||||
const SHOOT_SCALE = 2
|
||||
|
||||
const CHAR_SCALE = Vector3(0.3,0.3,0.3)
|
||||
|
||||
var facing_dir = Vector3(1, 0, 0)
|
||||
var movement_dir = Vector3()
|
||||
|
||||
var jumping=false
|
||||
|
||||
var turn_speed=40
|
||||
var keep_jump_inertia = true
|
||||
var air_idle_deaccel = false
|
||||
var accel=19.0
|
||||
var deaccel=14.0
|
||||
var sharp_turn_threshhold = 140
|
||||
|
||||
var max_speed=3.1
|
||||
var on_floor = false
|
||||
|
||||
var prev_shoot = false
|
||||
|
||||
var last_floor_velocity = Vector3()
|
||||
|
||||
var shoot_blend = 0
|
||||
|
||||
func adjust_facing(p_facing, p_target,p_step, p_adjust_rate,current_gn):
|
||||
|
||||
var n = p_target # normal
|
||||
var t = n.cross(current_gn).normalized()
|
||||
|
||||
var x = n.dot(p_facing)
|
||||
var y = t.dot(p_facing)
|
||||
|
||||
var ang = atan2(y,x)
|
||||
|
||||
if (abs(ang)<0.001): # too small
|
||||
return p_facing
|
||||
|
||||
var s = sign(ang)
|
||||
ang = ang * s
|
||||
var turn = ang * p_adjust_rate * p_step
|
||||
var a
|
||||
if (ang<turn):
|
||||
a=ang
|
||||
else:
|
||||
a=turn
|
||||
ang = (ang - a) * s
|
||||
|
||||
return ((n * cos(ang)) + (t * sin(ang))) * p_facing.length()
|
||||
|
||||
|
||||
|
||||
func _integrate_forces( state ):
|
||||
|
||||
var lv = state.get_linear_velocity() # linear velocity
|
||||
var g = state.get_total_gravity()
|
||||
var delta = state.get_step()
|
||||
var d = 1.0 - delta*state.get_total_density()
|
||||
if (d<0):
|
||||
d=0
|
||||
lv += g * delta #apply gravity
|
||||
|
||||
var anim = ANIM_FLOOR
|
||||
|
||||
var up = -g.normalized() # (up is against gravity)
|
||||
var vv = up.dot(lv) # vertical velocity
|
||||
var hv = lv - (up*vv) # horizontal velocity
|
||||
|
||||
|
||||
|
||||
var hdir = hv.normalized() # horizontal direction
|
||||
var hspeed = hv.length() #horizontal speed
|
||||
|
||||
var floor_velocity
|
||||
var onfloor = false
|
||||
|
||||
if (state.get_contact_count() == 0):
|
||||
floor_velocity = last_floor_velocity
|
||||
else:
|
||||
for i in range(state.get_contact_count()):
|
||||
if (state.get_contact_local_shape(i) != 1):
|
||||
continue
|
||||
|
||||
onfloor = true
|
||||
floor_velocity = state.get_contact_collider_velocity_at_pos(i)
|
||||
break
|
||||
|
||||
|
||||
var dir = Vector3() #where does the player intend to walk to
|
||||
var cam_xform = get_node("target/camera").get_global_transform()
|
||||
|
||||
if (Input.is_action_pressed("move_forward")):
|
||||
dir+=-cam_xform.basis[2]
|
||||
if (Input.is_action_pressed("move_backwards")):
|
||||
dir+=cam_xform.basis[2]
|
||||
if (Input.is_action_pressed("move_left")):
|
||||
dir+=-cam_xform.basis[0]
|
||||
if (Input.is_action_pressed("move_right")):
|
||||
dir+=cam_xform.basis[0]
|
||||
|
||||
var jump_attempt = Input.is_action_pressed("jump")
|
||||
var shoot_attempt = Input.is_action_pressed("shoot")
|
||||
|
||||
var target_dir = (dir - up*dir.dot(up)).normalized()
|
||||
|
||||
if (onfloor):
|
||||
|
||||
var sharp_turn = hspeed > 0.1 and rad2deg(acos(target_dir.dot(hdir))) > sharp_turn_threshhold
|
||||
|
||||
if (dir.length()>0.1 and !sharp_turn) :
|
||||
if (hspeed > 0.001) :
|
||||
|
||||
#linear_dir = linear_h_velocity/linear_vel
|
||||
#if (linear_vel > brake_velocity_limit and linear_dir.dot(ctarget_dir)<-cos(Math::deg2rad(brake_angular_limit)))
|
||||
# brake=true
|
||||
#else
|
||||
hdir = adjust_facing(hdir,target_dir,delta,1.0/hspeed*turn_speed,up)
|
||||
facing_dir = hdir
|
||||
else:
|
||||
|
||||
hdir = target_dir
|
||||
|
||||
if (hspeed<max_speed):
|
||||
hspeed+=accel*delta
|
||||
|
||||
else:
|
||||
hspeed-=deaccel*delta
|
||||
if (hspeed<0):
|
||||
hspeed=0
|
||||
|
||||
hv = hdir*hspeed
|
||||
|
||||
var mesh_xform = get_node("Armature").get_transform()
|
||||
var facing_mesh=-mesh_xform.basis[0].normalized()
|
||||
facing_mesh = (facing_mesh - up*facing_mesh.dot(up)).normalized()
|
||||
facing_mesh = adjust_facing(facing_mesh,target_dir,delta,1.0/hspeed*turn_speed,up)
|
||||
var m3 = Matrix3(-facing_mesh,up,-facing_mesh.cross(up).normalized()).scaled( CHAR_SCALE )
|
||||
|
||||
get_node("Armature").set_transform(Transform(m3,mesh_xform.origin))
|
||||
|
||||
if (not jumping and jump_attempt):
|
||||
vv = 7.0
|
||||
jumping = true
|
||||
get_node("sfx").play("jump")
|
||||
else:
|
||||
|
||||
if (vv>0):
|
||||
anim=ANIM_AIR_UP
|
||||
else:
|
||||
anim=ANIM_AIR_DOWN
|
||||
|
||||
var hs
|
||||
if (dir.length()>0.1):
|
||||
|
||||
hv += target_dir * (accel * 0.2) * delta
|
||||
if (hv.length() > max_speed):
|
||||
hv = hv.normalized() * max_speed
|
||||
|
||||
else:
|
||||
|
||||
if (air_idle_deaccel):
|
||||
hspeed = hspeed - (deaccel * 0.2) * delta
|
||||
if (hspeed<0):
|
||||
hspeed=0
|
||||
|
||||
hv = hdir*hspeed
|
||||
|
||||
|
||||
if (jumping and vv < 0):
|
||||
jumping=false
|
||||
|
||||
lv = hv+up*vv
|
||||
|
||||
|
||||
|
||||
if (onfloor):
|
||||
|
||||
movement_dir = lv
|
||||
#lv += floor_velocity
|
||||
last_floor_velocity = floor_velocity
|
||||
else:
|
||||
|
||||
if (on_floor) :
|
||||
|
||||
#if (keep_jump_inertia):
|
||||
# lv += last_floor_velocity
|
||||
pass
|
||||
|
||||
last_floor_velocity = Vector3()
|
||||
movement_dir = lv
|
||||
|
||||
on_floor = onfloor
|
||||
|
||||
state.set_linear_velocity(lv)
|
||||
|
||||
if (shoot_blend>0):
|
||||
shoot_blend -= delta * SHOOT_SCALE
|
||||
if (shoot_blend<0):
|
||||
shoot_blend=0
|
||||
|
||||
if (shoot_attempt and not prev_shoot):
|
||||
shoot_blend = SHOOT_TIME
|
||||
var bullet = preload("res://bullet.scn").instance()
|
||||
bullet.set_transform( get_node("Armature/bullet").get_global_transform().orthonormalized() )
|
||||
get_parent().add_child( bullet )
|
||||
bullet.set_linear_velocity( get_node("Armature/bullet").get_global_transform().basis[2].normalized() * 20 )
|
||||
PS.body_add_collision_exception( bullet.get_rid(), get_rid() ) #add it to bullet
|
||||
get_node("sfx").play("shoot")
|
||||
|
||||
prev_shoot = shoot_attempt
|
||||
|
||||
if (onfloor):
|
||||
get_node("AnimationTreePlayer").blend2_node_set_amount("walk",hspeed / max_speed)
|
||||
|
||||
get_node("AnimationTreePlayer").transition_node_set_current("state",anim)
|
||||
get_node("AnimationTreePlayer").blend2_node_set_amount("gun",min(shoot_blend,1.0))
|
||||
# state.set_angular_velocity(Vector3())
|
||||
|
||||
|
||||
|
||||
|
||||
func _ready():
|
||||
|
||||
|
||||
# Initalization here
|
||||
get_node("AnimationTreePlayer").set_active(true)
|
||||
pass
|
||||
|
||||
|
||||
73
samples/GDScript/pong.gd
Normal file
73
samples/GDScript/pong.gd
Normal file
@@ -0,0 +1,73 @@
|
||||
|
||||
extends Node2D
|
||||
|
||||
# member variables here, example:
|
||||
# var a=2
|
||||
# var b="textvar"
|
||||
const INITIAL_BALL_SPEED = 80
|
||||
var ball_speed = INITIAL_BALL_SPEED
|
||||
var screen_size = Vector2(640,400)
|
||||
#default ball direction
|
||||
var direction = Vector2(-1,0)
|
||||
var pad_size = Vector2(8,32)
|
||||
const PAD_SPEED = 150
|
||||
|
||||
|
||||
func _process(delta):
|
||||
|
||||
|
||||
# get ball positio and pad rectangles
|
||||
var ball_pos = get_node("ball").get_pos()
|
||||
var left_rect = Rect2( get_node("left").get_pos() - pad_size*0.5, pad_size )
|
||||
var right_rect = Rect2( get_node("right").get_pos() - pad_size*0.5, pad_size )
|
||||
|
||||
#integrate new ball postion
|
||||
ball_pos+=direction*ball_speed*delta
|
||||
|
||||
#flip when touching roof or floor
|
||||
if ( (ball_pos.y<0 and direction.y <0) or (ball_pos.y>screen_size.y and direction.y>0)):
|
||||
direction.y = -direction.y
|
||||
|
||||
#flip, change direction and increase speed when touching pads
|
||||
if ( (left_rect.has_point(ball_pos) and direction.x < 0) or (right_rect.has_point(ball_pos) and direction.x > 0)):
|
||||
direction.x=-direction.x
|
||||
ball_speed*=1.1
|
||||
direction.y=randf()*2.0-1
|
||||
direction = direction.normalized()
|
||||
|
||||
#check gameover
|
||||
if (ball_pos.x<0 or ball_pos.x>screen_size.x):
|
||||
ball_pos=screen_size*0.5
|
||||
ball_speed=INITIAL_BALL_SPEED
|
||||
direction=Vector2(-1,0)
|
||||
|
||||
|
||||
get_node("ball").set_pos(ball_pos)
|
||||
|
||||
#move left pad
|
||||
var left_pos = get_node("left").get_pos()
|
||||
|
||||
if (left_pos.y > 0 and Input.is_action_pressed("left_move_up")):
|
||||
left_pos.y+=-PAD_SPEED*delta
|
||||
if (left_pos.y < screen_size.y and Input.is_action_pressed("left_move_down")):
|
||||
left_pos.y+=PAD_SPEED*delta
|
||||
|
||||
get_node("left").set_pos(left_pos)
|
||||
|
||||
#move right pad
|
||||
var right_pos = get_node("right").get_pos()
|
||||
|
||||
if (right_pos.y > 0 and Input.is_action_pressed("right_move_up")):
|
||||
right_pos.y+=-PAD_SPEED*delta
|
||||
if (right_pos.y < screen_size.y and Input.is_action_pressed("right_move_down")):
|
||||
right_pos.y+=PAD_SPEED*delta
|
||||
|
||||
get_node("right").set_pos(right_pos)
|
||||
|
||||
|
||||
|
||||
func _ready():
|
||||
screen_size = get_viewport_rect().size # get actual size
|
||||
pad_size = get_node("left").get_texture().get_size()
|
||||
set_process(true)
|
||||
|
||||
Reference in New Issue
Block a user