func main() { lexer := lex.Lexer{} parser := parse.Parser{} interpreter := interpret.New() interpreter.Logdebug = true for _, m := range lang.Matchers { lexer.AddMatcher(m) } interpreter.AddFunction("len", funcs.Functions["len"]) interpreter.AddFunction("print", funcs.Functions["print"]) interpreter.AddFunction("random", funcs.Functions["random"]) interpreter.AddFunction("append", funcs.Functions["append"]) interpreter.AddFunction("vector", funcs.Functions["vector"]) interpreter.AddFunction("fire", funcs.Functions["fire"]) prog1 := `i = 0; while (i < len(self.weapons)) { append(self.projectiles, fire(self.weapons[i])); i = i + 1; };` tokens, err := lexer.Lex(prog1) if err != nil { fmt.Println(err) } else { parser.Logdebug = true root, err := parser.Parse(tokens) if err != nil { fmt.Println(err) } else { parse.Tree(root) weapon := &types.WeaponSystem{Ammo: 10, RechargeTimeTotal: 5, RechargeTime: 0, Ready: false} weapon_wrapper := interpret.Convert(weapon) interpreter.SetArray("self.projectiles", []interpret.Object{}) interpreter.SetArray("self.weapons", []interpret.Object{weapon_wrapper}) interpreter.SetFloat64("total_elapsed", 36.0101130) interpreter.SetFloat64("elapsed", 33) err = interpreter.Block(root) fmt.Println(err) if err != nil { fmt.Println(err) } else { arr, _ := interpreter.GetArray("self.projectiles") fmt.Println(arr[0]) //fmt.Println(interpreter.GetFloat64("x")) //fmt.Println(interpreter.GetFloat64("y")) //fmt.Println(interpreter.GetFloat64("z")) } } } }
func main() { var upgrader = websocket.Upgrader{ ReadBufferSize: 1024, WriteBufferSize: 1024, } lexer := lex.Lexer{} for _, m := range lang.Matchers { lexer.AddMatcher(m) } prog1 := ` vec = vector(self.position.x, self.position.y, other.position.x, other.position.y); self.speed = 0; self.heading.x = vec.x; self.heading.y = vec.y; if (total_elapsed > 10) { self.speed = 55; i = 0; while (i < len(self.weapons)) { append(self.projectiles, fire(self.weapons[i])); i = i + 1; } }; ` prog2 := `self.speed = 55; if (total_elapsed == 0) { self.heading.x = random(); self.heading.y = random(); }; if (total_elapsed % 7 < 1) { self.heading.x = random(); self.heading.y = random(); };` tokens, _ := lexer.Lex(prog1) parser1 := parse.Parser{} program1, _ := parser1.Parse(tokens) tokens, _ = lexer.Lex(prog2) parser2 := parse.Parser{} program2, err := parser2.Parse(tokens) fmt.Println(err) interpreter := interpret.New() interpreter.AddFunction("print", funcs.Functions["print"]) interpreter.AddFunction("random", funcs.Functions["random"]) interpreter.AddFunction("vector", funcs.Functions["vector"]) player1 := types.NewPlayer(program1, interpreter) player2 := types.NewPlayer(program2, interpreter) http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("/var/www/game/public/")))) http.HandleFunc("/game", func(w http.ResponseWriter, r *http.Request) { conn, _ := upgrader.Upgrade(w, r, nil) go readLoop(conn) var timeSinceUpdate time.Duration = 0 var totalElapsedTime time.Duration = 0 for { start := time.Now() data, _ := json.Marshal([]types.Entity{*player1.Entity, *player2.Entity}) if timeSinceUpdate/time.Millisecond >= 50 { if err := conn.WriteMessage(websocket.TextMessage, data); err != nil { return } timeSinceUpdate = 0 } elapsed := time.Since(start) player1.Update(player2, totalElapsedTime.Seconds(), elapsed.Seconds()) player2.Update(player1, totalElapsedTime.Seconds(), elapsed.Seconds()) timeSinceUpdate += elapsed totalElapsedTime += elapsed } }) http.ListenAndServe(":9090", nil) }