Run A Simple Game In Max

Submitted by rootmaster on Tue, 06/12/2018 - 00:10

--a sphere game
scriptStr =  "x_pos = (gw.mapCPToWorld (mapScreenToCP  mouse.pos)).x\n";
scriptStr += "if x_pos > 146.5 then x = 146.5;\n";
scriptStr += "else \n"
scriptStr += "(\n"
scriptStr += "\tif x_pos < -146.5 then x = -146.5;\n";
scriptStr += "\telse x = x_pos;\n"
scriptStr += ")\n";
scriptStr += "x;";

struct ball(
    sphere_Node = GeoSphere radius:10,
    pos = [0,0,30],
    radius = 10
);

struct Game(
    ball,
    border,
    border_B,
    direction,
    iSpeed = 2,
    ratoteMat,
    stepVector,
    function createBall ballStruct = (
        ball = ballStruct;
        setTransformLockFlags ball.sphere_Node #all;
    ),
    function createBorder = (
        border = box Length:250 Width:350 Height:60;
        border.boxMode = on;
        border.xray = off;
        setTransformLockFlags border #all;
    ),
    function createBorder_B = (
        border_B = box pos:[0,-112.428,0] Length:23 Width:57 Height:60;
        setTransformLockFlags border_B #all;
    ),
    function resetGame = (
        ball.pos = [0,0,0];
        direction = random 60 120;
        ratoteMat = rotateZMatrix direction;
        stepVector = iSpeed * [1,0,0] * ratoteMat;
    ),
    function colWhichBorder = (
        local x_border_R = border.width / 2;
        local x_border_L = -x_border_R;
        local y_border_T = border.length / 2;
        local y_border_B = -y_border_T;
        local position = ball.pos;
        local rad = ball.radius;
        local len = border_B.length;
        local wid = border_B.Width;
        if position.x > x_border_R - rad then return 1;
        if position.x < x_border_L + rad then return 2;
        if position.y > y_border_T - rad then return 3;
        if position.y < y_border_B + rad + len then
        (
            if abs(position.x - border_B.pos.x) <  wid / 2 then return 4;
            return 5;
        )
        
        return 0;
    ),
    function handleCollision = (
        local whichB = colWhichBorder();
        --format "whichB:%\n" whichB;
        case whichB of
        (
            0: return 0;
            1: stepVector.x = -stepVector.x;
            2: stepVector.x = -stepVector.x;
            3: stepVector.y = -stepVector.y;
            4: stepVector.y = -stepVector.y;
            5: resetGame();
        )
    ),
    function moveBall = (
        handleCollision();
        ball.pos += stepVector;
    ),
    function setConstraint = (
        ball.sphere_Node.pos.controller = position_script();
        ball.sphere_Node.pos.controller.script = "newGame.moveBall();"
        border_B.pos.controller.X_Position.controller = float_script();
        border_B.pos.controller.X_Position.controller.script = scriptStr;
    ),
    function start ball= (
        createBall ball;        
        createBorder();
        createBorder_B();
        resetGame();
        
        setConstraint();
    )
);


try(
    delete newBall.sphere_Node;
    delete newGame.border;
    delete newGame.border_B;
)
catch()
resetMaxFile #noPrompt;
viewport.activeViewport = 1;
viewport.setLayout #layout_1;
viewport.setType #view_top;
newBall = ball();
newGame = Game();
newGame.start newBall;
playAnimation();