------------------------------------------------------------ -- CS540 PROJECT 2 CRICKET MUSEUM EXHIBIT -- -- Version 1.00 -- -- By Julian Chen, Yiwen Sun, Arunan Rabindran ------------------------------------------------------------ -- The project has two modes: Browse Mode and Play Mode -- This file is mainly for browse mode, which introduces some rules and history about cricket game. -- -- It provides two kinds of environment: General stadium and Ashes stadium -- Each of them has 3D models and stereo sounds. -- The interaction between users and environment is triggered by collision detection ------------------------------------------------------------ -- path definition datapath = "../project2/models/" path = "../project2/oggs/" -- model files stumpfile = datapath.."stump.obj" bailfile = datapath.."bail.obj" ballfile = datapath.."ball.obj" batfile = datapath.."bat.obj" stadfile = datapath.."stad3.obj" pitchfile = datapath.."pitch.obj" skyfile = datapath.."sky.obj" handfile = datapath.."hand.obj" entryfile1 = datapath.."entry2.obj" entryfile2 = datapath.."entry1.obj" entryfile3 = datapath.."entry3.obj" boxfile = datapath.."box.obj" playerfile = {datapath.."player1.obj", datapath.."player2.obj", datapath.."player3.obj", datapath.."player4.obj", datapath.."player5.obj", datapath.."player6.obj", datapath.."player7.obj", datapath.."player8.obj", datapath.."player9.obj", datapath.."bowler.obj"} bookfile1 = datapath.."bookface.obj" bookfile2 = datapath.."book.obj" bktablefile = datapath.."table.obj" shelffile = datapath.."shelf.obj" screenfile = { datapath.."screen1.obj", datapath.."screen2.obj", datapath.."screen3.obj", datapath.."screen4.obj"} stadfile2 = datapath.."stad2a.obj" trophyfile = datapath.."trophy.obj" tablefile = datapath.."table.obj" framefile = { datapath.."frame1.obj", datapath.."frame2.obj", datapath.."frame3.obj", datapath.."frame4.obj" } bodyfile = { datapath.."body1.obj", datapath.."body2.obj", datapath.."body3.obj"} ----------------------------------------- -- 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=40 degToRad = 3.14159 / 180.0 radToDeg = 180.0 / 3.14159 mouse_x = 0 mouse_y = 0 SIMULATOR = 0 JOYSTICK_MIN = 0.1 CWALL_Y_OFFSET = 8 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 DEFAULT_CAM_Z = 100 --14 DEFAULT_HAND_OFFSET_X = 0.05 -- default offset of a non-tracked hand DEFAULT_HAND_OFFSET_Y = -0.1 DEFAULT_HAND_OFFSET_Z = 0 camera_turnY = 0 handOffsetX = 0 handOffsetY = 0 handOffsetZ = 0 MIN_SIZE_FOR_GEOM = 0.05 -- status variable to keep current status -- 0 : current -- 1 : ashes current_state = 0 total_states = 2 ------------------------------------------ --initial for browse mode ------------------------------------------ last_hotspot = 0 global_time = 0 sound = {} ashsound = {} last_sound = nil span = 0 BALLSPEED = 1500 -- initial velocity of the ball that can be thrown fielder = {} fielderbk = {} ground = {} bodyline = {} frame = {} screen = {} region_no = 0 hotspot = { {-80, 1, -200}, -- player1 {-5, 1, -100}, -- {60, 1, -10}, {90, 1, -200}, {100, 1, 80}, {60, 1, 180}, {-40, 1, 220}, {-100, 1, 120}, {-50, 1, -5}, {1, 1, -25}, -- bowler {-20, 5, -150}, -- book {0, 1, 100}, -- shelf {90,1,20}, -- entry for ashes {-90,1,50}} -- entry for play mode ashspot = { {1.5,6,-50}, -- ashes {-30, -25, 80}, -- bodyline {1.5, 1, -80}, -- entry back {10, 5, 40}, -- frame1 {10, 5, -5}, -- frame2 {-50, 5, -40}, -- frame3 {-50, 5, 20}} -- frame4 envLight = {} light_pos = { {160,40,-200}, {160,40,130}, {-160, 40, 130}, {-160, 40,-200} } light_clr = { {1,1,1}, {0.5,0.5,0.5}, {0.5,1,0.5}, {0.5,0.5,1} } --------------------------------------- -- load sounds to play later on --------------------------------------- function add_sound() bgsound = nil -- 1--9 for each player on the ground telling sth about Positions or famous Players, sound[1] = E.load_sound(path.."fielder2.ogg") sound[2] = E.load_sound(path.."fielder1.ogg") sound[3] = E.load_sound(path.."fielder6.ogg") sound[4] = E.load_sound(path.."fielder7.ogg") sound[5] = E.load_sound(path.."fielder5.ogg") sound[6] = E.load_sound(path.."player6.ogg") sound[7] = E.load_sound(path.."fielder4.ogg") sound[8] = E.load_sound(path.."fielder3.ogg") sound[9] = E.load_sound(path.."fielder3.ogg") sound[10] = E.load_sound(path.."bowler.ogg") -- telling sth about Bowler and Bowling sound[11] = E.load_sound(path.."rules.ogg") -- telling sth about basic Laws of cracket sound[12] = E.load_sound(path.."bodyline6.ogg") -- telling sth about ASHES URN sound[13] = E.load_sound(path.."bodyline1.ogg") -- telling sth about BODYLINE sound[14] = E.load_sound(path.."bodyline5.ogg") sound[15] = E.load_sound(path.."bodyline4.ogg") last_sound=bgsound end ----------------------------------------- -- camera movement modifiers ----------------------------------------- function exit_browse() E.nuke() dofile("../proj2/rescene_432_cricket_2.lua") end ------------------------------------------------- -- default browse environment is general stadium ------------------------------------------------- function init_browse() E.set_entity_flags(sky,E.entity_flag_hidden,false) E.set_entity_flags(stad1, E.entity_flag_hidden, false) E.set_entity_flags(offstump1, E.entity_flag_hidden, false) E.set_entity_flags(midstump1, E.entity_flag_hidden, false) E.set_entity_flags(legstump1, E.entity_flag_hidden, false) E.set_entity_flags(bail1, E.entity_flag_hidden, false) E.set_entity_flags(bail2, E.entity_flag_hidden, false) E.set_entity_flags(offstump2, E.entity_flag_hidden, false) E.set_entity_flags(midstump2, E.entity_flag_hidden, false) E.set_entity_flags(legstump2, E.entity_flag_hidden, false) E.set_entity_flags(bail3, E.entity_flag_hidden, false) E.set_entity_flags(bail4, E.entity_flag_hidden, false) E.set_entity_flags(bowler, E.entity_flag_hidden, false) E.set_entity_flags(book1,E.entity_flag_hidden,true) E.set_entity_flags(book2,E.entity_flag_hidden,false) E.set_entity_flags(bktable,E.entity_flag_hidden,false) E.set_entity_flags(ball1,E.entity_flag_hidden,false) E.set_entity_flags(ball2,E.entity_flag_hidden,false) E.set_entity_flags(ball3,E.entity_flag_hidden,false) E.set_entity_flags(bat1,E.entity_flag_hidden,false) E.set_entity_flags(bat2,E.entity_flag_hidden,false) E.set_entity_flags(bat3,E.entity_flag_hidden,false) E.set_entity_flags(shelf,E.entity_flag_hidden,false) E.set_entity_flags(entry1,E.entity_flag_hidden,false) E.set_entity_flags(entry2,E.entity_flag_hidden,false) E.set_entity_flags(entry3,E.entity_flag_hidden,true) for i =1, 9,1 do E.set_entity_flags(fielder[i],E.entity_flag_hidden,false) end for i=1,4,1 do E.set_entity_flags(screen[i], E.entity_flag_hidden, true) E.set_entity_flags(frame[i], E.entity_flag_hidden, true) E.set_light_color(envLight[i], light_clr[1][1], light_clr[1][2], light_clr[1][3]) end E.set_entity_flags(screen[1], E.entity_flag_hidden, false) E.set_entity_flags(stad2, E.entity_flag_hidden, true) E.set_entity_flags(trophy, E.entity_flag_hidden,true) E.set_entity_flags(table, E.entity_flag_hidden, true) E.set_entity_flags(bodyline[1], E.entity_flag_hidden, true) E.set_entity_flags(bodyline[2], E.entity_flag_hidden, true) E.set_entity_flags(bodyline[3], E.entity_flag_hidden, true) end ------------------------------------------------- -- ashes browse environment is old stadium ------------------------------------------------- function init_ashes() E.set_entity_flags(sky,E.entity_flag_hidden,true) E.set_entity_flags(stad1, E.entity_flag_hidden, true) E.set_entity_flags(offstump1, E.entity_flag_hidden, true) E.set_entity_flags(midstump1, E.entity_flag_hidden, true) E.set_entity_flags(legstump1, E.entity_flag_hidden, true) E.set_entity_flags(bail1, E.entity_flag_hidden, true) E.set_entity_flags(bail2, E.entity_flag_hidden, true) E.set_entity_flags(offstump2, E.entity_flag_hidden, true) E.set_entity_flags(midstump2, E.entity_flag_hidden, true) E.set_entity_flags(legstump2, E.entity_flag_hidden, true) E.set_entity_flags(bail3, E.entity_flag_hidden, true) E.set_entity_flags(bail4, E.entity_flag_hidden, true) E.set_entity_flags(bowler, E.entity_flag_hidden, true) E.set_entity_flags(book1,E.entity_flag_hidden,true) E.set_entity_flags(book2,E.entity_flag_hidden,true) E.set_entity_flags(bktable,E.entity_flag_hidden,true) E.set_entity_flags(ball1,E.entity_flag_hidden,true) E.set_entity_flags(ball2,E.entity_flag_hidden,true) E.set_entity_flags(ball3,E.entity_flag_hidden,true) E.set_entity_flags(bat1,E.entity_flag_hidden,true) E.set_entity_flags(bat2,E.entity_flag_hidden,true) E.set_entity_flags(bat3,E.entity_flag_hidden,true) E.set_entity_flags(shelf,E.entity_flag_hidden,true) E.set_entity_flags(entry1,E.entity_flag_hidden,true) E.set_entity_flags(entry2,E.entity_flag_hidden,true) E.set_entity_flags(entry3,E.entity_flag_hidden,false) for i =1, 9,1 do E.set_entity_flags(fielder[i],E.entity_flag_hidden,true) end for i=1,4,1 do E.set_entity_flags(screen[i], E.entity_flag_hidden, true) E.set_entity_flags(frame[i], E.entity_flag_hidden, false) E.set_light_color(envLight[i], light_clr[2][1], light_clr[2][2], light_clr[2][3]) end E.set_entity_flags(screen[1], E.entity_flag_hidden, true) E.set_entity_flags(stad2, E.entity_flag_hidden, false) E.set_entity_flags(trophy, E.entity_flag_hidden,false) E.set_entity_flags(table, E.entity_flag_hidden, false) E.set_entity_flags(bodyline[1], E.entity_flag_hidden, false) E.set_entity_flags(bodyline[2], E.entity_flag_hidden, true) E.set_entity_flags(bodyline[3], E.entity_flag_hidden, true) end function setup_roll(ground,top,x,y,z) E.set_entity_joint_type(ground, top, E.joint_type_hinge) E.set_entity_joint_attr(ground, top, E.joint_attr_axis_1, 0, 1, 0) E.set_entity_joint_attr(ground, top, E.joint_attr_anchor, x, y-1, z) E.set_entity_geom_attr(ground,E.geom_attr_friction, 5) E.set_entity_joint_attr(ground, top, E.joint_attr_force_max, 0) E.set_entity_joint_attr(ground, top, E.joint_attr_velocity, 1) -- E.set_entity_flags(ground,E.entity_flag_hidden,true) end -------------------------------------------------------------- -- function to locate the region in which user is currently in --------------------------------------------------------------- function location() local crtx,crty,crtz= E.get_entity_position(camera) print ("\n", crtx,crty,crtz) if (current_state == 1) then if ((crtx > -60) and (crtx < 30 )) and ((crtz > 40) and (crtz < 90 )) then --bodyline region_no = 2 end end end -------------------------------------------------------------- -- Function to Bowl a ball ------------------------------------------------------------------------------------------- function bowl_a_ball() ball = create_body(ballfile,world, hotspot[10][1],hotspot[10][2]+8,hotspot[10][3], 0,0,0, 1,1,1,true,1,2) E.set_entity_geom_attr(ball, E.geom_attr_callback, 2^32-1) local xv,yv,zv = E.get_entity_z_vector(ball) local xv1,yv1,zv1 = E.get_entity_y_vector(ball) E.add_entity_force(ball,xv*BALLSPEED,yv*BALLSPEED,zv*BALLSPEED) -- 3200 E.set_entity_geom_attr(ball,E.geom_attr_bounce,0.9) end ------------------------------------------------------------------------------------------------ -- Make the ball bounce ------------------------------------------------------------------------------------------------ function pitch_ball() local x,y,z = E.get_entity_position(ball) --print(y) if y>1 and y<1.3 then --print("ball hitting plane") local xv,yv,zv = E.get_entity_y_vector(ball) local bounciness=math.random(400,800) --print(bounciness) E.add_entity_force(ball,xv*bounciness,yv*bounciness,zv*bounciness) end end ------------------------------------------------------------------------------------------------ -- Function for interaction ------------------------------------------------------------------------------------------------ function active_show(index) if (current_state == 0) then -- if (index < 10) then -- E.set_entity_joint_attr(ground[index], fielder[index], E.joint_attr_force_max, 10) -- end if (index == 10) then -- bowl a ball if (last_hotspot ~= 10) then bowl_a_ball() end pitch_ball() end if (index == 11 ) then -- open book and on screen for laws E.set_entity_flags(book1, E.entity_flag_hidden, true) E.set_entity_flags(book2, E.entity_flag_hidden, false) if (span > 0 ) then if (global_time - span)< 5 then E.set_entity_flags(screen[1], E.entity_flag_hidden, false) E.set_entity_flags(screen[2], E.entity_flag_hidden, true) E.set_entity_flags(screen[3], E.entity_flag_hidden, true) E.set_entity_flags(screen[4], E.entity_flag_hidden, true) elseif (global_time - span)< 10 then E.set_entity_flags(screen[1], E.entity_flag_hidden, true) E.set_entity_flags(screen[2], E.entity_flag_hidden, false) E.set_entity_flags(screen[3], E.entity_flag_hidden, true) E.set_entity_flags(screen[4], E.entity_flag_hidden, true) elseif (global_time - span)< 15 then E.set_entity_flags(screen[1], E.entity_flag_hidden, true) E.set_entity_flags(screen[2], E.entity_flag_hidden, true) E.set_entity_flags(screen[3], E.entity_flag_hidden, false) E.set_entity_flags(screen[4], E.entity_flag_hidden, true) elseif (global_time - span)< 20 then E.set_entity_flags(screen[1], E.entity_flag_hidden, true) E.set_entity_flags(screen[2], E.entity_flag_hidden, true) E.set_entity_flags(screen[3], E.entity_flag_hidden, true) E.set_entity_flags(screen[4], E.entity_flag_hidden, false) else span = global_time end end end -- entry to THE ASHES environment if (index == 13) then current_state = 1 init_ashes() end -- entry to PLAY MODE if (index == 14) then exit_browse() end if (last_sound == bgsound ) and (index <12) then E.play_sound(sound[index]) last_sound= sound[index] span = global_time end else --in ashes environment if (index == 1) then -- for the trophy -- E.set_entity_joint_attr(tpground, trophy, E.joint_attr_force_max, 10) if (last_sound == bgsound ) then E.play_sound(sound[12]) last_sound= sound[12] span = global_time end end if (index == 2 ) then -- screening for bodyline if (span > 0 ) then if (global_time - span)< 4 then E.set_entity_flags(bodyline[1], E.entity_flag_hidden, false) E.set_entity_flags(bodyline[2], E.entity_flag_hidden, true) E.set_entity_flags(bodyline[3], E.entity_flag_hidden, true) elseif (global_time - span)< 8 then E.set_entity_flags(bodyline[1], E.entity_flag_hidden, true) E.set_entity_flags(bodyline[2], E.entity_flag_hidden, false) E.set_entity_flags(bodyline[3], E.entity_flag_hidden, true) elseif (global_time - span)< 12 then E.set_entity_flags(bodyline[1], E.entity_flag_hidden, true) E.set_entity_flags(bodyline[2], E.entity_flag_hidden, true) E.set_entity_flags(bodyline[3], E.entity_flag_hidden, false) else span = global_time end end if (last_sound == bgsound ) then E.play_sound(sound[13]) last_sound= sound[13] span = global_time end end -- entry to THE ASHES environment if (index == 3) then current_state = 0 init_browse() end if (index == 24) then if (last_sound == bgsound) then E.play_sound(sound[14]) last_sound= sound[14] span = global_time end end if (index == 26) then if (last_sound == bgsound) then E.play_sound(sound[15]) last_sound= sound[15] span = global_time end end end last_hotspot= index end ---------------------------------------------------------------------------- -- function to restore to static environment ------------------------------------------------------------------------------ function static_show() if (current_state == 0) then -- model local i=1 -- for i =1,3,1 do -- E.set_light_color(light[i],light_clr[1][1],light_clr[1][2],light_clr[1][3]) -- end for i =1, 9,1 do E.set_entity_joint_attr(ground[i], fielder[i], E.joint_attr_force_max, 0) end E.set_entity_flags(book1, E.entity_flag_hidden, false) E.set_entity_flags(book2, E.entity_flag_hidden, true) E.set_entity_flags(screen[1], E.entity_flag_hidden, false) E.set_entity_flags(screen[2], E.entity_flag_hidden, true) E.set_entity_flags(screen[3], E.entity_flag_hidden, true) E.set_entity_flags(screen[4], E.entity_flag_hidden, true) else -- ashes E.set_entity_joint_attr(tpground, trophy, E.joint_attr_force_max, 0) E.set_entity_flags(bodyline[1], E.entity_flag_hidden, false) E.set_entity_flags(bodyline[2], E.entity_flag_hidden, true) E.set_entity_flags(bodyline[3], E.entity_flag_hidden, true) end if (last_sound ~= bgsound) then E.stop_sound(last_sound) last_sound= bgsound span = 0 end last_hotspot=0 end ---------------------------------------------------------------------------- -- function for overlay ------------------------------------------------------------------------------ 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("../project2/images/"..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 == "alpha.png" then E.set_entity_scale(sprite, scale / 2, scale, scale) else E.set_entity_scale(sprite, scale, scale, scale) end return sprite end -- TODO initialize 2D on-screen overlays function init_overlay() local w = viewport.w overlay.title = create_overlay("title.png", 1.00, false) overlay.minimap = create_overlay("minimap.png", 0.50, false) E.set_entity_position(overlay.minimap, viewport.L + 64, viewport.T - 64, 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("alpha.png", 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("blue_digit.png", 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("red_digit.png", 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("blue_digit.png", 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 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 ---------------------------------------------------------------------------------------------- -- INITIALIZING SCENE ---------------------------------------------------------------------------------------------- function init_scene() camera = E.create_camera(E.camera_type_perspective) light = E.create_light(E.light_type_directional) world = 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(light_R, camera_2d) --E.parent_entity(light_L, light_R) --E.parent_entity(scene2d, light_L) --E.parent_entity(camera_2d,world) E.parent_entity(light, camera) E.set_entity_position(camera, 1, 8, 30) E.set_entity_position(light, 0, 10, 32) add_sound() -- set initial position for camera and light envLight[1] = E.create_light(E.light_type_directional) envLight[2] = E.create_light(E.light_type_directional) envLight[3] = E.create_light(E.light_type_directional) envLight[4] = E.create_light(E.light_type_directional) E.parent_entity(envLight[1], camera) --light) E.parent_entity(envLight[2], envLight[1]) E.parent_entity(envLight[3], envLight[2]) E.parent_entity(envLight[4], envLight[3]) E.parent_entity(world, envLight[4]) E.set_entity_position(envLight[1],light_pos[1][1],light_pos[1][2],light_pos[1][3]) E.set_entity_position(envLight[2],light_pos[2][1],light_pos[2][2],light_pos[2][3]) E.set_entity_position(envLight[3],light_pos[3][1],light_pos[3][2],light_pos[3][3]) E.set_entity_position(envLight[4],light_pos[4][1],light_pos[4][2],light_pos[4][3]) -- E.set_light_color(envLight[1], light_clr[1][1], light_clr[1][2], light_clr[1][3]) E.set_entity_scale(world, viewport.k, viewport.k, viewport.k) end ---------------------------------------------------------------------------- -- MAIN FUNCTION ------------------------------------------------------------------------------ function do_start() -- INITIALIZE SCENE HIERARCHY init_viewport() init_scene() --init_overlay() --init_score() -- 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,world,0,0,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) ---------------------------------------------------------------------------------------------- -- STADIUM stad1 = create_body(stadfile,world,0,0,0,-90,0,0,0.5,0.5,0.5,false,0,0) stad2 = create_body(stadfile2,world,0,-20,0, 180,0,0, 0.01,0.003,0.01, false,0,0) pitch = create_body(pitchfile,world,0,0,0,-90,0,0, 0.8,0.5,0.5,false,0,0) -- if geom set 1 and following used, then do_contact would report CD,but once geom set 1 hand_obj will disappear when it posi is 0,0,0 --E.set_entity_geom_attr(pitch, E.geom_attr_callback, 2^32-1) -- THE WICKETS offstump1 =create_body(stumpfile,world,1,2,-30,0,0,0,1,0.7,1,false,0,0) midstump1 =create_body(stumpfile,world,1.3,2,-30,0,0,0,1,0.7,1,false,0,0) legstump1 =create_body(stumpfile,world,1.6,2,-30,0,0,0,1,0.7,1,false,0,0) bail1 =create_body(bailfile,world,1.1,3.3,-30,0,0,0,0.7,0.7,0.7,false,0,0) bail2 =create_body(bailfile,world,1.5,3.3,-30,0,0,0,0.7,0.7,0.7,false,0,0) offstump2 =create_body(stumpfile,world,1,2,30,0,0,0,1,0.7,1,false,0,0) midstump2 =create_body(stumpfile,world,1.3,2,30,0,0,0,1,0.7,1,false,0,0) legstump2 =create_body(stumpfile,world,1.6,2,30,0,0,0,1,0.7,1,false,0,0) bail3 =create_body(bailfile,world,1.1,3.3,30,0,0,0,0.7,0.7,0.7,false,0,0) bail4 =create_body(bailfile,world,1.5,3.3,30,0,0,0,0.7,0.7,0.7,false,0,0) -- THE BALL --my_ball = create_body(ballfile,world,-160,40,-200,0,0,0,0.5,0.5,0.5,false,0,0) --1,5,20 scale= 0.7 -- THE PLAYERS scale = 1.0 for i =1,9,1 do fielder[i]=create_body(playerfile[i],world,hotspot[i][1],hotspot[i][2],hotspot[i][3], 0,0,180, scale, scale,scale, false,0,1) ground[i] = create_body(playerfile[11],world, hotspot[i][1],-5,hotspot[i][3], 90,0,0, 1,1,1, true,0,1) --y=0 -- setup_roll(ground[i],fielder[i],hotspot[i][1],hotspot[i][2],hotspot[i][3]) -- E.set_entity_geom_attr(fielder[i], E.geom_attr_callback, 2^32-1) end bowler=create_body(playerfile[10],world,hotspot[10][1],hotspot[10][2],hotspot[10][3], 0,0,180, scale, scale,scale, false,0,1) -- THE SCREEN scale = 80 scaley = 50.0 for j=1,4,1 do screen[j]=create_body(screenfile[j],world,hotspot[11][1],hotspot[11][2]-55,hotspot[11][3]-100, -90,0,0, scale, scaley,scale, false,0,0) end -- THE BOOK scale =0.01 book1=create_body(bookfile1,world,hotspot[11][1],hotspot[11][2]-2,hotspot[11][3], -90,0,0, scale, scale,scale, false,0,0) book2=create_body(bookfile2,world,hotspot[11][1],hotspot[11][2]-2-0.5,hotspot[11][3], -90,0,0, 0.04, 0.04,0.04, false,0,0) scale = 0.75 bktable=create_body(bktablefile,world,hotspot[11][1],hotspot[11][2]-5,hotspot[11][3], 0,0,0, scale, scale,scale, false,0,1) -- THE SHELF scale=0.5 shelf=create_body(shelffile,world,hotspot[12][1]+1,hotspot[12][2],hotspot[12][3]+3, -90,0,0, scale, scale,0.3, false,0,0) ball1 = create_body(ballfile,world,hotspot[12][1]-4,hotspot[12][2]+0.5,hotspot[12][3], 0,0,0, 1.5,1.5,1.5,false,0,0) ball2 = create_body(ballfile,world,hotspot[12][1],hotspot[12][2]+0.5,hotspot[12][3], 0,0,0, 1.5,1.5,1.5,false,0,0) ball3 = create_body(ballfile,world,hotspot[12][1]+4,hotspot[12][2]+0.5,hotspot[12][3], 0,0,0, 1.5,1.5,1.5,false,0,0) scale=2.0 bat1 = create_body(batfile,world,hotspot[12][1]-4,hotspot[12][2]+8,hotspot[12][3], -90,0,0, scale, scale,scale,false,0,0) bat2 = create_body(batfile,world,hotspot[12][1],hotspot[12][2]+8,hotspot[12][3], -90,0,0, scale, scale,scale,false,0,0) bat3 = create_body(batfile,world,hotspot[12][1]+4,hotspot[12][2]+8,hotspot[12][3], -90,0,0, scale, scale,scale,false,0,0) -- THE ENTRIES scale =0.04 entry1 = create_body(entryfile1,world,hotspot[13][1],hotspot[13][2],hotspot[13][3], -90,0,-90, scale, scale,scale, false,0,2) entry2 = create_body(entryfile2,world,hotspot[14][1],hotspot[14][2],hotspot[14][3], -90,0,90, scale, scale,scale, false,0,2) entry3 = create_body(entryfile3,world,ashspot[3][1],ashspot[3][2],ashspot[3][3], -90,0,0, scale, scale,scale, false,0,2) -- THE ASHES scale = 1 trophy = create_body(trophyfile,world,ashspot[1][1],ashspot[1][2]-3.2,ashspot[1][3], 0,0,0, 0.1,0.15,0.1, false,0,1 ) tpground = create_body(playerfile[11],world, ashspot[1][1],-10,ashspot[1][3], 90,0,0, 0.5,0.5,0.5, true,0,1) setup_roll(tpground,trophy,ashspot[1][1],ashspot[1][2],ashspot[1][3]) table = create_body(tablefile,world,ashspot[1][1],ashspot[1][2]-6,ashspot[1][3], 0,0,0, 1,1,1, false,0,1 ) -- THE FRAMES FOR ASHES scale = 0.06 for i=1,4,1 do frame[i] = create_body(framefile[i],world,ashspot[i+3][1],ashspot[i+3][2],ashspot[i+3][3], 0,90,0, scale,scale,scale, false,0,1 ) end E.set_entity_rotation(frame[2],0,90,0) E.set_entity_rotation(frame[3],0,90,0) E.set_entity_rotation(frame[4],0,90,0) -- THE BODYLINE scale = 40.0 scaley = 20.0 for j=1,4,1 do bodyline[j]=create_body(bodyfile[j],world,ashspot[2][1],ashspot[2][2],ashspot[2][3], -90,0,90, scale, scale,scale, false,0,0) end wand = E.create_pivot() hand = E.create_pivot() -- THE BAT hand_obj = create_body(batfile,world,0,1,10, 0,0,0, 2,2,10, true,0,1) --DEFAULT_HAND_OFFSET_X,DEFAULT_HAND_OFFSET_Y,DEFAULT_HAND_OFFSET_Z E.set_entity_geom_attr(hand_obj, E.geom_attr_mass, 6000000.0) E.parent_entity(wand, light) E.parent_entity(hand, light) E.set_entity_geom_attr(hand_obj, E.geom_attr_callback, 2^32-1) 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) E.set_entity_tracking(hand_obj, 1, E.tracking_mode_world) E.set_entity_flags (hand_obj, E.entity_flag_track_pos, true) E.set_entity_flags (hand_obj, 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 handOffsetX = DEFAULT_HAND_OFFSET_X handOffsetY = DEFAULT_HAND_OFFSET_Y handOffsetZ = DEFAULT_HAND_OFFSET_Z init_browse() 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+CWALL_Y_OFFSET,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 xmin,ymin,zmin,xmax,ymax,zmax = E.get_entity_bound(object) Xd = (xmax - xmin) Yd = (ymax - ymin) Zd = (zmax - zmin) -- make sure we have some kind of a box (not just a plane) if Xd < MIN_SIZE_FOR_GEOM then Xd = MIN_SIZE_FOR_GEOM end if Yd < MIN_SIZE_FOR_GEOM then Yd = MIN_SIZE_FOR_GEOM end if Zd < MIN_SIZE_FOR_GEOM then Zd = MIN_SIZE_FOR_GEOM end if geom==1 then E.set_entity_geom_type(object,E.geom_type_box,Xd*sx,Yd*sy,Zd*sz) end if geom==2 then if Xd >= Yd then r = Xd/2 else r = Yd/2 end E.set_entity_geom_type(object,E.geom_type_sphere,r*sx) end if geom==3 then E.set_entity_geom_type(object,E.geom_type_capsule,Zd*sz,(Yd/2)*sy) end --if geom >0 then --E.set_entity_flags(object, E.entity_flag_visible_geom, true) --end return object end ------------------------------------------------------------------------------ -- update the world taking user's input ------------------------------------------------------------------------------ function do_timer(dt) -- 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) E.turn_entity(hand_obj,0,-cdt_x * dt * 90,0) E.move_entity(hand_obj,0,cdt_y * dt * MOV_RATE,0) --set user movement --'W'&'S' E.move_entity(camera,0,0,nav_z * dt * MOV_RATE) E.move_entity(hand_obj,0,0,nav_z * dt * MOV_RATE) --'A'&'D' E.move_entity(camera,nav_x * dt * MOV_RATE,0,0) E.move_entity(hand_obj,nav_x * dt * MOV_RATE,0,0) ---- 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) E.turn_entity(hand_obj, 0, -joy_x * dt * 90, 0) 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) E.move_entity(hand_obj, -mov_x * joy_y * dt * s, -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) E.move_entity(hand_obj, -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) E.move_entity(hand_obj, -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) end -- interaction with environment location() if (region_no > 0 ) then active_show(region_no) else -- restore to static state static_show() end region_no = 0 global_time = global_time+dt return true 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) -- check for key_down if s then if k == E.key_space then E.set_entity_flags(overlay.title, E.entity_flag_hidden, true) end -- print(k,"key_down") -- reset user/cam to default position on ENTER keypress if k == E.key_return then 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) --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 return true end -- Set navigation and camera movement 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 if k == E.key_space then bowl_a_ball() end -- move the wand - bat -- try to play with moving the wand without a tracker if k == E.key_t then hand_pan_y = hand_pan_y + 1 end if k == E.key_y then hand_pan_y = hand_pan_y - 1 end if k == E.key_u then hand_pan_x = hand_pan_x - 1 end if k == E.key_i then hand_pan_x = hand_pan_x + 1 end if k == E.key_o then hand_pan_z = hand_pan_z + 1 end if k == E.key_p then hand_pan_z = hand_pan_z - 1 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 else -- stop doing navig. & cam. movement -- print("key_up",k) 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 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 --move the wanda - bat if k == E.key_t then hand_pan_y = 0 end if k == E.key_y then hand_pan_y = 0 end if k == E.key_u then hand_pan_x = 0 end if k == E.key_i then hand_pan_x = 0 end if k == E.key_o then hand_pan_z = 0 end if k == E.key_p then hand_pan_z = 0 end end end return false end ------------------------------------------------------------------------------------------- -- TODO: do_contact ------------------------------------------------------------------------------------------- function do_contact(entityA, entityB, px, py, pz, nx, ny, nz, d) if (current_state == 0) then if entityA == ball or entityB==ball then -- print("Bowl ball Contact!", px, py, pz, nx, ny, nz, d) end if entityA == bowler or entityB == bowler then region_no=10 -- print("Bowler", px, py, pz, nx, ny, nz, d) end for i=1,9,1 do if region_no == 0 and entityA == fielder[i] or entityB == fielder[i] then region_no = i end end if entityA == bktable or entityB==bktable then region_no=11 -- print("Book table!", px, py, pz,nx, ny, nz, d) end if entityA == entry1 or entityB==entry1 then region_no = 13 end if entityA == entry2 or entityB==entry2 then region_no = 14 end else -- Ashes if entityA == table or entityB==table then region_no = 1 end if entityA == bodyline[1] or entityB==bodyline[1] then region_no = 2 print("Bodyline!", px, py, pz, nx, ny, nz, d) end if entityA == frame[2] or entityB==frame[2] then region_no = 24 end if entityA == frame[3] or entityB==frame[3] then region_no = 26 end if entityA == entry3 or entityB==entry3 then region_no = 3 end end end ------------------------------------------------------------------------------------------- -- TODO: do_point(dx, dy) ------------------------------------------------------------------------------------------- function do_point(dx, dy) mouse_x = mouse_x + dx mouse_y = mouse_y + dy -- 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", n, b) E.set_entity_flags(overlay.title, E.entity_flag_hidden, true) end end -- Score calculation function function calculate_score() local my_score = hit * 7 - miss * 3 if my_score < 0 then my_score = 0 end return my_score end ---------------------------------------------------------------------------------------------- do_start() -- START EVERYTHING!! ----------------------------------------------------------------------------------------------