-- Version 1.00 - models to be loaded in -- path definition prefix = "/home/cs528/julian/proj2" -- prefix = "/Users/julian/Courses/CS528/proj2" -- datapath = "/Users/julian/Courses/CS528/proj2/models" datapath = "/home/cs528/julian/proj2/models" -- datapath = "/root/cric/proj2/models" oggpath = prefix.."/oggs" SIMULATOR = 0 CWALL_Y_OFFSET = 8 DEBUG = 1 -- model files stumpfile = datapath.."/newstump.obj" bailfile = datapath.."/newbail.obj" ballfile = datapath.."/ball.obj" batfile = datapath.."/newbat.obj" -- center at handle oldbatfile = datapath.."/bat.obj" -- center at center rampfile = datapath.."/ramp.obj" stadfile = datapath.."/stad3.obj" skyfile = datapath.."/sky.obj" floorfile = datapath.."/checker.obj" -- some sound files pitchsound = oggpath.."/pitch.ogg" batsound = oggpath.."/bat.ogg" crowdsound = oggpath.."/crowd.ogg" stumpsound = oggpath.."/stump.ogg" ouchsound = oggpath.."/ouch.ogg" -- some image files alphaimage = prefix.."/alpha.png" titleimage = prefix.."/title.png" minimapimage = prefix.."/minimap.png" camera_posimage = prefix.."/camera_pos.png" ball_posimage = prefix.."/ball_pos.png" bluedigitimage = prefix.."/blue_digit.png" reddigitimage = prefix.."/red_digit.png" ----------------------------- -- camera movement modifiers ----------------------------- -- cdt_x=0 -- cdt_y=0 -- cdt_z=0 -- cx,cy,cz=0 -- nav_x=0 -- nav_y=0 -- nav_z=0 MOV_RATE=10 hitcounter = 0 ballhit = false -- FIXME ----------------------------- -- bat movement physics ----------------------------- bat_disp = 0 bat_velo = 0 bat_acce = 0 bat_start = false bat_stop = false ----------------------------- -- FIXME julian's modifiers ----------------------------- degToRad = 3.14159 / 180.0 radToDeg = 180.0 / 3.14159 scene = nil limbo = nil mouse_x = 0 mouse_y = 0 JOYSTICK_MIN = 0.1 rot_x = 0 rot_y = 0 pan_x = 0 pan_y = 0 pan_z = 0 hand_pan_x = 0; hand_pan_y = 0; hand_pan_z = 0; DEFAULT_CAM_X = 0 -- default position of the camera DEFAULT_CAM_Y = CWALL_Y_OFFSET -- 4+ DEFAULT_CAM_Z = 40 -- 30 DEFAULT_HAND_OFFSET_X = 1.0 -- 0.2 -- default offset of a non-tracked hand DEFAULT_HAND_OFFSET_Y = -0.2 DEFAULT_HAND_OFFSET_Z = 2.0 --0 DEFAULT_LIGHT_X = 1 -- 0 DEFAULT_LIGHT_Y = 8 -- 10 DEFAULT_LIGHT_Z = 8 -- 32 camera_turnY = 0 handOffsetX = 0 handOffsetY = 0 handOffsetZ = 0 -- status variable to keep current status -- 0 : init, title -- 1 : ready status -- 2 : browse mode -- 3: bat mode current_state = 0 total_states = 4 ------------------------------------- Overlay stuff ----------------------------------- viewport = { } overlay = { } initials = { 'V', 'R', 'C' } hints = { 'H', 'M', 'S' } hit = 0 miss = 0 score = 0 function make_sprite(filename) local image = E.create_image(filename) local brush = E.create_brush() E.set_brush_flags(brush, E.brush_flag_transparent, true) E.set_brush_flags(brush, E.brush_flag_unlit, true) E.set_brush_color(brush, 1.0, 1.0, 1.0, 1.0) E.set_brush_image(brush, image) return E.create_sprite(brush) end function create_overlay(filename, scale, hidden) sprite = make_sprite(filename) -- Add the new overlay sprite to the scene, hidden if requested. E.parent_entity (sprite, scene2d) E.set_entity_flags(sprite, E.entity_flag_hidden, hidden) -- Marginal hack: the aspect ratio of the alphabet sprite is 2:1. if filename == alphaimage then E.set_entity_scale(sprite, scale / 2, scale, scale) else E.set_entity_scale(sprite, scale, scale, scale) end return sprite end -- Initialize 2D on-screen overlays function init_overlay() local w = viewport.w overlay.title = create_overlay(titleimage, 1.00, false) overlay.minimap = create_overlay(minimapimage, 1.00, false) E.set_entity_position(overlay.minimap, viewport.L + 64, viewport.T - 64, 0) overlay.camera_pos = create_overlay(camera_posimage, 1.00, false) E.set_entity_position(overlay.camera_pos, viewport.L + 65, viewport.T - 80, 0) overlay.ball_pos = create_overlay(ball_posimage, 1.00, false) E.set_entity_position(overlay.ball_pos, viewport.L + 65, viewport.T - 40, 0) -- Create and position the high score player's initials display. -- overlay.high_inits = { } --- for i = 1, 3 do --- overlay.high_inits[i] = create_overlay("alpha.png", 0.125, false) --- E.set_entity_position(overlay.high_inits[i], viewport.L + 48 * i + 200, --- viewport.T - 32, 0) --- end overlay.hit = { } overlay.miss = { } overlay.score = { } overlay.hints = { } for i = 1, 3 do overlay.hints[i] = create_overlay(alphaimage, 0.125, false) -- overlay.hints[i] = create_overlay("alpha.png", 0.0625, false) E.set_entity_position(overlay.hints[i], viewport.R - 40 * 4 - 10, viewport.T - 32 - 54 * (i-1), 0) end -- Create and position the hit, miss and score overlays for i = 1, 3 do -- 3 digits are enough for a simple cricket game like this -- Number of hits overlay.hit[i] = create_overlay(bluedigitimage, 0.125, false) -- overlay.hit[i] = create_overlay("blue_digit.png", 0.0625, false) E.set_entity_position(overlay.hit[i], viewport.R - 40 * i, viewport.T - 32, 0) -- Number of misses overlay.miss[i] = create_overlay(reddigitimage, 0.125, false) -- overlay.miss[i] = create_overlay("red_digit.png", 0.0625, false) E.set_entity_position(overlay.miss[i], viewport.R - 40 * i, viewport.T - 32 - 54, 0) -- Score overlay.score[i] = create_overlay(bluedigitimage, 0.125, false) -- overlay.score[i] = create_overlay("blue_digit.png", 0.0625, false) E.set_entity_position(overlay.score[i], viewport.R - 40 * i, viewport.T - 32 - 54 - 54, 0) -- Placed in top left corner -- E.set_entity_position(overlay.high_score[i], viewport.L - 40 * i + 230, -- viewport.T - 32, 0) end end -- Overlay utility functions overlay_digit_map = { [0] = { 0.00, 0.25, 0.75, 1.00 }, [1] = { 0.25, 0.50, 0.75, 1.00 }, [2] = { 0.50, 0.75, 0.75, 1.00 }, [3] = { 0.75, 1.00, 0.75, 1.00 }, [4] = { 0.00, 0.25, 0.50, 0.75 }, [5] = { 0.25, 0.50, 0.50, 0.75 }, [6] = { 0.50, 0.75, 0.50, 0.75 }, [7] = { 0.75, 1.00, 0.50, 0.75 }, [8] = { 0.00, 0.25, 0.25, 0.50 }, [9] = { 0.25, 0.50, 0.25, 0.50 } } overlay_alpha_map = { ["A"] = { 0.000, 0.125, 0.75, 1.00 }, ["B"] = { 0.125, 0.250, 0.75, 1.00 }, ["C"] = { 0.250, 0.375, 0.75, 1.00 }, ["D"] = { 0.375, 0.500, 0.75, 1.00 }, ["E"] = { 0.500, 0.625, 0.75, 1.00 }, ["F"] = { 0.625, 0.750, 0.75, 1.00 }, ["G"] = { 0.750, 0.875, 0.75, 1.00 }, ["H"] = { 0.875, 1.000, 0.75, 1.00 }, ["I"] = { 0.000, 0.125, 0.50, 0.75 }, ["J"] = { 0.125, 0.250, 0.50, 0.75 }, ["K"] = { 0.250, 0.375, 0.50, 0.75 }, ["L"] = { 0.375, 0.500, 0.50, 0.75 }, ["M"] = { 0.500, 0.625, 0.50, 0.75 }, ["N"] = { 0.625, 0.750, 0.50, 0.75 }, ["O"] = { 0.750, 0.875, 0.50, 0.75 }, ["P"] = { 0.875, 1.000, 0.50, 0.75 }, ["Q"] = { 0.000, 0.125, 0.25, 0.50 }, ["R"] = { 0.125, 0.250, 0.25, 0.50 }, ["S"] = { 0.250, 0.375, 0.25, 0.50 }, ["T"] = { 0.375, 0.500, 0.25, 0.50 }, ["U"] = { 0.500, 0.625, 0.25, 0.50 }, ["V"] = { 0.625, 0.750, 0.25, 0.50 }, ["W"] = { 0.750, 0.875, 0.25, 0.50 }, ["X"] = { 0.875, 1.000, 0.25, 0.50 }, ["Y"] = { 0.000, 0.125, 0.00, 0.25 }, ["Z"] = { 0.125, 0.250, 0.00, 0.25 } } function set_overlay_digit(sprite, n) E.set_sprite_range(sprite, overlay_digit_map[n][1], overlay_digit_map[n][2], overlay_digit_map[n][3], overlay_digit_map[n][4]) end function set_overlay_alpha(sprite, c) E.set_sprite_range(sprite, overlay_alpha_map[c][1], overlay_alpha_map[c][2], overlay_alpha_map[c][3], overlay_alpha_map[c][4]) end function set_overlay_hit(n) local d1 = math.mod(n, 10) local d2 = math.mod(math.floor(n / 10), 10) local d3 = math.mod(math.floor(n / 100), 10) -- local d4 = math.mod(math.floor(n / 1000), 10) -- local d5 = math.mod(math.floor(n / 10000), 10) set_overlay_digit(overlay.hit[1], d1) set_overlay_digit(overlay.hit[2], d2) set_overlay_digit(overlay.hit[3], d3) -- set_overlay_digit(overlay.hit[4], d4) -- set_overlay_digit(overlay.hit[5], d5) end function set_overlay_miss(n) local d1 = math.mod(n, 10) local d2 = math.mod(math.floor(n / 10), 10) local d3 = math.mod(math.floor(n / 100), 10) -- local d4 = math.mod(math.floor(n / 1000), 10) -- local d5 = math.mod(math.floor(n / 10000), 10) set_overlay_digit(overlay.miss[1], d1) set_overlay_digit(overlay.miss[2], d2) set_overlay_digit(overlay.miss[3], d3) -- set_overlay_digit(overlay.miss[4], d4) -- set_overlay_digit(overlay.miss[5], d5) end function set_overlay_score(n) local d1 = math.mod(n, 10) local d2 = math.mod(math.floor(n / 10), 10) local d3 = math.mod(math.floor(n / 100), 10) -- local d4 = math.mod(math.floor(n / 1000), 10) -- local d5 = math.mod(math.floor(n / 10000), 10) set_overlay_digit(overlay.score[1], d1) set_overlay_digit(overlay.score[2], d2) set_overlay_digit(overlay.score[3], d3) -- set_overlay_digit(overlay.score[4], d4) -- set_overlay_digit(overlay.score[5], d5) end function init_score() -- Set defaults in case the high score file does not exist. -- initials[1] = "J" -- initials[2] = "U" -- initials[3] = "L" -- high_score = 000 -- Try to read the high score file. -- local fin = io.open(high_score_file, "r") -- if fin then -- local score = fin:read("*n") -- local name = fin:read("*a") -- -- fin:close(fin) -- if score then -- high_score = score -- end -- if name then -- initials[1] = string.sub(name, 1, 1) -- initials[2] = string.sub(name, 2, 2) -- initials[3] = string.sub(name, 3, 3) -- end -- end -- Apply the current high score to the display. -- set_overlay_alpha(overlay.high_inits[1], initials[1]) -- set_overlay_alpha(overlay.high_inits[2], initials[2]) -- set_overlay_alpha(overlay.high_inits[3], initials[3]) set_overlay_alpha(overlay.hints[1], hints[1]) set_overlay_alpha(overlay.hints[2], hints[2]) set_overlay_alpha(overlay.hints[3], hints[3]) set_overlay_hit(hit) set_overlay_miss(miss) set_overlay_score(score) end -- function write_score() -- local fout = io.open(high_score_file, "w") -- if fout then -- fout:write(high_score, initials[1], initials[2], initials[3]) -- fout:close() -- end -- end function init_viewport() viewport.x, viewport.y, viewport.w, viewport.h = E.get_display_union() local width = 512 + 384 * viewport.w / viewport.h viewport.a = viewport.h / viewport.w viewport.k = viewport.w / width viewport.h = width * viewport.a viewport.B = -width * viewport.a / 2 viewport.T = width * viewport.a / 2 viewport.w = width viewport.L = -width / 2 viewport.R = width / 2 -- Output some viewport information -- print("viewport(xywh)", viewport.x, viewport.y, viewport.w, viewport.h) -- print("viewport(LBTR): ", viewport.L, viewport.B, viewport.T, viewport.R) -- print("more viewport(ak):", viewport.a, viewport.k) end -- end of Overlay stuff ---------------------------------------------------------------------------------------------- -- INITIALIZING SCENE ...... done.. ---------------------------------------------------------------------------------------------- function init_scene() camera_pivot = E.create_pivot() camera = E.create_camera(E.camera_type_perspective) light = E.create_light(E.light_type_directional) scene = E.create_pivot() camera_2d= E.create_camera(E.camera_type_orthogonal) light_L = E.create_light(E.light_type_positional) light_R = E.create_light(E.light_type_positional) scene2d = E.create_pivot() -- setup scene hierachy E.parent_entity(camera, camera_pivot) E.parent_entity(light, camera) E.parent_entity(scene, light) E.parent_entity(camera_2d, scene) E.parent_entity(light_R, camera_2d) E.parent_entity(light_L, light_R) E.parent_entity(scene2d, light_L) -- E.set_entity_position(camera_2d, 0, 0, 0) E.set_entity_position(camera, DEFAULT_CAM_X, DEFAULT_CAM_Y, DEFAULT_CAM_Z) E.set_entity_position(light, DEFAULT_LIGHT_X, DEFAULT_LIGHT_Y, DEFAULT_LIGHT_Z) -- Move the origin to the center of the screen. E.set_entity_position(camera_2d, -viewport.k * viewport.w / 2, -viewport.k * viewport.h / 2, 0) E.set_entity_scale(scene2d, viewport.k, viewport.k, viewport.k) -- FIXME E.set_entity_scale(scene, viewport.k, viewport.k, viewport.k) -- FIXME put a entry to browse mode -----------for browse entry ------------ entryfile = datapath.."/entry3.obj" scale = 0.04 entry = create_body( entryfile, scene, -90, 1+CWALL_Y_OFFSET, 50, -90, 0, 90, scale, scale, scale, false, 0, 0) -- entry = create_body(entryfile, world, -90, 1, 50, -90,0,-90, scale, scale,scale, false, 0, 2) ------------for browse entry----------- end ---------------- FIXME for browse entry ------- function location() local crtx,crty,crtz= E.get_entity_position(camera) -- print ("\n", crtx,crty,crtz) if ((crtx > -110) and (crtx < -70 )) and ((crtz > 30) and (crtz < 70) )then E.nuke() dofile("../project2/browse.lua") -- dofile("cricket.lua") end end ---------------for browse entry -------- function init_sky() -- load sky images skymap = E.create_image (datapath.."/sky_nx.png", datapath.."/sky_px.png", datapath.."/sky_ny.png", datapath.."/sky_py.png", datapath.."/sky_nz.png", datapath.."/sky_pz.png") -- load sky object and apply image sky = create_body(skyfile,scene,0,0+CWALL_Y_OFFSET,0,0,0,0,500,500,500,false, 0, 0) E.set_brush_image(E.get_mesh(sky, 0), skymap) E.set_brush_flags(E.get_mesh(sky, 0), E.brush_flag_unlit, true) E.set_brush_flags(E.get_mesh(sky, 0), E.brush_flag_sky_map_0, true) end function init_objects() -- FIXME Add a floor as ground floor = create_body(floorfile, scene, 0, 0+CWALL_Y_OFFSET+0.7, 0, 0, 0, 0, 1000, 1, 1000, false, 1, 1) E.set_entity_flags(floor, E.entity_flag_hidden, true) -- E.set_entity_flags(floor, E.entity_flag_visible_geom, true) -- STADIUM create_body(stadfile, scene, 0, 0+CWALL_Y_OFFSET, 0, -90, 0, 0, 0.5, 0.5, 0.5, false, 0, 0) -- THE WICKETS offstump1 =create_body(stumpfile, scene, 1-0.1, 2+CWALL_Y_OFFSET, -30,0,0,0,1,1,1, true, 1, 1) midstump1 =create_body(stumpfile, scene, 1.3, 2+CWALL_Y_OFFSET, -30,0,0,0,1,1,1, true, 1, 1) legstump1 =create_body(stumpfile, scene, 1.6+0.1, 2+CWALL_Y_OFFSET, -30,0,0,0,1,1,1, true, 1, 1) farbail1 =create_body(bailfile, scene, 1.1, 3.3+CWALL_Y_OFFSET+0.1, -30, 0, 0, 0, 1, 1, 1, true, 1, 1) farbail2 =create_body(bailfile, scene, 1.5, 3.3+CWALL_Y_OFFSET+0.1, -30, 0, 0, 0, 1, 1, 1, true, 1, 1) offstump2 =create_body(stumpfile, scene, 1-0.1, 2+CWALL_Y_OFFSET, 30,0,0,0,1,1,1, true, 1, 1) midstump2 =create_body(stumpfile, scene, 1.3, 2+CWALL_Y_OFFSET, 30,0,0,0,1,1,1, true, 1, 1) legstump2 =create_body(stumpfile, scene, 1.6+0.1, 2+CWALL_Y_OFFSET, 30,0,0,0,1,1,1, true, 1, 1) nearbail1 =create_body(bailfile, scene, 1.1, 3.3+CWALL_Y_OFFSET+0.1, 30, 0, 0, 0, 1, 1, 1, true, 1, 1) nearbail2 =create_body(bailfile, scene, 1.5, 3.3+CWALL_Y_OFFSET+0.1, 30, 0, 0, 0, 1, 1, 1, true, 1, 1) -- THE BALL --create_body(ballfile,scene,1,5,20,0,0,0,0.7,0.7,0.7,false,0,0) end -- Initialize the wanda related objects function init_wand() wand = E.create_pivot() hand = E.create_pivot() E.parent_entity(wand, light) E.parent_entity(hand, light) -- THE BAT on the ground --mybat = create_body(oldbatfile, scene, 1, CWALL_Y_OFFSET+3, 0, 0, 90, 0, 1, 1, 1, true, 1, 1) -- hand_obj = create_body(batfile, hand, 2.5, 1, -26, 0, 0, 0, 2, 2.3, 2.3, true, false, 1) -- hand_obj = create_body(rampfile, hand, 0, 0, 0, 0, 0, 0, 0.03, 0.03, 0.04, true, false, 1) -- the wand hand -- hand_obj = E.create_object(rampfile) hand_obj = E.create_object(oldbatfile) E.parent_entity(hand_obj, hand) E.set_entity_position(hand_obj, 0, 0, 0) E.set_entity_rotation(hand_obj, 0, 0, 0) -- E.set_entity_scale(hand_obj, 0.03, 0.03, 0.04) E.set_entity_body_type(hand_obj, true) E.set_entity_body_attr(hand_obj, E.body_attr_gravity, 0) Xmin, Ymin, Zmin, Xmax, Ymax, Zmax = E.get_entity_bound(hand_obj) hand_dx=Xmax-Xmin hand_dy=Ymax-Ymin hand_dz=Zmax-Zmin -- E.set_entity_geom_type(hand_obj, E.geom_type_box, -- hand_dx, hand_dy, hand_dz) E.set_entity_flags(hand_obj, E.entity_flag_hidden, false) --FIXME handOffsetX = DEFAULT_HAND_OFFSET_X handOffsetY = DEFAULT_HAND_OFFSET_Y handOffsetZ = DEFAULT_HAND_OFFSET_Z -- FIXME some wand helper objects bat_inter = E.create_object(oldbatfile) -- E.set_entity_scale(bat_inter, 0.03, 0.03, 0.04) E.parent_entity(bat_inter, scene) E.set_entity_body_type(bat_inter, true) E.set_entity_body_attr(bat_inter, E.body_attr_gravity, 0) E.set_entity_flags(bat_inter, E.entity_flag_hidden, false) Xmin, Ymin, Zmin, Xmax, Ymax, Zmax = E.get_entity_bound(bat_inter) dx = Xmax - Xmin dy = Ymax - Ymin dz = Zmax - Zmin E.set_entity_geom_type(bat_inter, E.geom_type_box, dx, dy, dz) E.set_entity_geom_attr(bat_inter, E.geom_attr_callback, 2^32-1) E.set_entity_geom_attr(bat_inter, E.geom_attr_mass, 100000000) -- E.set_entity_flags(bat_inter, E.entity_flag_visible_geom, true) E.set_entity_position(bat_inter, E.get_entity_position(hand)) E.set_entity_flags(bat_inter, E.entity_flag_hidden, false) -- Only initialize tracking stuff if SIMULATOR == 0 if (SIMULATOR == 0) then E.set_entity_tracking(hand, 1, E.tracking_mode_world) E.set_entity_flags (hand, E.entity_flag_track_pos, true) E.set_entity_flags (hand, E.entity_flag_track_rot, true) -- Track the real contact object also E.set_entity_tracking(bat_inter, 1, E.tracking_mode_world) E.set_entity_flags (bat_inter, E.entity_flag_track_pos, true) E.set_entity_flags (bat_inter, E.entity_flag_track_rot, true) E.set_entity_tracking(wand, 1, E.tracking_mode_local) E.set_entity_flags (wand, E.entity_flag_track_pos, true) E.set_entity_flags (wand, E.entity_flag_track_rot, true) end end -- Sound initializations ------------------------ function init_sounds() pitch = E.load_sound(pitchsound) bat = E.load_sound(batsound) crowd = E.load_sound(crowdsound) stump = E.load_sound(stumpsound) ouch = E.load_sound(ouchsound) end -- MAIN FUNCTION ------------------------------------------------------------------------------------------------ function do_start() init_viewport() init_scene() -- INITIALIZE SCENE HIERARCHY init_objects() init_wand() init_overlay() init_score() init_sky() init_sounds() E.enable_timer(true) end --------------------------------------------------------------------------------- -- Functions that take care of loading object files, setting geoms, bodies etc.. --------------------------------------------------------------------------------- function create_body(file, parent, x, y, z, rx, ry, rz, sx, sy, sz, body, gravity, geom) local xmin,ymin,zmin,xmax,ymax,zmax = 0 local object = E.create_object(file) E.parent_entity(object, parent) E.set_entity_position(object, x, y, z) E.set_entity_rotation(object, rx, ry, rz) E.set_entity_scale(object, sx, sy, sz) E.set_entity_body_type(object, body) E.set_entity_body_attr(object, E.body_attr_gravity, gravity) if geom==0 then E.set_entity_geom_type(object,E.geom_type_none) end local xmin,ymin,zmin,xmax,ymax,zmax = E.get_entity_bound(object) if geom==1 then E.set_entity_geom_type(object, E.geom_type_box, (xmax-xmin)*sx, (ymax-ymin)*sy, (zmax-zmin)*sz) end if geom==2 then E.set_entity_geom_type(object, E.geom_type_sphere, ((xmax-xmin)/2)*sx) end if geom==3 then E.set_entity_geom_type(object, E.geom_type_capsule, (zmax-zmin)*sz, ((ymax-ymin)/2)*sy) end -- E.set_entity_flags(object, E.entity_flag_visible_geom, true) E.set_entity_geom_attr(object, E.geom_attr_callback, 2^32-1) -- E.set_entity_geom_attr(object, E.geom_attr_collider, 2^32-1) return object end -- get scaled u/v position for ball and camera overlays function get_scaled_u(pos_x) local u = (pos_x + 160) * 0.325 return u end function get_scaled_v(pos_z) local v = (pos_z + 400) * 0.130 return v end ------------------------------------------------------------------------------ -- update the scene taking user's input ------------------------------------------------------------------------------ function do_timer(dt) location() -- Check if it's in browse mode entry -- FIXME Update ball info local bx, by, bz = E.get_entity_position(ball) if bz > 400 or bz < -400 or bx > 160 or bx < -160 then E.delete_entity(ball) print("Out bound ball deleted") end if ballhit then -- print("ball position: ", E.get_entity_position(ball)) end -- TODO Update camera and ball position info in minimap cx, cy, cz = E.get_entity_position(camera) E.set_entity_position(overlay.camera_pos, viewport.L + get_scaled_u(cx) + 12, viewport.T - get_scaled_v(cz) - 24, 0) E.set_entity_position(overlay.ball_pos, viewport.L + get_scaled_u(bx) + 12, viewport.T - get_scaled_v(bz) - 24, 0) -- TODO A better way to count scores if hitcount == 4 and not ball == nil then local bx, by, bz = E.get_entity_position(ball) local cx, cy, cz = E.get_entity_position(camera) local dx = bx - cx local dz = bz - cz local range = math.sqrt(dx^2 + dz^2) print("Range: ", range) end -- set camera movement------------------------------------------------------------------------------ -- set timer to perform keyboard handling -- E.turn_entity(camera,0,-cdt_x * dt * 90,0) -- E.move_entity(camera,0,cdt_y * dt * MOV_RATE,0) --set user movement --'W'&'S' -- E.move_entity(camera,0,0,nav_z * dt * MOV_RATE) --'A'&'D' -- E.move_entity(camera,nav_x * dt * MOV_RATE,0,0) -- Pitch the ball pitch_ball() -- add_force_to_ball() ------------------------------------------ -- Do calculation on bat_inter constantly ------------------------------------------ --if bat_start == true then -- local xi,yi,zi = E.get_entity_position(bat_inter) --end --if bat_stop == true then -- local xt,yt,zt = E.get_entity_position(bat_inter) --end -- if xi ~= nil or yi ~= nil or zi ~= nil or xt ~= nil or yt ~= nil or zt ~= nil then -- bat_disp = (math.abs(xt-xi))/dt -- bat_velo = -- end ---- FIXME do tracking stuff local joy_x, joy_y = E.get_joystick(0) local s = MOV_RATE -- turn the user based on left/right movement of the joystick if (joy_x < -JOYSTICK_MIN) or (JOYSTICK_MIN < joy_x) then E.turn_entity(camera, 0, -joy_x * dt * 90, 0) camera_turnY = camera_turnY + (-joy_x * dt * 90) end -- move the user based on in/our movement of the joystick if (joy_y < -JOYSTICK_MIN) or (JOYSTICK_MIN < joy_y) then mov_x, mov_y, mov_z = E.get_entity_z_vector(hand) E.move_entity(camera, -mov_x * joy_y * dt * s, 0, ---mov_y * joy_y * dt * s, -mov_z * joy_y * dt * s) else -- deal with moving and turning via the keyboard E.turn_entity(camera, 0, -pan_x * dt * 90, 0) camera_turnY = camera_turnY + (-pan_x * dt * 90) mov_x, mov_y, mov_z = E.get_entity_y_vector(wand) E.move_entity(camera, -mov_x * pan_y * dt * s, -mov_y * pan_y * dt * s, -mov_z * pan_y * dt * s) mov_x, mov_y, mov_z = E.get_entity_z_vector(wand) E.move_entity(camera, -mov_x * pan_z * dt * s, -mov_y * pan_z * dt * s, -mov_z * pan_z * dt * s) end -- deal with moving the hand via keybord (if there is no hand tracking) if (SIMULATOR == 1) then handOffsetX = handOffsetX + hand_pan_x * 0.01 handOffsetY = handOffsetY + hand_pan_y * 0.01 handOffsetZ = handOffsetZ + hand_pan_z * 0.01 camx, camy, camz = E.get_entity_position(camera) camzx, camzy, camzz = E.get_entity_z_vector(camera) xAmount = handOffsetX * math.cos(camera_turnY*degToRad) + -handOffsetZ * math.sin(camera_turnY*degToRad) zAmount = -handOffsetX * math.sin(camera_turnY*degToRad) + -handOffsetZ * math.cos(camera_turnY*degToRad) E.set_entity_position(hand, camx + xAmount - camzx, camy + handOffsetY - camzy, camz + zAmount - camzz) E.set_entity_rotation(hand, 0, camera_turnY, 0) -- FIXME sync bat_inter E.set_entity_rotation(bat_inter, 0, camera_turnY, 0) -- FIXME update bat_inter helper position and orientation E.set_entity_position(bat_inter, E.get_entity_position(hand)) -- Pitch the ball pitch_ball() -- it's out there anyway end ---- end of FIXME do tracking stuff return true end function reset_state() E.create_light(camera,0,10,40) E.create_light(camera,0,15,40) E.create_light(camera,0,20,40) E.set_entity_position(camera, 18,140,-500) -- FIXME ORIGS E.set_entity_position(camera, DEFAULT_CAM_X, DEFAULT_CAM_Y, DEFAULT_CAM_Z) E.set_entity_rotation(camera, 0.0, 0.0, 0.0) camera_turnY = 0 handOffsetX = DEFAULT_HAND_OFFSET_X handOffsetY = DEFAULT_HAND_OFFSET_Y handOffsetZ = DEFAULT_HAND_OFFSET_Z -- may need to deal with the position of wand here too E.set_entity_position(hand, 0, 0, 0) E.set_entity_rotation(hand, 0, 0, 0) rot_x = 0 rot_y = 0 pan_x = 0 pan_y = 0 pan_z = 0 hand_pan_x = 0 hand_pan_y = 0 hand_pan_z = 0 --set camera orientation values to default -- cdt_x=0 -- cdt_y=0 -- cdt_z=0 -- set nav. values to default -- nav_x=0 -- nav_y=0 -- nav_z=0 -- Reset wickets -- far side E.set_entity_position(offstump1, 1-0.1, 2+CWALL_Y_OFFSET, -30) E.set_entity_rotation(offstump1, 0, 0, 0) E.set_entity_position(midstump1, 1.3, 2+CWALL_Y_OFFSET, -30) E.set_entity_rotation(midstump1, 0, 0, 0) E.set_entity_position(legstump1, 1.6+0.1, 2+CWALL_Y_OFFSET, -30) E.set_entity_rotation(legtump1, 0, 0, 0) E.set_entity_position(farbail1, 1.1, 3.3+CWALL_Y_OFFSET+0.2, -30) E.set_entity_rotation(farbail1, 0, 0, 0) E.set_entity_position(farbail2, 1.5, 3.3+CWALL_Y_OFFSET+0.2, -30) E.set_entity_rotation(farbail2, 0, 0, 0) -- near side E.set_entity_position(offstump2, 1-0.1, 2+CWALL_Y_OFFSET, 30) E.set_entity_rotation(offstump2, 0, 0, 0) E.set_entity_position(midstump2, 1.3, 2+CWALL_Y_OFFSET, 30) E.set_entity_rotation(midstump2, 0, 0, 0) E.set_entity_position(legstump2, 1.6+0.1, 2+CWALL_Y_OFFSET, 30) E.set_entity_rotation(legstump2, 0, 0, 0) E.set_entity_rotation(legtump1, 0, 0, 0) E.set_entity_position(nearbail1, 1.1, 3.3+CWALL_Y_OFFSET+0.2, 30) E.set_entity_rotation(nearbail1, 0, 0, 0) E.set_entity_position(nearbail2, 1.5, 3.3+CWALL_Y_OFFSET+0.2, 30) E.set_entity_rotation(nearbail2, 0, 0, 0) -- reset score stuff -- hit = 0 -- miss = 0 -- set_overlay_hit(hit) -- set_overlay_miss(miss) -- set_overlay_score(calculate_score()) end ------------------------------------------------------------------------------ -- keyboard handling -- W (move front), S (move back), A (move left), D (move right) -- Arrow Keys (Up - camera moves up), (Down - camera moves down) -- contd. (left - turn cam. along Y axis), right - (turn cam. along Y axis) ------------------------------------------------------------------------------ function do_keyboard(k, s) if s then -- if k == E.key_space and curr_state and curr_state.button_A then if k == E.key_space then E.set_entity_flags(overlay.title, E.entity_flag_hidden, true) bowl_a_ball() end -- user presses and holds 'b' to bat if k == E.key_b then bat_start=true bat_stop=false end -- reset user/cam to default position on ENTER keypress if k == E.key_return then -- TODO reset to init state reset_state() return true end -- Set navigation and camera movement -- FIXME try to replace it -- if not E.get_modifier(E.key_modifier_control) then -- A, W, S, D -- if k == E.key_w then nav_z=nav_z-1 end -- moving forward -- if k == E.key_a then nav_x=nav_x-1 end -- strafing left -- if k == E.key_s then nav_z=nav_z+1 end -- moving backward -- if k == E.key_d then nav_x=nav_x+1 end -- strafing right -- Up, Down, Left, Right -- if k == E.key_up then cdt_y=cdt_y+1 end -- cam moves up -- if k == E.key_down then cdt_y=cdt_y-1 end -- cam moves down -- if k == E.key_left then cdt_x=cdt_x-1 end -- cam rotates left along Y axis -- if k == E.key_right then cdt_x=cdt_x+1 end -- cam rotates right along Y axis -- end -- FIXME move the wanda - bat -- try to play with moving the wand without a tracker if k == E.key_w then hand_pan_y = hand_pan_y + 1 end if k == E.key_s then hand_pan_y = hand_pan_y - 1 end if k == E.key_a then hand_pan_x = hand_pan_x - 1 end if k == E.key_d then hand_pan_x = hand_pan_x + 1 end if k == E.key_q then hand_pan_z = hand_pan_z + 1 end if k == E.key_z then hand_pan_z = hand_pan_z - 1 end if not E.get_modifier(E.key_modifier_control) then if k == E.key_up then pan_z = pan_z + 1 end if k == E.key_down then pan_z = pan_z - 1 end if k == E.key_pagedown then pan_y = pan_y + 1 end if k == E.key_pageup then pan_y = pan_y - 1 end if k == E.key_right then pan_x = pan_x + 1 end if k == E.key_left then pan_x = pan_x - 1 end end -- FIXME end -- TODO one hit, curR_score++, if k == E.key_h then hit = hit + 1 set_overlay_hit(hit) set_overlay_score(calculate_score()) end if k == E.key_m then miss = miss + 1 set_overlay_miss(miss) set_overlay_score(calculate_score()) end -- end FIXME else -- stop doing navig. & cam. movement -- print("key_up",k) if not E.get_modifier(E.key_modifier_control) then -- FIXME replacement -- A, W, S, D -- if k == E.key_w then nav_z=nav_z+1 end -- if k == E.key_a then nav_x=nav_x+1 end -- if k == E.key_s then nav_z=nav_z-1 end -- if k == E.key_d then nav_x=nav_x-1 end -- Up, Down, Left, Right -- if k == E.key_up then cdt_y=cdt_y-1 end -- if k == E.key_down then cdt_y=cdt_y+1 end -- if k == E.key_left then cdt_x=cdt_x+1 end -- if k == E.key_right then cdt_x=cdt_x-1 end if k == E.key_up then pan_z = pan_z - 1 end if k == E.key_down then pan_z = pan_z + 1 end if k == E.key_pagedown then pan_y = pan_y - 1 end if k == E.key_pageup then pan_y = pan_y + 1 end if k == E.key_right then pan_x = pan_x - 1 end if k == E.key_left then pan_x = pan_x + 1 end end -- move the wanda - bat if k == E.key_w then hand_pan_y = 0 end if k == E.key_s then hand_pan_y = 0 end if k == E.key_a then hand_pan_x = 0 end if k == E.key_d then hand_pan_x = 0 end if k == E.key_q then hand_pan_z = 0 end if k == E.key_z then hand_pan_z = 0 end if k == E.key_b then bat_stop=true bat_start=false end end return false end ------------------------------------------------------------------------------------------- -- Bowl a ball ------------------------------------------------------------------------------------------- function bowl_a_ball() -- THE BALL if hitcounter > 0 then E.delete_entity(ball) end hitcounter = 0 ball = create_body(ballfile, scene, 1, 10+CWALL_Y_OFFSET, -30, 0, 0, 0, 1, 1, 1, true, 1, 2) -- -60 to -30 local xv, yv, zv = E.get_entity_z_vector(ball) local xv1, yv1, zv1 = E.get_entity_y_vector(ball) local ballforce=math.random(1400,1800) E.add_entity_force(ball, xv*ballforce, yv*ballforce, zv*ballforce) -- E.set_entity_geom_attr(ball,E.geom_attr_mass,2) E.set_entity_geom_attr(ball, E.geom_attr_bounce, 0.9) -- E.set_entity_geom_attr(plane,E.geom_attr_bounce,0.00000000000001) -- print("Just bowl a ball") -- FIXME play a pitch sound E.play_sound(pitch) end ------------------------------------------------------------------------------------------------ -- Make the ball bounce ------------------------------------------------------------------------------------------------ function pitch_ball() local x,y,z = E.get_entity_position(ball) --print(y) --if y>1+CWALL_Y_OFFSET and y<1.3+CWALL_Y_OFFSET then -- --end end -- TODO handle wicket collision function handle_wickets(entityA, entityB) -- offstump2 if entityA == offstump2 then if entityB == floor or entityB == nearbail1 or entityB == nearbail2 then return else -- print("offstump2 contact A") if entityB == ball then miss = miss + 1 set_overlay_miss(miss) set_overlay_score(calculate_score()) -- FIXME E.play_sound(stump) end end end if entityB == offstump2 then if entityA == floor or entityA == nearbail1 or entityA == nearbail2 then return else -- print("offstump2 contact B") if entityA == ball then miss = miss + 1 set_overlay_miss(miss) set_overlay_score(calculate_score()) -- FIXME E.play_sound(stump) end end end -- midstump2 if entityA == midstump2 then if entityB == floor or entityB == nearbail1 or entityB == nearbail2 then return else -- print("midstump2 contact A") if entityB == ball then miss = miss + 1 set_overlay_miss(miss) set_overlay_score(calculate_score()) -- FIXME E.play_sound(stump) end end end if entityB == midstump2 then if entityA == floor or entityA == nearbail1 or entityA == nearbail2 then return else -- print("midstump2 contact B") if entityA == ball then miss = miss + 1 set_overlay_miss(miss) set_overlay_score(calculate_score()) -- FIXME E.play_sound(stump) end end end -- legstump2 if entityA == legstump2 then if entityB == floor or entityB == nearbail2 or entityB == nearbail2 then return else -- print("legstump2 contact A") if entityB == ball then miss = miss + 1 set_overlay_miss(miss) set_overlay_score(calculate_score()) -- FIXME E.play_sound(stump) end end end if entityB == legstump2 then if entityA == floor or entityA == nearbail1 or entityA == nearbail2 then return else -- print("legstump2 contact B") if entityA == ball then miss = miss + 1 set_overlay_miss(miss) set_overlay_score(calculate_score()) -- FIXME E.play_sound(stump) end end end end function handle_camera_ball(entityA, entityB) if entityA == ball and entityB == camera then E.play_sound(ouch) end if entityA == camera and entityB == ball then E.play_sound(ouch) -- E.play_sound(you_hit_the_ball) end end ------------------------------------------------------------------------------------------- -- TODO: do_contact ------------------------------------------------------------------------------------------- function do_contact(entityA, entityB, px, py, pz, nx, ny, nz, d) -- print("Contact!", px, py, pz, nx, ny, nz, d) -- TODO Detect stumps' collision handle_wickets(entityA, entityB) handle_camera_ball(entityA, entityB) ----------------------------------------------------------------------------- -- To make ball bounce ----------------------------------------------------------------------------- if entityA == ball or entityB == ball then print("ball hitting something!!") -- Ball hits pitch if entityA == floor or entityB == floor and hitcounter < 10 then print("ball hitting plane") hitcounter = hitcounter + 1 local xv,yv,zv = E.get_entity_y_vector(ball) local bounciness=math.random(1000,1200) E.add_entity_force(ball,xv*bounciness*((10-hitcounter)/10),yv*bounciness*((10-hitcounter)/10),zv*bounciness*((10-hitcounter)/10)) --print("hitcounter=",hitcounter) --print(bounciness) --E.add_entity_force(ball, xv*bounciness, yv*bounciness, zv*bounciness) end -- Ball hits bat if entityA == bat_inter or entityB == bat_inter then print("bat is in contact with the ball") -- FIXME E.play_sound(bat) E.play_sound(crowd) -- calculate bat direction and add force to the ball in that direction -- globals : displacement, velocity and acceleration -- simple try to see if ball travels in the right direction local zx,zy,zz = E.get_entity_y_vector(bat_inter) E.add_entity_force(ball,zx*1200,zy*1200,zz*1200) -- calculate force (double differentiation of displacement w.r.t time) -- Add some points hit = hit + 1 set_overlay_hit(hit) set_overlay_score(calculate_score()) -- FIXME try to track ball position ballhit = true end end end ------------------------------------------------------------------------------------------- -- TODO: do_point(dx, dy) ------------------------------------------------------------------------------------------- function do_point(dx, dy) mouse_x = mouse_x + dx mouse_y = mouse_y + dy E.set_entity_flags(overlay.title, E.entity_flag_hidden, true) -- print("mouse rel. do_point: ", dx, dy) -- print("mouse_x, mouse_y :", mouse_x, mouse_y) return true end function do_joystick(n, b, s) -- n is joystick device number -- b is button number -- s is boolean giving button state if (s) then -- print("FIXME Joystick button pressed", s, n, b) --if not E.get_entity_flags(overlay.title, E.entity_flag_hidden) then E.set_entity_flags(overlay.title, E.entity_flag_hidden, true) --return --end bowl_a_ball() end end -- Score calculation function function calculate_score() local my_score = hit * 2 - miss * 1 if my_score < 0 then my_score = 0 end if my_score > 999 then my_score = 0 end return my_score end -- setup object category CAT_CAM = 1 CAT_WAND = 2 CAT_WORLD = 4 CAT_INTER = 8 CAT_FLOOR = 16 CAT_SPECIAL = 32 function setObjectCat(object, cat) local collider = 0 local response = 0 local callback = 0 E.set_entity_geom_attr(object, E.geom_attr_category, category) E.set_entity_geom_attr(object, E.geom_attr_collider, loc_collider) E.set_entity_geom_attr(object, E.geom_attr_response, loc_response) E.set_entity_geom_attr(object, E.geom_attr_callback, loc_callback) end ---------------------------------------------------------------------------------------------- do_start() -- START EVERYTHING!! ----------------------------------------------------------------------------------------------