Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[links
- http://desmond.imageshack.us/Himg560/scaled.php?server=560&filename=20110822225402.png&res=medium
- http://desmond.imageshack.us/Himg51/scaled.php?server=51&filename=20110822225401.png&res=medium
- engine v3: http://www.mediafire.com/?jz5vdek99rc5kyz
- ]]
- adhoc = adhoc or {send = function() end, recv = function() end}
- os.cpu(333)
- obj = {}
- obj.pitch = {current = 1}
- obj.pitch[1] = model.load("gfx/models/pitch1.obj",0.3,color.new(0,0,0))
- obj.red = model.load("gfx/models/red.obj",0.3,color.new(0,0,0))
- obj.blue = model.load("gfx/models/blue.obj",0.3,color.new(0,0,0))
- obj.ball = model.load("gfx/models/ball.obj",0.3,color.new(0,0,0))
- --obj.base = model.load("gfx/models/pitch.obj")
- --obj.goal = model.load("gfx/models/goal.obj")
- ball = {inc=0.0002,side=1,velocity=1.5,obj = obj.ball, vel={x=nil,y=nil},pos={x=0,y=0,z=5},rot={x=0,y=0,z=0,vel = 0.2},old={},w=4,h=4,movebool = true, ang = nil}
- img = {}
- mode = {current = "normal",timer = timer.new()} mode.timer:reset() mode.timer:stop()
- snd = {}
- mode.free = {horizontal = true, limit ={w=100,h=50},maxscore=10}
- mode.normal = {horizontal = false, limit ={w=100,h=50},maxscore=10}
- player = {w=5,h=6,adhoc={},socket={}}
- player[1] = {ballang=nil,vel=0, vacc=0, hacc=0, obj = obj.red, pos={x=-mode[mode.current].limit.w/2+player.w/2,y=0,z=5}, rot={x=0,y=0,z=0,vel = 0.1}, score=0, w=player.w, h=player.h, who = "human", move = true,difficulty=100}
- player[2] = {ballang=nil,vel=0, vacc=0, hacc=0, obj = obj.blue, pos={x=mode[mode.current].limit.w/2-player.w/2,y=0,z=5}, rot={x=0,y=0,z=0,vel = 0.1}, score=0, w=player.w, h=player.h, who = "cpu", move = true,difficulty=100}
- goal = {w=5,h=16}
- goal[1] = {pos={x = mode[mode.current].limit.w/2+goal.w/2, y = 0}}
- goal[2] = {pos={x = -mode[mode.current].limit.w/2-goal.w/2, y = 0}}
- os.cpu(222)
- adhoc.recv = function()
- return player[1].pos.x.."|"..player[1].pos.y
- end
- function mode.set(str)
- mode.current = str
- end
- function player.adhoc.recv()
- local recvstr = adhoc.recv()
- if recvstr then
- local xyarray = string.explode(recvstr,"|")
- player[2].pos.x = tonumber(-xyarray[1])
- player[2].pos.y = tonumber(xyarray[2])
- end
- end
- function player.adhoc.send()
- adhoc.send(player[1].pos.x.."|"..player[1].pos.y)
- end
- function player.socket.send()
- end
- function player.socket.recv()
- end
- function mode.get()
- return mode.current
- end
- function mode.countdown()
- mode.timer:reset(); mode.timer:start()
- local size = 1.6
- while mode.timer:time() < 3000 do
- local txt = 3-math.floor(mode.timer:time()/1000)
- controls.read()
- size = 1.6+(mode.timer:time()/1000-math.floor(mode.timer:time()/1000))
- blit_base()
- player.blit()
- ball.blit()
- mode.blitinfo()
- screen.print(240-screen.textwidth(txt,size)/2,130,txt,size,color.new(255,0,0),color.new(0,0,0))
- screen.flip()
- end
- mode.timer:reset(); mode.timer:stop()
- end
- function mode.wait(mili)
- mode.timer:reset(); mode.timer:start()
- while mode.timer:time() < mili do
- blit_base()
- player.blit()
- ball.blit()
- mode.blitinfo()
- screen.flip()
- end
- mode.timer:reset(); mode.timer:stop()
- end
- function mode.resetall()
- ball.inc=0.0005
- ball.velocity=1.5
- ball.pos={x=0,y=0,z=0}
- ball.rot={x=0,y=0,z=0,vel = 0.01}
- ball.movebool = true
- mode.free.maxscore=30
- mode.normal.maxscore=2
- player[1].pos={x=-mode[mode.current].limit.w/2+player.w/2,y=0,z=0}
- player[2].pos={x=mode[mode.current].limit.w/2-player.w/2,y=0,z=0}
- for i = 1, #player do
- player[i].rot={x=0,y=0,z=0,vel = 0.1}
- player[i].score=0
- player[i].who = "human"
- player[i].move = true
- player[i].difficulty=50
- end
- end
- function mode.blitinfo()
- screen.print(240-screen.textwidth(player[1].score.." - "..player[2].score,0.77)/2,6,player[1].score.." - "..player[2].score)
- screen.print(2,2,"Mode: "..mode.get(),0.58,color.new(255,255,255),0x0)
- screen.print(475-screen.textwidth("@"..os.cpu().."mHz @"..screen.fps(),0.58),2,"@"..os.cpu().."mHz @"..screen.fps(),0.58,color.new(255,255,255),0x0)
- end
- function model.changecolor(material,clor)
- local mat = io.open(material, "r"); mat:seek("set");
- local mtl = mat:read("*a"); mat:flush(); mat:close();
- r = color.R(clor); g = color.G(clor); b = color.B(clor);
- r = r/255; g = g/255; b = b/255;
- if r == 0 then r = "0.000000" end if r == 1 then r = "1.000000" end
- if g == 0 then g = "0.000000" end if g == 1 then g = "1.000000" end
- if b == 0 then b = "0.000000" end if b == 1 then b = "1.000000" end
- mtl=mtl:gsub("Kd %d* %d* %d*","Kd "..r.." "..g.." "..b)
- --os.message(mtl)
- local mat = io.open(material, "w*"); mat:seek("set");
- mat:write(mtl); mat:flush(); mat:close();
- end
- function math.ang(x,y,x2,y2)
- return math.atan2(x-x2,y-y2)+1.57
- end
- function math.coli(tab,tab2)
- if tab.x + tab.w >= tab2.x and tab.x <= tab2.x+tab2.w and
- tab.y + tab.h >= tab2.y and tab.y <= tab2.y+tab2.h then
- return true
- end
- return false
- end
- function player.blit()
- for i = 1, #player do
- if player[i].move then player[i].rot.x = player[i].rot.x + player[i].rot.vel end
- player[i].obj:position(player[i].pos.x, player[i].pos.y, player[i].pos.z)
- player[i].obj:rotation(player[i].rot.x, player[i].rot.y, player[i].rot.z)
- player[i].obj:blit()
- --Calculate angles
- player[i].ballang = math.ang(player[i].pos.x,player[i].pos.y,ball.pos.x,ball.pos.y)
- end
- end
- function player.win()
- local winah = "Ha ganado el jugador %i."
- for i = 1, #player do
- if player[i].score >= mode[mode.current].maxscore then
- os.message(string.format(winah,i))
- dofile("script.lua")
- end
- end
- end
- function ball.blit()
- if ball.move then
- ball.rot.x = ball.rot.x + ball.rot.vel
- --ball.rot.y = ball.rot.y - ball.rot.vel
- --ball.rot.z = ball.rot.z + ball.rot.vel
- end
- ball.obj:position(ball.pos.x, ball.pos.y, ball.pos.z)
- ball.obj:rotation(ball.rot.x, ball.rot.y, ball.rot.z)
- ball.obj:blit()
- end
- function ball.inverty()
- ball.vel.y = -ball.vel.y
- end
- function ball.invertx()
- ball.vel.x = -ball.vel.x
- end
- function ball.colision()
- --Player collision
- for i = 1, #player do
- if math.coli({x=ball.pos.x-ball.w/2,y=ball.pos.y-ball.h/2,w=ball.w,h=ball.h},{x=player[i].pos.x-player[i].w/2,y=player[i].pos.y-player[i].h/2,w=player[i].w,h=player[i].h}) then
- ball.pos.x = ball.old.x
- ball.pos.y = ball.old.y
- if player[i].ballang >= -0.79 and player[i].ballang < 0.79 then
- --ball.inverty()
- ball.invertx()
- elseif player[i].ballang >= 0.79 and player[i].ballang < 2.36 then
- ball.inverty()
- --ball.invertx()
- elseif player[i].ballang >= 2.36 and player[i].ballang < 3.93 then
- --ball.inverty()
- ball.invertx()
- elseif player[i].ballang >= 3.93 and player[i].ballang <= 4.71 or player[i].ballang >= -1.57 and player[i].ballang < -0.79 then
- ball.inverty()
- --ball.invertx()
- end
- ball.pos.x = ball.pos.x + ball.vel.x*ball.velocity
- ball.pos.y = ball.pos.y + ball.vel.y*ball.velocity
- end
- end
- --Goal collision
- for i = 1, #goal do
- if math.coli({x=ball.pos.x-ball.w/2,y=ball.pos.y-ball.h/2,w=ball.w,h=ball.h},{x=goal[i].pos.x-goal.w/2,y=goal[i].pos.y-goal.h/2,w=goal.w,h=goal.h}) then
- if i == 1 then
- player[1].score = player[1].score + 1
- ball.serve()
- mode.wait(500)
- elseif i == 2 then
- player[2].score = player[2].score + 1
- ball.serve()
- mode.wait(500)
- end
- end
- end
- end
- function ball.calcxy()
- ball.vel.x = math.cos(ball.ang)
- ball.vel.y = math.sin(ball.ang)
- end
- function ball.serve()
- ball.pos.x = 0
- ball.pos.y = 0
- ball.velocity = 1.5
- ball.ang = ({ math.rad(math.random(30,60)) , math.rad(math.random(120,150)) , math.rad(math.random(210,240)) , math.rad(math.random(300,330)) })[math.random(1,4)]
- ball.calcxy()
- end
- function ball.move()
- ball.velocity = ball.velocity + ball.inc
- ball.old.x = ball.pos.x
- ball.old.y = ball.pos.y
- ball.pos.x = ball.pos.x + ball.vel.x*ball.velocity
- ball.pos.y = ball.pos.y + ball.vel.y*ball.velocity
- ball.colision()
- if ball.pos.y + ball.h/2 >= mode[mode.current].limit.h/2 then
- ball.inverty()
- ball.pos.y = mode[mode.current].limit.h/2-ball.h/2
- end
- if ball.pos.y - ball.h/2 <= -mode[mode.current].limit.h/2 then
- ball.inverty()
- ball.pos.y = -mode[mode.current].limit.h/2+ball.h/2
- end
- if ball.pos.x + ball.w/2 >= mode[mode.current].limit.w/2 then
- ball.invertx()
- ball.pos.x = mode[mode.current].limit.w/2 - ball.w/2
- end
- if ball.pos.x - ball.w/2 <= -mode[mode.current].limit.w/2 then
- ball.invertx()
- ball.pos.x = -mode[mode.current].limit.w/2 + ball.w/2
- end
- --Check ball side ( 1 is left side, 2 is right side, 0 is just the middle)
- if ball.pos.x + ball.w/2 > 0 then
- ball.side = 2
- elseif ball.pos.x - ball.w/2 < 0 then
- ball.side = 1
- else
- ball.side = 0
- end
- ball.ang = math.atan2(ball.vel.x,ball.vel.y)-1.57
- end
- function player.move()
- --Move red player
- if not math.coli({x=ball.pos.x-ball.w/2,y=ball.pos.y-ball.h/2,w=ball.w,h=ball.h},{x=player[1].pos.x-player[1].w/2,y=player[1].pos.y-player[1].h/2,w=player[1].w,h=player[1].h}) then
- if player[1].who == "human" then
- if controls.down() then
- player[1].pos.y = player[1].pos.y - 1
- player[1].vacc = player[1].vacc + 0.06
- elseif controls.up() then
- player[1].pos.y = player[1].pos.y + 1
- player[1].vacc = player[1].vacc - 0.06
- end
- if player[1].vacc > 0 and not controls.down() then
- player[1].pos.y = player[1].pos.y - 0.6
- player[1].vacc = player[1].vacc-0.1
- if controls.press("up") then player[1].vacc = 0 end
- end
- if player[1].vacc < 0 and not controls.up() then
- player[1].pos.y = player[1].pos.y + 0.6
- player[1].vacc = player[1].vacc+0.1
- if controls.press("down") then player[1].vacc = 0 end
- end
- elseif player[1].who == "cpu" then
- if ball.side == 1 and (ball.ang <- 1.57 or ball.ang > 1.57) then
- if player[1].pos.y < ball.pos.y then
- player[1].pos.y = player[1].pos.y + math.min(ball.pos.y-player[1].pos.y,(player[1].difficulty/100)*ball.velocity)
- elseif player[1].pos.y > ball.pos.y then
- player[1].pos.y = player[1].pos.y - math.min(player[1].pos.y-ball.pos.y,(player[1].difficulty/100)*ball.velocity)
- end
- end
- end
- end
- --Move blue player
- if not math.coli({x=ball.pos.x-ball.w/2,y=ball.pos.y-ball.h/2,w=ball.w,h=ball.h},{x=player[2].pos.x-player[2].w/2,y=player[2].pos.y-player[2].h/2,w=player[2].w,h=player[2].h}) then
- if player[2].who == "human" then
- if controls.cross() then
- player[2].pos.y = player[2].pos.y - 1
- player[2].vacc = player[2].vacc + 0.06
- elseif controls.triangle() then
- player[2].pos.y = player[2].pos.y + 1
- player[2].vacc = player[2].vacc - 0.06
- end
- if player[2].vacc > 0 and not controls.cross() then
- player[2].pos.y = player[2].pos.y - 0.6
- player[2].vacc = player[2].vacc-0.1
- if controls.press("triangle") then player[2].vacc = 0 end
- end
- if player[2].vacc < 0 and not controls.triangle() then
- player[2].pos.y = player[2].pos.y + 0.6
- player[2].vacc = player[2].vacc+0.1
- if controls.press("cross") then player[2].vacc = 0 end
- end
- elseif player[2].who == "cpu" then
- if ball.side == 2 and ball.ang > -1.57 and ball.ang < 1.57 then
- if player[2].pos.y < ball.pos.y then
- player[2].pos.y = player[2].pos.y + math.min(ball.pos.y-player[2].pos.y,(player[2].difficulty/100)*ball.velocity)
- elseif player[2].pos.y > ball.pos.y then
- player[2].pos.y = player[2].pos.y - math.min(player[2].pos.y-ball.pos.y,(player[2].difficulty/100)*ball.velocity)
- end
- end
- elseif player[2].who == "adhoc" then
- player.adhoc.send()
- player.adhoc.recv()
- end
- end
- --Check vertical limits
- for i = 1, #player do
- if player[i].pos.y + player[i].h/2 > mode[mode.current].limit.h/2 then
- player[i].pos.y = mode[mode.current].limit.h/2 - player[i].h/2
- end
- if player[i].pos.y - player[i].h/2 < -mode[mode.current].limit.h/2 then
- player[i].pos.y = -mode[mode.current].limit.h/2 + player[i].h/2
- end
- end
- if mode[mode.current].horizontal then
- --Move red player
- if controls.left() then
- player[1].pos.x = player[1].pos.x - 1
- player[1].hacc = player[1].hacc + 0.06
- elseif controls.right() then
- player[1].pos.x = player[1].pos.x + 1
- player[1].hacc = player[1].hacc - 0.06
- end
- if player[1].hacc > 0 and not controls.left() then
- player[1].pos.x = player[1].pos.x - 0.6
- player[1].hacc = player[1].hacc-0.1
- if controls.press("right") then player[1].hacc = 0 end
- end
- if player[1].hacc < 0 and not controls.right() then
- player[1].pos.x = player[1].pos.x + 0.6
- player[1].hacc = player[1].hacc+0.1
- if controls.press("left") then player[1].hacc = 0 end
- end
- --Move blue player
- if controls.square() then
- player[2].pos.x = player[2].pos.x - 1
- player[2].hacc = player[2].hacc + 0.06
- elseif controls.circle() then
- player[2].pos.x = player[2].pos.x + 1
- player[2].hacc = player[2].hacc - 0.06
- end
- if player[2].hacc > 0 and not controls.square() then
- player[2].pos.x = player[2].pos.x - 0.6
- player[2].hacc = player[2].hacc-0.1
- if controls.press("circle") then player[2].hacc = 0 end
- end
- if player[2].hacc < 0 and not controls.circle() then
- player[2].pos.x = player[2].pos.x + 0.6
- player[2].hacc = player[2].hacc+0.1
- if controls.press("square") then player[2].hacc = 0 end
- end
- --Check horizontal limits
- --Player 1
- if player[1].pos.x + player[1].w/2 > 0 then
- player[1].pos.x = -player[1].w/2
- end
- if player[1].pos.x - player[1].w/2 < -mode[mode.current].limit.w/2 then
- player[1].pos.x = -mode[mode.current].limit.w/2 + player[1].w/2
- end
- --Player 2
- if player[2].pos.x + player[2].w/2 > mode[mode.current].limit.w/2 then
- player[2].pos.x = mode[mode.current].limit.w/2 - player[2].w/2
- end
- if player[2].pos.x - player[2].w/2 < 0 then
- player[2].pos.x = player[2].w/2
- end
- end
- end
- function blit_base()
- obj.pitch[obj.pitch.current]:position(0,0,0)
- obj.pitch[obj.pitch.current]:rotation(0,0,0)
- obj.pitch[obj.pitch.current]:blit()
- end
- ball.serve()
- --os.runeboot("LuaHM.PBP")
- --mode.countdown()
- --TAB1 = { x, y, z } (posicion de la camara)
- --TAB2 = { x, y, z } (punto al que miramos)
- --TAB3 = { x, y, z } (donde está el techo, (rotacion de la camara)). ( Normalmente { 0, 1, 0 } )
- lk={pos={x=0,y=0,z=45},roof={0,1,0}}
- lk.move = false
- function mode.test()
- screen.print(230,250,tostring(lk.move))
- if lk.move then
- if math.abs(controls.analogy()) > 70 then lk.pos.y = lk.pos.y -controls.analogy()/500 end
- if math.abs(controls.analogx()) > 70 then lk.pos.x = lk.pos.x +controls.analogx()/500 end
- if controls.r() then lk.pos.z = lk.pos.z-0.5 end
- if controls.l() then lk.pos.z = lk.pos.z+0.5 end
- world.lookat({player[1].pos.x+1,player[1].pos.y,player[1].pos.z},{0,0,0},{0,0,1})
- screen.print(5,25,"x: "..lk.pos.x.."\ny: "..lk.pos.y.."\nz: "..lk.pos.z)
- if controls.press("square") then
- arx = io.open("pos.txt","r+");
- arx:seek("end");
- arx:write("pos={"..lk.pos.x..","..lk.pos.y..","..lk.pos.z.."},roof={"..lk.roof[1]..","..lk.roof[2]..","..lk.roof[3].."}\n")
- arx:flush(); arx:close()
- end
- else
- if controls.press("r") then position.current = position.current +1 if position.current > #position then position.current = 1 end end
- if controls.press("l") then position.current = position.current -1 if position.current < 1 then position.current = #position end end
- world.lookat(position[position.current].pos,{0,0,0},{0,1,0})
- screen.print(5,25,"x: "..position[position.current].pos[1].."\ny: "..position[position.current].pos[2].."\nz: "..position[position.current].pos[3])
- end
- if controls.press("triangle") then lk.pos.x = position[position.current].pos[1]; lk.pos.y=position[position.current].pos[2]; lk.pos.z=position[position.current].pos[3]; lk.move = not lk.move end
- world.update()
- end
- position = {current=1}
- position[1] ={pos={0,0,45},roof={0,1,0}}--Normal
- position[2] ={pos={0,-52.8,34.5},roof={0,1,0}}
- position[3] ={pos={0,-56,29.5},roof={0,1,0}}
- position[4] ={pos={0,-53.3,20.5},roof={0,0,1}}
- function mode.setcamera()
- end
- while true do
- --change camera position
- mode.test()
- ---------DTMB GAME---------------------------------
- controls.read()
- blit_base()
- player.move()
- player.blit()
- ball.blit()
- --Comprobar si ha ganado algun jugador
- player.win()
- --
- screen.flip()
- ball.move()
- --Score
- mode.blitinfo()
- if controls.select() then os.cpu(333) a() end
- ---------------------------------------------------
- --[[screen.print(2,25,player[1].pos.x.."/"..player[1].pos.y,0.77,color.new(255,0,0),0x0)
- screen.print(472-screen.textwidth(player[2].pos.x.."/"..player[2].pos.y),25,player[2].pos.x.."/"..player[2].pos.y,0.77,color.new(0,0,255),0x0)
- screen.print(2,230,ball.velocity)
- --------------------------
- screen.print(2,260,controls.analogy().."/"..controls.analogx())
- screen.print(2,45,math.floor(ball.pos.x-ball.old.x).."\n"..math.floor(ball.pos.y-ball.old.y).."\n")
- screen.print(130,2,math.deg(ball.ang))
- screen.print(230,230,ball.side)
- screen.print(2,90,player[1].ballang,0.58,color.new(255,255,255),0x0)
- screen.print(440,90,player[2].ballang,0.58,color.new(255,255,255),0x0)
- draw.pbar(140,258,200,10, color.new(255,255,255), color.new(255,0,0),player[2].difficulty-1,99)
- if controls.r() then player[2].difficulty = player[2].difficulty + 0.5 end
- if controls.l() then player[2].difficulty = player[2].difficulty - 0.5 end
- player[2].difficulty = math.max(0,math.min(player[2].difficulty,100))
- player[1].difficulty = math.max(0,math.min(player[2].difficulty,100))
- screen.print(87,260,player[2].difficulty)
- if controls.start() then
- if (math.abs(controls.analogx())>100 or math.abs(controls.analogy()) >100) then
- ball.ang = math.atan2(-controls.analogy(),controls.analogx())
- ball.calcxy()
- end
- if controls.press("r") then ball.ang = math.rad(math.random(0,360)) ball.calcxy() end
- end
- --]]
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement