Merge branch 'master' into 1515-local

This commit is contained in:
Arfon Smith
2014-09-17 09:54:45 -05:00
16 changed files with 56860 additions and 8 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
Gemfile.lock
.bundle/
vendor/
benchmark/

View File

@@ -1,9 +1,11 @@
before_install:
before_install:
- git fetch origin master:master
- git fetch origin v2.0.0:v2.0.0
- git fetch origin test/attributes:test/attributes
- sudo apt-get install libicu-dev -y
- gem update --system 2.1.11
before_script:
- bundle exec rake samples
rvm:
- 1.9.3
- 2.0.0

View File

@@ -2,6 +2,7 @@ require 'json'
require 'rake/clean'
require 'rake/testtask'
require 'yaml'
require 'pry'
task :default => :test
@@ -22,6 +23,67 @@ task :build_gem do
File.delete("lib/linguist/languages.json")
end
namespace :benchmark do
benchmark_path = "benchmark/results"
# $ bundle exec rake benchmark:generate CORPUS=path/to/samples
desc "Generate results for"
task :generate do
ref = `git rev-parse HEAD`.strip[0,8]
corpus = File.expand_path(ENV["CORPUS"] || "samples")
require 'linguist/language'
results = Hash.new
Dir.glob("#{corpus}/**/*").each do |file|
next unless File.file?(file)
filename = file.gsub("#{corpus}/", "")
results[filename] = Linguist::FileBlob.new(file).language
end
# Ensure results directory exists
FileUtils.mkdir_p("benchmark/results")
# Write results
if `git status`.include?('working directory clean')
result_filename = "benchmark/results/#{File.basename(corpus)}-#{ref}.json"
else
result_filename = "benchmark/results/#{File.basename(corpus)}-#{ref}-unstaged.json"
end
File.write(result_filename, results.to_json)
puts "wrote #{result_filename}"
end
# $ bundle exec rake benchmark:compare REFERENCE=path/to/reference.json CANDIDATE=path/to/candidate.json
desc "Compare results"
task :compare do
reference_file = ENV["REFERENCE"]
candidate_file = ENV["CANDIDATE"]
reference = JSON.parse(File.read(reference_file))
reference_counts = Hash.new(0)
reference.each { |filename, language| reference_counts[language] += 1 }
candidate = JSON.parse(File.read(candidate_file))
candidate_counts = Hash.new(0)
candidate.each { |filename, language| candidate_counts[language] += 1 }
changes = diff(reference_counts, candidate_counts)
if changes.any?
changes.each do |language, (before, after)|
before_percent = 100 * before / reference.size.to_f
after_percent = 100 * after / candidate.size.to_f
puts "%s changed from %.1f%% to %.1f%%" % [language || 'unknown', before_percent, after_percent]
end
else
puts "No changes"
end
end
end
namespace :classifier do
LIMIT = 1_000
@@ -71,3 +133,10 @@ namespace :classifier do
end
end
end
def diff(a, b)
(a.keys | b.keys).each_with_object({}) do |key, diff|
diff[key] = [a[key], b[key]] unless a[key] == b[key]
end
end

View File

@@ -20,6 +20,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'json'
s.add_development_dependency 'mocha'
s.add_development_dependency 'pry'
s.add_development_dependency 'rake'
s.add_development_dependency 'yajl-ruby'
end

View File

@@ -23,7 +23,7 @@ module Linguist
end
end
# .h extensions are ambigious between C, C++, and Objective-C.
# .h extensions are ambiguous between C, C++, and Objective-C.
# We want to shortcut look for Objective-C _and_ now C++ too!
#
# Returns an array of Languages or []

View File

@@ -135,7 +135,7 @@ module Linguist
# No shebang. Still more work to do. Try to find it with our heuristics.
elsif (determined = Heuristics.find_by_heuristics(data, possible_language_names)) && !determined.empty?
determined.first
# Lastly, fall back to the probablistic classifier.
# Lastly, fall back to the probabilistic classifier.
elsif classified = Classifier.classify(Samples::DATA, data, possible_language_names).first
# Return the actual Language object based of the string language name (i.e., first element of `#classify`)
Language[classified[0]]

View File

@@ -119,7 +119,7 @@ ApacheConf:
Apex:
type: programming
lexer: Text only
lexer: Java
extensions:
- .cls
@@ -283,7 +283,7 @@ C#:
type: programming
ace_mode: csharp
search_term: csharp
color: "#5a25a2"
color: "#178600"
aliases:
- csharp
extensions:
@@ -756,6 +756,14 @@ Frege:
extensions:
- .fr
G-code:
type: data
lexer: Text only
extensions:
- .g
- .gco
- .gcode
Game Maker Language:
type: programming
color: "#8ad353"
@@ -785,6 +793,12 @@ GAS:
- .s
- .S
GDScript:
type: programming
lexer: Text only
extensions:
- .gd
GLSL:
group: C
type: programming
@@ -1792,6 +1806,7 @@ Processing:
Prolog:
type: programming
lexer: Logtalk
color: "#74283c"
extensions:
- .pl

View File

@@ -181,6 +181,9 @@
"Frege": [
".fr"
],
"G-code": [
".g"
],
"GAMS": [
".gms"
],
@@ -192,6 +195,9 @@
"GAS": [
".s"
],
"GDScript": [
".gd"
],
"GLSL": [
".fp",
".frag",
@@ -854,8 +860,8 @@
"exception.zep.php"
]
},
"tokens_total": 659559,
"languages_total": 908,
"tokens_total": 661949,
"languages_total": 916,
"tokens": {
"ABAP": {
"*/**": 1,
@@ -22836,6 +22842,69 @@
"newContentPane.setOpaque": 1,
"frame.setContentPane": 1
},
"G-code": {
";": 8,
"RepRapPro": 1,
"Ormerod": 1,
"Board": 1,
"test": 1,
"GCodes": 1,
"M111": 1,
"S1": 1,
"Debug": 1,
"on": 1,
"G21": 1,
"mm": 1,
"G90": 1,
"Absolute": 1,
"positioning": 1,
"M83": 1,
"Extrusion": 1,
"relative": 1,
"M906": 1,
"X800": 1,
"Y800": 1,
"Z800": 1,
"E800": 1,
"Motor": 1,
"currents": 1,
"(": 1,
"mA": 1,
")": 1,
"T0": 2,
"Extruder": 1,
"G1": 17,
"X50": 1,
"F500": 2,
"X0": 2,
"G4": 18,
"P500": 6,
"Y50": 1,
"Y0": 2,
"Z20": 1,
"F200": 2,
"Z0": 1,
"E20": 1,
"E": 1,
"-": 146,
"M106": 2,
"S255": 1,
"S0": 1,
"M105": 13,
"G10": 1,
"P0": 1,
"S100": 2,
"M140": 1,
"P5000": 12,
"M0": 2,
"e": 145,
"G28": 1,
"X55": 3,
"Y5": 3,
"F2000": 1,
"Y180": 2,
"X180": 2
},
"GAMS": {
"*Basic": 1,
"example": 2,
@@ -24207,6 +24276,391 @@
"xd": 1,
".subsections_via_symbols": 1
},
"GDScript": {
"extends": 4,
"BaseClass": 1,
"var": 86,
"a": 6,
"s": 4,
"arr": 1,
"[": 22,
"]": 22,
"dict": 1,
"{": 2,
"}": 2,
"const": 11,
"answer": 1,
"thename": 1,
"v2": 1,
"Vector2": 61,
"(": 314,
")": 313,
"v3": 1,
"Vector3": 9,
"func": 19,
"some_function": 1,
"param1": 4,
"param2": 5,
"local_var": 2,
"if": 56,
"<": 14,
"print": 6,
"elif": 4,
"else": 11,
"for": 9,
"i": 7,
"in": 12,
"range": 6,
"while": 1,
"-": 31,
"local_var2": 2,
"+": 24,
"return": 14,
"class": 1,
"Something": 1,
"_init": 1,
"lv": 10,
"Something.new": 1,
"lv.a": 1,
"Control": 1,
"score": 4,
"score_label": 2,
"null": 1,
"MAX_SHAPES": 2,
"block": 3,
"preload": 2,
"block_colors": 3,
"Color": 7,
"block_shapes": 4,
"#": 18,
"I": 1,
"O": 1,
"S": 1,
"Z": 1,
"L": 1,
"J": 1,
"T": 1,
"block_rotations": 2,
"Matrix32": 4,
"width": 5,
"height": 6,
"cells": 8,
"piece_active": 7,
"false": 16,
"piece_shape": 8,
"piece_pos": 3,
"piece_rot": 5,
"piece_cell_xform": 4,
"p": 2,
"er": 4,
"r": 2,
"%": 3,
".xform": 1,
"_draw": 1,
"sb": 2,
"get_stylebox": 1,
"use": 1,
"line": 1,
"edit": 1,
"bg": 1,
"draw_style_box": 1,
"Rect2": 5,
"get_size": 1,
".grow": 1,
"bs": 3,
"block.get_size": 1,
"y": 12,
"x": 12,
"draw_texture_rect": 2,
"*bs": 2,
"c": 6,
"piece_check_fit": 6,
"ofs": 2,
"pos": 4,
"pos.x": 2,
"pos.y": 2,
"true": 11,
"new_piece": 3,
"randi": 1,
"width/2": 1,
"piece_pos.y": 2,
"not": 5,
"#game": 1,
"over": 1,
"#print": 1,
"game_over": 2,
"update": 7,
"test_collapse_rows": 2,
"accum_down": 6,
"collapse": 3,
"cells.erase": 1,
"accum_down*100": 1,
"score_label.set_text": 2,
"str": 1,
"get_node": 24,
".set_text": 2,
"restart_pressed": 1,
"cells.clear": 1,
"piece_move_down": 2,
"piece_rotate": 2,
"adv": 2,
"_input": 1,
"ie": 1,
"ie.is_pressed": 1,
"ie.is_action": 4,
"piece_pos.x": 2,
"setup": 2,
"w": 3,
"h": 3,
"set_size": 1,
"*block.get_size": 1,
".start": 1,
"_ready": 3,
"Initalization": 2,
"here": 2,
"set_process_input": 1,
"RigidBody": 1,
"#var": 1,
"dir": 8,
"ANIM_FLOOR": 2,
"ANIM_AIR_UP": 2,
"ANIM_AIR_DOWN": 2,
"SHOOT_TIME": 2,
"SHOOT_SCALE": 2,
"CHAR_SCALE": 2,
"facing_dir": 2,
"movement_dir": 3,
"jumping": 5,
"turn_speed": 2,
"keep_jump_inertia": 2,
"air_idle_deaccel": 2,
"accel": 2,
"deaccel": 2,
"sharp_turn_threshhold": 2,
"max_speed": 5,
"on_floor": 3,
"prev_shoot": 3,
"last_floor_velocity": 5,
"shoot_blend": 7,
"adjust_facing": 3,
"p_facing": 4,
"p_target": 2,
"p_step": 2,
"p_adjust_rate": 2,
"current_gn": 2,
"n": 2,
"normal": 1,
"t": 2,
"n.cross": 1,
".normalized": 2,
"n.dot": 1,
"t.dot": 1,
"ang": 12,
"atan2": 1,
"abs": 1,
"too": 1,
"small": 1,
"sign": 1,
"*": 15,
"turn": 3,
"cos": 2,
"sin": 1,
"p_facing.length": 1,
"_integrate_forces": 1,
"state": 5,
"state.get_linear_velocity": 1,
"linear": 1,
"velocity": 3,
"g": 3,
"state.get_total_gravity": 1,
"delta": 8,
"state.get_step": 1,
"d": 2,
"delta*state.get_total_density": 1,
"<0):>": 2,
"d=": 1,
"apply": 1,
"gravity": 2,
"anim": 4,
"up": 12,
"normalized": 6,
"is": 1,
"against": 1,
"vv": 5,
"dot": 3,
"vertical": 1,
"hv": 8,
"horizontal": 3,
"hdir": 7,
"direction": 6,
"hspeed": 14,
"length": 1,
"speed": 2,
"floor_velocity": 5,
"onfloor": 6,
"get_contact_count": 2,
"0": 6,
"get_contact_local_shape": 1,
"1": 2,
"continue": 1,
"get_contact_collider_velocity_at_pos": 1,
"break": 1,
"where": 1,
"does": 1,
"the": 1,
"player": 1,
"intend": 1,
"to": 3,
"walk": 1,
"cam_xform": 5,
"target": 1,
"camera": 1,
"get_global_transform": 1,
"Input": 6,
"is_action_pressed": 6,
"move_forward": 1,
"basis": 5,
"2": 2,
"move_backwards": 1,
"move_left": 1,
"move_right": 1,
"jump_attempt": 2,
"jump": 2,
"shoot_attempt": 3,
"shoot": 1,
"target_dir": 5,
"sharp_turn": 2,
"and": 16,
"rad2deg": 1,
"acos": 1,
"target_dir.dot": 1,
"dir.length": 2,
"#linear_dir": 1,
"linear_h_velocity/linear_vel": 1,
"#if": 2,
"linear_vel": 1,
"brake_velocity_limit": 1,
"linear_dir.dot": 1,
"ctarget_dir": 1,
"Math": 1,
"deg2rad": 1,
"brake_angular_limit": 1,
"brake": 1,
"#else": 1,
"/hspeed*turn_speed": 1,
"accel*delta": 1,
"deaccel*delta": 1,
"hspeed=": 1,
"mesh_xform": 2,
"Armature": 2,
"get_transform": 1,
"facing_mesh=": 1,
"facing_mesh": 7,
"m3": 2,
"Matrix3": 1,
"cross": 1,
"scaled": 1,
"set_transform": 1,
"Transform": 1,
"origin": 1,
"7": 1,
"sfx": 1,
"play": 1,
"hs": 1,
"hv.length": 1,
"hv.normalized": 1,
"hdir*hspeed": 1,
"up*vv": 1,
"#lv": 1,
"pass": 2,
"state.set_linear_velocity": 1,
"bullet": 3,
".instance": 1,
"bullet.set_transform": 1,
".get_global_transform": 2,
".orthonormalized": 1,
"get_parent": 1,
".add_child": 1,
"bullet.set_linear_velocity": 1,
".basis": 1,
"PS.body_add_collision_exception": 1,
"bullet.get_rid": 1,
"get_rid": 1,
"#add": 1,
"it": 1,
".play": 1,
".blend2_node_set_amount": 2,
"/": 1,
".transition_node_set_current": 1,
"min": 1,
"state.set_angular_velocity": 1,
".set_active": 1,
"Node2D": 1,
"INITIAL_BALL_SPEED": 3,
"ball_speed": 2,
"screen_size": 2,
"#default": 1,
"ball": 3,
"pad_size": 4,
"PAD_SPEED": 1,
"_process": 1,
"get": 2,
"positio": 1,
"pad": 3,
"rectangles": 1,
"ball_pos": 8,
".get_pos": 5,
"left_rect": 1,
"pad_size*0.5": 2,
"right_rect": 1,
"#integrate": 1,
"new": 1,
"postion": 1,
"direction*ball_speed*delta": 1,
"#flip": 2,
"when": 2,
"touching": 2,
"roof": 1,
"or": 4,
"floor": 1,
"ball_pos.y": 1,
"direction.y": 5,
"<0)>": 1,
"screen_size.y": 3,
"change": 1,
"increase": 1,
"pads": 1,
"left_rect.has_point": 1,
"direction.x": 4,
"right_rect.has_point": 1,
"ball_speed*": 1,
"randf": 1,
"*2.0": 1,
"direction.normalized": 1,
"#check": 1,
"gameover": 1,
"ball_pos.x": 1,
"<0>": 1,
"screen_size.x": 1,
"screen_size*0.5": 1,
".set_pos": 3,
"#move": 2,
"left": 1,
"left_pos": 2,
"left_pos.y": 4,
"Input.is_action_pressed": 4,
"PAD_SPEED*delta": 4,
"right": 1,
"right_pos": 2,
"right_pos.y": 4,
"get_viewport_rect": 1,
".size": 1,
"actual": 1,
"size": 1,
".get_texture": 1,
".get_size": 1,
"set_process": 1
},
"GLSL": {
"////": 4,
"High": 1,
@@ -73160,9 +73614,11 @@
"Erlang": 2928,
"Forth": 1516,
"Frege": 5564,
"G-code": 432,
"GAMS": 363,
"GAP": 9944,
"GAS": 133,
"GDScript": 1958,
"GLSL": 4076,
"Game Maker Language": 13310,
"Gnuplot": 1023,
@@ -73375,9 +73831,11 @@
"Erlang": 5,
"Forth": 7,
"Frege": 4,
"G-code": 4,
"GAMS": 1,
"GAP": 7,
"GAS": 1,
"GDScript": 4,
"GLSL": 7,
"Game Maker Language": 13,
"Gnuplot": 6,
@@ -73538,5 +73996,5 @@
"fish": 3,
"wisp": 1
},
"md5": "1a8591982ec28c592a742122734c142c"
"md5": "c4591bd0be052bcbb2ab31e06fe4c730"
}

57
samples/G-code/duettest.g Normal file
View File

@@ -0,0 +1,57 @@
; RepRapPro Ormerod
; Board test GCodes
M111 S1; Debug on
G21 ; mm
G90 ; Absolute positioning
M83 ; Extrusion relative
M906 X800 Y800 Z800 E800 ; Motor currents (mA)
T0 ; Extruder 0
G1 X50 F500
G1 X0
G4 P500
G1 Y50 F500
G1 Y0
G4 P500
G1 Z20 F200
G1 Z0
G4 P500
G1 E20 F200
G1 E-20
G4 P500
M106 S255
G4 P500
M106 S0
G4 P500
M105
G10 P0 S100
T0
M140 S100
G4 P5000
M105
G4 P5000
M105
G4 P5000
M105
G4 P5000
M105
G4 P5000
M105
G4 P5000
M105
G4 P5000
M105
G4 P5000
M105
G4 P5000
M105
G4 P5000
M105
G4 P5000
M105
G4 P5000
M105
M0

25912
samples/G-code/lm.g Normal file

File diff suppressed because it is too large Load Diff

29735
samples/G-code/rm.g Normal file

File diff suppressed because it is too large Load Diff

13
samples/G-code/square.g Normal file
View File

@@ -0,0 +1,13 @@
G28 X0 Y0
G1 X55 Y5 F2000
G1 Y180
G1 X180
G1 Y5
G1 X55
G1 Y180
G1 X180
G1 Y5
G1 X55
M0

View 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
View 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
View 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
View 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)