diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/ecs.moon | 27 | ||||
| -rw-r--r-- | src/main.lua | 17 | ||||
| -rw-r--r-- | src/net.moon | 6 | ||||
| -rw-r--r-- | src/router.moon | 16 | ||||
| -rw-r--r-- | src/shaders/stars.lua | 2 | ||||
| -rw-r--r-- | src/shaders/world.frag | 1 | ||||
| -rw-r--r-- | src/shaders/world.moon | 138 | ||||
| -rw-r--r-- | src/shaders/world.vert | 10 | ||||
| -rw-r--r-- | src/ui.moon | 5 | ||||
| -rw-r--r-- | src/ui/textbox.moon | 2 | ||||
| -rw-r--r-- | src/util.lua | 12 | ||||
| -rw-r--r-- | src/window.moon | 1 | ||||
| -rw-r--r-- | src/world.moon | 4 | ||||
| -rw-r--r-- | src/world_test.moon | 77 |
14 files changed, 286 insertions, 32 deletions
diff --git a/src/ecs.moon b/src/ecs.moon index 4fdecc9..d20984a 100644 --- a/src/ecs.moon +++ b/src/ecs.moon @@ -29,7 +29,7 @@ class Entity --Entity is responsible for the component -> entity link @components = componenets or {} - for name, component in pairs(components) + for name, component in pairs(@components) component.entity = @ @c_by_type[component.__class] = @c_by_type[component.__class] or {} @c_by_type[component.__class][name] = component @@ -43,12 +43,15 @@ class Entity cid += 1 assert(@components[cid] == nil, "Already had a component with id" .. tostring(cid)) @components[cid] = component + @c_by_type[component.__class][cid] = component @ remove: (cid) => component = @components[cid] component.entity = nil component\leave(@) component + @components[cid] = nil + @c_by_type[component.__class][cid] = nil get: (cid) => @components[cid] @@ -74,7 +77,23 @@ class PredictedComponent extends Component class GraphicsComponent extends Component new: (name, properties) => - assert(properties.node , "Failed to find node for graphics component") + assert(properties and properties.node , "Failed to find node for graphics component") + super(name, properties) + static: () => + @@static + buffer_size: () => + error("Subclasses of GraphicsComponent must implement a buffer_size() method") + populate_buffer: (buffer, offset) => + error("Subclasses of GraphicsComponent must implement a populate_buffer() method") + +class PhysicsComponent extends Component + new: (name, properties) => + assert(properties and properties.shape, "Failed to find a shape for physics component") + super(name, properties) + +class ScriptComponent extends Component + new: (name, properties) => + assert(properties and properties.script, "Failed to find script name for script component") super(name, properties) { @@ -83,6 +102,6 @@ class GraphicsComponent extends Component NetworkedComponent: NetworkedComponent PredictedComponent: PredictedComponent GraphicsComponent: GraphicsComponent -} - + PhysicsComponent: PhysicsComponent + ScriptComponent: ScriptComponent } diff --git a/src/main.lua b/src/main.lua index 9f55c43..406c528 100644 --- a/src/main.lua +++ b/src/main.lua @@ -1,17 +1,26 @@ require("preload") local win = require("window") ---local stars = require("shaders.stars") +local pp = am.postprocess({ + clear_color = vec4(0.2, 0.2, 0.3, 1), + depth_buffer = true +}) +local stars = require("shaders.stars") +pp:append(stars) --win.scene:append(stars) ---world_shader = require("shaders.world") + +local world_shader = require("shaders.world") --print("World shader:",world_shader) --win.scene:append(world_shader.node) +pp:append(world_shader.node) +win.scene:append(pp) + local ui = require("ui") win.scene:append(ui.node) ---require("world_test") +require("world_test") --require("net_test") --require("ui_test") --require("router_test") -require("controller_test") +--require("controller_test") require("log").observe(function(chunk) if chunk.tags.ui then diff --git a/src/net.moon b/src/net.moon index acd75c7..3d2a1cb 100644 --- a/src/net.moon +++ b/src/net.moon @@ -188,7 +188,8 @@ rewrite_events = { net.pump = () -> msg_ = net.pull_peers! - log.info("Processing " .. tostring(#msg_) .. " peer messages", {"net"}) + if #msg_ > 0 + log.info("Processing " .. tostring(#msg_) .. " peer messages", {"net"}) for message in *msg_ log.info(tostring(message), {"net", message.data.peer}) if rewrite_events[message.data.e] @@ -203,7 +204,8 @@ net.pump = () -> assert(callback, "Failed to find callback " .. message.message .. " on peer " .. message.data.peer) callback(peer,message.data) msg_ = net.pull_connections! - log.info("Processing " .. tostring(#msg_) .. " connection messages", {"net"}) + if #msg_ > 0 + log.info("Processing " .. tostring(#msg_) .. " connection messages", {"net"}) for message in *msg_ log.info(tostring(message), {"net", message.data.peer}) connection = Connection\get(message.dest, message.peer) diff --git a/src/router.moon b/src/router.moon index 7ef9bfc..e414f07 100644 --- a/src/router.moon +++ b/src/router.moon @@ -72,7 +72,7 @@ net.register_message("Simplify",{required:{ --Testing net.register_message("Raw",{optional:{s:"string"}}) - +router_singleton = nil class Queue new: () => @queue = {} @@ -86,15 +86,17 @@ class Router @peerlist = {} @routes = {} - -- "uninitalized", "peer", "candidate", "elected" - @state = "unitialized" + -- "uninitialized", "peer", "candidate", "elected" + @state = "uninitialized" -- [peerid] = "votes for peerid" + @nonce = 0 @prevotes = {} @term = 0 -- The uncommited queue @uncommited = Queue! + router_singleton = @ initalize: (id) => @set_route("RequestClusterInfo",(conn, message) => @@ -242,4 +244,10 @@ class Router else log.warn("No message callback registered for format " .. msgfmt .. " routes are: " .. tostring(@routes), {"net"}) -{:Router} +node = am.group! +node\update(() -> + if router_singleton and router_singleton.state ~= "uninitalized" + net.pump! + coroutine.yield! +) +{:Router, :node} diff --git a/src/shaders/stars.lua b/src/shaders/stars.lua index 111bef8..8d54b16 100644 --- a/src/shaders/stars.lua +++ b/src/shaders/stars.lua @@ -41,7 +41,7 @@ local node = am.use_program(am.program([[ void main() { float world_x_off = mod(world_x, world_x_period); float world_y_off = mod(world_y, world_y_period); - gl_Position = P * MV * vec4(stars.x - world_x_off, stars.y - world_y_off, 0.0, 1.0); + gl_Position = P * MV * vec4(stars.x - world_x_off, stars.y - world_y_off, 0., 1.0); float intensity = sin(stars.z + time) * cos(time) + 1.; gl_PointSize = pow(intensity, 2.) * stars.z * 0.3; } diff --git a/src/shaders/world.frag b/src/shaders/world.frag index ded8abb..15d7b27 100644 --- a/src/shaders/world.frag +++ b/src/shaders/world.frag @@ -1,5 +1,6 @@ precision mediump float; varying vec2 textureuv; // uv +varying float radius; uniform sampler2D textures; uniform sampler2D emissives; uniform sampler2D normals; diff --git a/src/shaders/world.moon b/src/shaders/world.moon index ad40cb9..e9ddcc9 100644 --- a/src/shaders/world.moon +++ b/src/shaders/world.moon @@ -3,10 +3,9 @@ color = require("color") world = require("world") sprites = require("world.sprites") shader_shim = require("shader_shim") -hc = require("party.hardoncollider.init") +hc = require("party.hc.init") -- Process the world into buffers to send to the shader -error("Who is including world?") print("sprites:",sprites,getmetatable(sprites)) print("sprites.floor1_diffuse",sprites["floor1_diffuse"]) view_angle = math.pi / 4 @@ -26,6 +25,9 @@ p_mv = mat4( 0, 0,(-far_plane * near_plane)/(far_plane - near_plane), 0 ) +sd = sprites.floor1_diffuse +w1 = sprites.wall1_diffuse + -- Each point needs: -- vec3 position (x,y,z) -- vec2 (u,v) @@ -57,8 +59,84 @@ up_down_hall = (x,y) -> } r -sd = sprites.floor1_diffuse -w1 = sprites.wall1_diffuse +room = (x,y,w,h,left_holes,right_holes,top_holes,bottom_holes) -> + left_holes\sort() + right_holes\sort() + top_holes\sort() + bottom_holes\sort() + r = { + --floor + vec3(x,y,0) + vec3(x+w,y,0), + vec3(x+w,y-h,0), + vec3(x+w,y-h,0), + vec3(x,y-h,0), + vec3(x,y,0), + + --left wall + } + r + +barrel = (x,y,w,h) -> + tris = 18 + rad = (w/2) + l = x - (w/2) + j = x + (w/2) + t = h + b = 0 + f = y - (w/2) + n = y + (w/2) + r = { + --top + vec3(l,f,h), + vec3(l,n,h), + vec3(j,n,h), + vec3(j,n,h), + vec3(j,f,h), + vec3(l,f,h), + } + step = (2*math.pi)/tris + for i = 0,2*math.pi,step + r[#r+1] =vec3(x + math.cos(i)*rad,y + math.sin(i)*n,h) + r[#r+1] =vec3(x + math.cos(i+step)*rad,math.sin(i+step)*n,h) + r[#r+1] =vec3(x + math.cos(i+step)*rad,math.sin(i+step)*n,0) + r[#r+1] =vec3(x + math.cos(i+step)*rad,math.sin(i+step)*n,0) + r[#r+1] =vec3(x + math.cos(i)*rad,math.sin(i)*n,0) + r[#r+1] =vec3(x + math.cos(i)*rad,math.sin(i)*n,h) + r + +barrel_uv = (x,y,w,h) -> + tris = 18 + r = { + vec4(w1.s1,w1.t1,1,1), + vec4(w1.s1,w1.t2,1,1), + vec4(w1.s2,w1.t2,1,1), + vec4(w1.s2,w1.t2,1,1), + vec4(w1.s2,w1.t1,1,1), + vec4(w1.s1,w1.t1,1,1), + } + step = (2*math.pi)/tris + for i = 0,2*math.pi,step + perc = (i / (2*math.pi)) + nextperc = ((i+step) / (2*math.pi)) + srange = w1.s2 - w1.s1 + trange = w1.t2 - w1.t1 + sstart = w1.s1 + (srange * perc) + send = w1.s1 + (srange * nextperc) + tstart = w1.t1 + tend = w1.t2 + r[#r+1] = vec4(sstart ,tstart,1,1) + r[#r+1] = vec4(sstart ,tend,1,1) + r[#r+1] = vec4(send ,tend,1,1) + r[#r+1] = vec4(send ,tend,1,1) + r[#r+1] = vec4(send ,tstart,1,1) + r[#r+1] = vec4(sstart ,tstart,1,1) + r + +barrel_r = (x,y,w,h) -> + r = {-1,-1,1,1,1,-1,0,0,0,0,0,0} + r + -- uvs are s,t,smult, tmult up_down_hall_uv = (x,y) -> @@ -88,31 +166,73 @@ up_down_hall_uv = (x,y) -> } r +up_down_hall_r = (x,y) -> + r = { + 0,0,0,0,0,0 + 0,0,0,0,0,0 + 0,0,0,0,0,0 + 0,0,0,0,0,0 + 0,0,0,0,0,0 + 0,0,0,0,0,0 + } + r + +-- Barrel? + + add_verts = (tbl, new) -> for i = 1, #new tbl[#tbl+1] = new[i] world_geom = {} world_uv = {} -add_verts(world_geom, up_down_hall(0,0)) +world_r = {} +add_verts(world_geom, up_down_hall(0,-1)) add_verts(world_uv, up_down_hall_uv(0,0)) +add_verts(world_r, up_down_hall_r(0,0)) add_verts(world_geom, up_down_hall(0,1)) add_verts(world_uv, up_down_hall_uv(0,0)) -add_verts(world_geom, up_down_hall(0,-1)) +add_verts(world_r, up_down_hall_r(0,0)) +add_verts(world_geom, up_down_hall(0,0)) add_verts(world_uv, up_down_hall_uv(0,0)) +add_verts(world_r, up_down_hall_r(0,0)) +add_verts(world_geom, barrel(-1,0,0.5,0.5)) +add_verts(world_uv, barrel_uv(-1,0,0.5,0.5)) +add_verts(world_r, barrel_r(-1,0,0.5,0.5)) +--add_verts(world_geom, barrel(0.5,0.5,0.5,0.5)) +--add_verts(world_uv, barrel_uv(0.5,0.5,0.5,0.5)) +--add_verts(world_r, barrel_r(0.5,0.5,0.5,0.5)) --sprites["diffuse"].texture.wrap = "repeat" --sprites["normals"].texture.wrap = "repeat" -node = shader_shim.world\append(am.cull_face("front")\append(am.bind({ + +test_world = { + vec3(0,0,0), + vec3(1,0,0), + vec3(0,1,0) +} +test_uvs = { + vec4(sd.s1,sd.t1,1,1), + vec4(sd.s2,sd.t1,1,1), + vec4(sd.s2,sd.t2,1,1), +} +test_r = { + 0,0,0 +} +node = shader_shim.world\append(am.depth_test("less")\append(am.cull_face("front")\append(am.bind({ -- should cull front MV: s_mv P: mat4(1) color: color.am_color.highlight, world_x: 0, world_y: 0, world: am.vec3_array(world_geom) + texuv: am.vec4_array(world_uv) + r: am.float_array(world_r) + --world:am.vec3_array(test_world) + --texuv: am.vec4_array(test_uvs) + --r: am.float_array(test_r) time: am.current_time(), textures: sprites.floor1_diffuse.texture - texuv: am.vec4_array(world_uv) -})\append(am.draw("triangles")))) +})\append(am.draw("triangles"))))) node\action(() => bind = self("bind") bind.time = am.current_time! diff --git a/src/shaders/world.vert b/src/shaders/world.vert index 0e6747a..42276fe 100644 --- a/src/shaders/world.vert +++ b/src/shaders/world.vert @@ -1,7 +1,9 @@ precision highp float; attribute vec3 world; // position attribute vec2 texuv; +attribute float r; // for round objects, 0 for non-round varying vec2 textureuv; +varying float radius; varying mat3 light1; uniform vec4 color; varying vec4 v_color; @@ -13,10 +15,12 @@ void main() { v_color = vec4(world.xyz,1.); vec2 vxy = vec2(world.x - world_x, world.y - world_y); float z_scale = 0.5; - float xoff = world.z * vxy.x * z_scale; - float yoff = world.z * vxy.y * z_scale; + float xoff = clamp(world.z * vxy.x * z_scale, -32., 32.); + float yoff = clamp(world.z * vxy.y * z_scale, -32., 32.); textureuv=texuv; + //radius = r; // if z > 0 then // xoff = ceil(xoff, 0) - gl_Position = P * MV * vec4(vxy.x + xoff, vxy.y + yoff, 0., 1.0); + // add to the z coord so we don't intersect with the ui + gl_Position = P * MV * vec4(vxy.x + xoff, vxy.y + yoff, -world.z -1., 1.0); } diff --git a/src/ui.moon b/src/ui.moon index 4746302..4c551b3 100644 --- a/src/ui.moon +++ b/src/ui.moon @@ -1,4 +1,4 @@ -hc = require("party.hardoncollider.init") +hc = require("party.hc.init") win = require("window") log = require("log") Button = require("ui.button") @@ -48,7 +48,7 @@ ui.node = am.group! has_fire = (obj) -> assert(obj.fire, obj.__class.__name .. " doesn't have a .fire method") -ui.dbg = am.translate(0,0)\append(am.circle(vec2(0,0),5,vec4(0,0,0,1))) +ui.dbg = am.translate(0,0)\append(am.circle(vec2(0,0),5,vec4(0,0,0,1)))\append(am.text("Hello, world!")) ui.node\append(ui.dbg) ui.node\action(() -> @@ -88,6 +88,7 @@ ui.node\action(() -> has_fire(uiobj) uiobj\fire(etbl) if #keys > 0 + print("Got keys:" .. tostring(keys)) etbl = event: "keys_pressed" data: keys diff --git a/src/ui/textbox.moon b/src/ui/textbox.moon index c1bd521..c029ba4 100644 --- a/src/ui/textbox.moon +++ b/src/ui/textbox.moon @@ -27,6 +27,7 @@ class Textbox extends Button for i = 0,9 @valid_chars[tostring(i)] = tostring(i) @valid_chars["kp_" .. tostring(i)] = tostring(i) + @valid_chars["KP_" .. tostring(i)] = tostring(i) @valid_chars.kp_divide = "/" @valid_chars.kp_multiply = "*" @valid_chars.kp_minus = "-" @@ -73,6 +74,7 @@ class Textbox extends Button if add_key t = @text.text for key in *e.data + print("Examining key:", key) if key == "delete" or key == "backspace" @cursor_pos -=1 if @cursor_pos < 0 diff --git a/src/util.lua b/src/util.lua index 52597b2..f0192dc 100644 --- a/src/util.lua +++ b/src/util.lua @@ -90,4 +90,16 @@ function util.reverse(tbl, val) return ret end +function util.typecheck(tbl, ...) + local args = {...} + assert(#args % 2 == 0,"Typecheck should have an odd number of arguments, found " .. tostring(#args + 1) .. ".") + for i = 1, #args, 2 do + assert(args[i] and type(args[i]) == "string", "Cannot check a field of type " .. type(args[i]) .. " at position " .. tostring(i + 1) .. ".") + assert(tbl[args[i]], "Failed to find a field: " .. args[i]) + assert(args[i+1] and type(args[i + 1]) == "string", "Cannot check for a type " .. type(args[i + 1]) .. " at position " .. tostring(i + 2) .. ".") + assert(type(tbl[args[i]]) == args[i+1], "Expected a " .. args[i+1] .. " at position " .. tostring(i+2) .. " but found a " .. type(tbl[args[i]])) + end + return true +end + return util diff --git a/src/window.moon b/src/window.moon index e3ec8ba..8aa8f2a 100644 --- a/src/window.moon +++ b/src/window.moon @@ -5,6 +5,7 @@ win = am.window{ width: 1280 height: 720 clear_color: vec4(0.2, 0.2, 0.3, 1) + --depth_buffer: true } win.scene = am.group! win diff --git a/src/world.moon b/src/world.moon index 3e4e09a..2862a97 100644 --- a/src/world.moon +++ b/src/world.moon @@ -1,8 +1,10 @@ -- Global state win = require("window") -hc = require("party.hardoncollider.init") +hc = require("party.hc.init") ecs = require("ecs") + +print("hc:", hc) --Use a collider to decide what to render x = { world_e: ecs.Entity(1) diff --git a/src/world_test.moon b/src/world_test.moon index 7ba6eb8..8fa9da5 100644 --- a/src/world_test.moon +++ b/src/world_test.moon @@ -1,7 +1,42 @@ shader = require("shaders.world") win = require("window") -hc = require("party.hardoncollider.init") +hc = require("party.hc.init") +ecs = require("ecs") world = require("world") +ss = require("shader_shim") +ui = require("ui") +peer = require("router") + +server = ui.button(-200,0,100,100, "Server") +co = nil +server.on = () => + if @co == nil + print("Setting co...") + @co = coroutine.create(() -> + router = peer.Router! + router\initalize! + router + ) + button = @ + @node\action(() => + if button.co and coroutine.status(button.co) ~= "dead" + succ, val = coroutine.resume(button.co) + if not succ + error(debug.traceback(button.co,val)) + if type(val) == "string" + button.text.text = val + else + router = val + + world.router = router + ) + +client.on = () => + +textbox = ui.textbox(200,100,100,32, "Serverid") +client = ui.button(200,0,100,100, "Client") +test_sprite = am.translate(0,0) ^ am.scale(20) ^ am.sprite("BRY\nYYY\nBBB") +ui.node\prepend(test_sprite) world_cache = { @@ -11,5 +46,43 @@ print("Before setting world level:", world) world.level = { graphics:{} entities:{} - graphic_world: hc\new(5) + graphic_world: hc.new(5) + physics_world: hc.new(1) } +world.level.entities[0] = ecs.Entity("World",{ + ecs.NetworkedComponent("net",{seed: "1234567"}), + ecs.GraphicsComponent("graphics",{ + node: ss.world\append( + am.depth_test("less")\append( + am.cull_face("front")\append( + am.bind({})\append( + am.draw("triangles") + )))) + }), + ecs.PhysicsComponent("physics",{ + shape: world.level.physics_world\rectangle(0,0,100,100) + }), + ecs.ScriptComponent("script",{script: "prefab.worldgen"}) +}) + +world.level.entities[1] = ecs.Entity("LocalPlayer",{ + ecs.NetworkedComponent({ + name: "" + position: {0,0} + velocity: {0,0} + acceleration: {0,0} + lasttime: 0 + }), + ecs.GraphicsComponent("graphics",{ + node: ss.world\append( + am.depth_test("less")\append( + am.cull_face("front")\append( + am.bind({})\append( + am.draw("triangles") + )))) + }), + ecs.PhysicsComponent("physics",{shape: world.level.physics_world\circle(0,0,0.5)}), + ecs.ScriptComponent("script",{script: "player"}) +}) + + |
