Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Help with classes in my game
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Stigmata




PostPosted: Wed May 24, 2006 10:22 pm   Post subject: Help with classes in my game

Hey everyone, I'm new to these forums, but I've lurked for a couple weeks, and everyone seems to be really cool here, so I don't think I'll need to ask for you to go easy on me Razz

Basically, I've been working on a game for my grade 12 programming class, end-of-the-year project. I took it last year, and made an Asteroids clone with powerups and such for the same project. 1800-odd lines for moving asteroids that break, powerups, a poorly-made HUD, and simple graphics. So this year, I decided to one-up myself and make, as I pitched it, "Asteroids meets Diablo 2."

The problem is, I taught myself classes in about a week, but now, I have no idea where I can go from here. I'm sure I've done one and/or many things improperly, especially with my use of classes, and there's a lot of things I can't figure out as far as those go. What I have is a framework for a spaceship-based RPG. Each individual ship is part of the Ship class, and each ship has (or rather, will have) individual "item" stats, represented by pointers to Armor, Engine, Weapon, and Generator within each Ship.

Now, the real problem is, I have no idea how to integrate these. I figured I might go for a program that, instead of having all the items hard-coded, simply loads all the items from a data file onto memory, and copies the data into the pointers for each ship. But I can't figure that out, among other things.

It's a bit complicated, as you can see, but I'm hoping someone here will be able to help out. *crosses fingers*

code:
import physics in "physics.tu"


var window : int := Window.Open ("position:center,center,graphics:800,600,offscreenonly")
%top-down shooting game

%----------------------------------------
%-------------- VARIABLES ---------------
%----------------------------------------

% CONSTANTS
const speedconstant : real := 0.3 %Multiplier that affects the speed of the ship


%----------------------------------------
%-------------- CLASSES -----------------
%----------------------------------------
% UPGRADES
class part %Base item class.
    var name, kind, description : string %Name and type of ship part/upgrade
    var powermin, powermax, mass : int %Min and max power consumption, and mass of part.
    var cost : int %Cost of the item.
end part

class generator
    %Doesn't inherit Part class -- no need for powermin/max variables.  Saves space.
    var name, kind, description : string
    var mass, power : int %Mass and power output.
    proc initialize (name_, kind_ : string, cost_, mass_, power_ : int)
        description := "Different generators weight more, and have different power outputs. The more power you have, the better equipment you can use."
    end initialize
end generator
class engine
    inherit part
    var speed : int %The base speed the engines apply to your ship.
    proc initialize (name_, kind_ : string, cost_, mass_, powermin_, powermax_, speed_ : int)
        description := "Different engines have different maximum speeds."
    end initialize
end engine
class armor
    inherit part     %Solid-state requires no power.  Energy shielding does not deteriorate.
    var defense : int     %The armor rating provided by the armoring purchased.
    var degraderate : real     %The rate at which the armor degrades per damage taken.  Energy shielding rate is 0.
    proc initialize (name_, kind_ : string, cost_, mass_, powermin_, powermax_, defense_ : int, degraderate_ : real)
        if kind = "solidstate" then %Solid-state armor doesn't consume power.
            powermin := 0
            powermax := 0
            degraderate := degraderate_
            description := "Solid-state armor. Doesn't require power input, but it will deteriorate in combat. Repair often."
        elsif kind = "energy" then
            powermin := powermin_
            powermax := powermax_
            degraderate := 0
            description := "Energy shielding. Requires power input from your generator, but it will not deteriorate in combat."
        end if
    end initialize
end armor

%---------- WEAPONS ----------
class weapon
    inherit part
    %Weapon type (kind) dictates the projectile type, solid or energy.
    var projectile : string
    proc initialize
        if kind = "solid" then
            if projectile = "" then
            end if
        elsif kind = "energy" then
        end if
    end initialize
end weapon

%---------- PROJECTILES ----------
class projectile_solid
    var damage : int     %Damage per hit
    var speed : int     %Speed of projectile.  0 = stationary (i.e. mines)
    var accel : int     %Acceleration of projectile.  Used for physics.
    var radius : int     %Explosion radius.  Used to scale explosion pics and to determine proximity detonations.
    var size : int     %Radius size of the projectile.  Used to determine collision detonations.
    var weight : int %Weight of projectile. Used in physics for guided rockets and such.
    var image : int %The projectile image.
end projectile_solid

class projectile_energy
    var damage : int
    var speed : int %Speed of projectile.  -1 is instant.
    var radius : int %Explosion radius.  Used to determine explosion/damage radius.
    var size : int %Size of projectile.  Used in collision detection.
    var image : int %The projectile image.
end projectile_energy

% ---------- SHIP ----------
class ship
    import generator, armor, engine, weapon
    export all

    var health : int := 100 %Ship health.
    var x : real := 0 % X coordinate.
    var y : real := 0 % Y coordinate.
    var xchange : real := 0 % Acceleration.
    var ychange : real := 0
    var speed, maxspeed : real := 0 %Current and max speed for the ship.
    var width : int := 0 % Dimensions.
    var height : int := 0
    var image : int := 0 % Image variable.

    %Equipment slots.  Flexible arrays, as different chassis' have different #s of slots for each type.
    var slotWeapon : pointer to weapon
    var slotArmor : pointer to armor
    var slotGenerator : pointer to generator
    var slotEngines : pointer to engine
    procedure initialize (x_, y_ : real, image_ : int)
        x := x_
        y := y_
        image := image_
        width := Pic.Width (image)
        height := Pic.Height (image)
    end initialize
    procedure damage (input : int)
        health := health - input
    end damage
    procedure move (xchange_ : real, ychange_ : real)
        xchange += xchange_
        ychange += ychange_
        x += xchange
        y += ychange
        %Limit Speed
        speed := sqrt (xchange ** 2 + ychange ** 2)
        if speed > maxspeed then
            xchange := maxspeed * (xchange / speed)
            ychange := maxspeed * (ychange / speed)
        end if
    end move
    procedure changeimage (input : int)
        image := input
    end changeimage
end ship

%------------------------------

var playerShip : pointer to ship
var npcShip : array 1 .. 3 of pointer to ship


%----- PUBLIC VARIABLES
%Physics
var xchange : real := 0
var ychange : real := 0
%Input
var key : array char of boolean
type mouse_data :
    record
        x, y, button : int
        dist : real
    end record
var mouse : mouse_data
%Upgrades and Ship Variables
var powermultiplier : real     %Determines the increase of part efficiency per power increase.


% IMAGE DATA
var pic : int := 0


%----------------------------------------
%-------------- GAME PROCEDURES ---------
%----------------------------------------


%--------------------------------------------------
%INPUT
proc move_brakes
    if ship (playerShip).speed not= 0 then
        xchange += ((0 - ship (playerShip).xchange) / ship (playerShip).speed) * .3
        ychange += ((0 - ship (playerShip).ychange) / ship (playerShip).speed) * .3
        %if (xchange < 0 and ship (playerShip).xchange > 0) or (xchange > 0 and ship (playerShip).xchange < 0) then
        %    xchange := 0
        %end if
        %if (ychange < 0 and ship (playerShip).ychange > 0) or (ychange > 0 and ship (playerShip).ychange < 0) then
        %    ychange := 0
        %end if
    end if
end move_brakes
%Screen-relative movement
proc move_up
    ychange := 1
end move_up
proc move_down
    ychange := -1
end move_down
proc move_left
    xchange := -1
end move_left
proc move_right
    xchange := 1
end move_right

%Keyboard input
proc keyin
    Input.KeyDown (key)
    xchange := 0
    ychange := 0
    if key (KEY_UP_ARROW) then
        move_up
    end if
    if key (KEY_DOWN_ARROW) then
        move_down
    end if
    if key (KEY_LEFT_ARROW) then
        move_left
    end if
    if key (KEY_RIGHT_ARROW) then
        move_right
    end if
    if key (KEY_CTRL) then
        move_brakes
    end if
    if xchange not= 0 or ychange not= 0 then
        xchange := xchange / sqrt ((xchange) ** 2 + (ychange) ** 2)
        ychange := ychange / sqrt ((xchange) ** 2 + (ychange) ** 2)
    end if
end keyin
%Mouse input
proc mousein
    Mouse.Where (mouse.x, mouse.y, mouse.button)
    mouse.dist := sqrt ((ship (playerShip).x - mouse.x + (ship (playerShip).width / 2)) ** 2 + (ship (playerShip).y - mouse.y + (ship (playerShip).height / 2)) ** 2)
end mousein


%--------------------------------------------------
% MOVEMENT
proc move
    ship (playerShip).move (xchange, ychange)
    for a : 1 .. upper (npcShip)
        %ship (npcShip(a)).move (xchange, ychange)
    end for
end move


%----------------------------------------
%-------------- RENDER PROCEDURES -------
%----------------------------------------
proc drawplayer
    Pic.Draw (ship (playerShip).image, round (ship (playerShip).x), round (ship (playerShip).y), picMerge)
end drawplayer

proc render
    drawplayer
end render


%----------------------------------------
%-------------- GAME LOOP ---------------
%----------------------------------------

new ship, playerShip
ship (playerShip).initialize (100, 100, Pic.FileNew ("Images/Player/base.bmp"))


loop
    mousein
    keyin
    move
    render
    %Draws a dot at the center of the ship, for reference.
    Draw.FillOval (round (ship (playerShip).x + (ship (playerShip).width / 2)), round (ship (playerShip).y + (ship (playerShip).height / 2)), 2, 2, 255)
    View.Update
    delay (25)
    cls
end loop


Here's hoping I don't get banned for a really long post Razz
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: