Making Samples executable. #!/usr/bin/env io and chmod a+x *.io
[io/jrb1.git] / addons / OpenGL / samples / game / client.io
blob7768cd10c9f8548d2dffa45876a84700235f6641
1 #!/usr/bin/env io
4 doFile(Path with(launchPath, "Tank.io"))
6 systemfont := Font clone open("Library/Fonts/Free/FreeSans.ttf") setPointSize(14)
8 GameClient := Object clone
9 GameClient parent := OpenGL
10 GameClient objects := Map clone
11 GameClient player := nil
12 GameClient clientId := "nil"
13 GameClient message := "running client"
15 GameClient reshape := method(w, h,
16 self width := w
17 self height := h
18 glViewport(0, 0, w, h)
19 glMatrixMode(GL_PROJECTION)
20 glLoadIdentity
21 gluOrtho2D(0, w, 0, h)
22 glMatrixMode(GL_MODELVIEW)
23 glLoadIdentity
24 glutPostRedisplay
27 GameClient display := method(
28 glClearColor(.1,.6,.1,1)
29 glClearColor(0,0,0,1)
30 glClear(GL_COLOR_BUFFER_BIT)
31 glLoadIdentity
32 glPushMatrix
33 objects foreach(i, object, object draw)
35 glColor4d(1,1,1,1)
36 glTranslated(10,10,0)
37 systemfont drawString(message)
38 glPopMatrix
39 glFlush
40 glutSwapBuffers
43 GameClient keyboard := method(key, x, y,
44 k := key asCharacter
45 self message := "key := " .. k
46 self serverConnection send("k", k)
49 GameClient motion := method(x, y, nil)
50 GameClient passiveMotion := method(x, y, nil)
51 GameClient mouse := method(button, state, x, y, nil)
53 GameClient timer := method(v,
54 glutTimerFunc(10, 0)
55 yield
56 self display
59 GameClient run := method(
60 glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA )
61 glutInitWindowSize(512, 512)
62 glutInit
63 glutCreateWindow("Multiplayer Game")
64 glutEventTarget(GameClient)
65 glutDisplayFunc
66 glutKeyboardFunc
67 glutMotionFunc
68 glutMouseFunc
69 glutReshapeFunc
70 glutPassiveMotionFunc
71 glutTimerFunc(100, 0)
73 glEnable(GL_LINE_SMOOTH)
74 glEnable(GL_BLEND)
75 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
76 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST)
77 glLineWidth(1)
79 GameClient connect
81 glutMainLoop
84 // --- Networking -----------------------------------
86 GameClient connect := method(
87 self serverConnection := ServerConnection setDelegate(self)
88 self serverConnection setup
89 self serverConnection @run
92 // --- Delegate methods -----------------------------------
94 GameClient setClientId := method(id,
95 write("i ", id, "\n")
96 self clientId := id
99 GameClient add := method(id, type,
100 write("add ", id, " '", type, "'\n")
101 objects atPut(id, getSlot(type) clone)
104 GameClient remove := method(id,
105 write("remove ", id, "\n")
106 objects remove(id)
109 GameClient update := method(id, x, y, angle,
110 write("update ", id, "\n")
111 obj := objects at(id)
112 if (obj) then(
113 obj position x <- x
114 obj position y <- y
115 obj angle <- angle
116 ) else (
117 write("no object with id '", id, "' found for update\n")
121 // --- ServerConnection -----------------------------------
123 ServerConnection := Object clone
124 ServerConnection setDelegate := method(d, self delegate := d; self)
125 ServerConnection setup := method(
126 ip := if(args at(0), args at(0), "67.118.104.220")
127 port := if(args at(1), args at(1), 8888)
128 self socket := Socket clone setHost(ip) setPort(port) connect
129 if (socket == nil, write("Unable to connect to ", ip, " on port ", port, "\n"); exit)
132 ServerConnection run := method(
133 while (socket isOpen,
134 if(socket read,
135 text := socket readBuffer asString
136 //Object write("got: " .. text .. "\n")
137 p := text splitNoEmpties(" ")
138 action := p at(0)
139 if (action == "i", delegate setClientId(p at(1)) )
140 if (action == "a", delegate append(p at(1), p at(2)) )
141 if (action == "u",
142 delegate update(p at(1), p at(2) asNumber, p at(3) asNumber, p at(4) asNumber)
144 if (action == "r", delegate remove(p at(1)) )
146 socket readBuffer empty
148 exit
151 ServerConnection send := method(action, value,
152 s := action .. " " .. value .. " "
153 write("send: " , s, "\n")
154 socket write(s)
157 GameClient run