Class World
java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.JPanel
ActiverseEngine.World
- All Implemented Interfaces:
ActionListener, KeyListener, ImageObserver, MenuContainer, Serializable, EventListener, Accessible
Represents the world where actors interact.
This class extends JPanel and implements ActionListener, KeyListener.
The world has a fixed size and a black border, and can display a background image.
- Version:
- 1.4.1
- Author:
- Knivier
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class JPanel
JPanel.AccessibleJPanelNested classes/interfaces inherited from class JComponent
JComponent.AccessibleJComponentNested classes/interfaces inherited from class Container
Container.AccessibleAWTContainerNested classes/interfaces inherited from class Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy -
Field Summary
Fields inherited from class JComponent
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOWFields inherited from class Component
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENTFields inherited from interface ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidUpdates the world and repaints itvoidAdds an actor to the world at the specified locationvoidaddSound(ActiverseSound sound) Adds a sound to the worldfloatcalculateDistance(int x1, int y1, int x2, int y2) Calculates the distance between two points, based on the Euclidean distance formula.voidclear()Clears all actors from the world and cleans up resources.static voidSets default FPS value for Activerse (60)Returns the list of actors in the worldgetActorsByType(Class<T> type) Returns all actors of a specific typegetActorsInRegion(int x, int y, int width, int height) Gets actors within a specific regionstatic intgetFPS()Returns targeting FPS<T extends Actor>
TgetNearestActor(int x, int y, Class<T> type, double maxDistance) Finds the nearest actor of a specific type to a given positionReturns the list of loaded images in the worldstatic intGets ticks done in the enginevoidHandles key pressesvoidHandles key releasesvoidHandles key typingprotected voidPaints the world by drawing the background image, actors, and debug info Applies dynamic lighting if enabledvoidpause()Pauses the world timervoidremoveObject(Actor actor) Removes an actor from the worldvoidresume()Resumes the world timervoidsetBackgroundImage(String imagePath) Sets background image of world subclassesstatic voidsetFPS(int fpsValue) voidShows text on the world at the specified locationvoidstart()Starts the world timervoidstop()Stops the world timer and cleans up resourcesvoidupdate()Updates the world by calling the act method of each actor at a tick interval Updates memory trackerMethods inherited from class JPanel
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUIMethods inherited from class JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, updateMethods inherited from class Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTreeMethods inherited from class Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMixingCutoutShape, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
-
Constructor Details
-
World
public World(int width, int height, int cellSize) Constructor for the World class. Initializes the world with the given width, height, and cell size. Sets the preferred size, background color, and border. Initializes the list of actors, images, and sounds. Creates a timer for updating the world. Loads properties and sets debug mode and dynamic lighting. Adds a debug button and a terminate button to the world. Adds key listener and sets focus to the world.- Parameters:
width- The width of the world in cells.height- The height of the world in cells.cellSize- The size of each cell in pixels.
-
-
Method Details
-
getFPS
public static int getFPS()Returns targeting FPS- Returns:
- int
-
setFPS
public static void setFPS(int fpsValue) - Parameters:
fpsValue- Targeted FPS value
-
defaultFPS
public static void defaultFPS()Sets default FPS value for Activerse (60) -
getTicksDone
public static int getTicksDone()Gets ticks done in the engine- Returns:
- int Ticks done
-
setBackgroundImage
Sets background image of world subclasses- Parameters:
imagePath- Relative path for image to load as background image
-
update
public void update()Updates the world by calling the act method of each actor at a tick interval Updates memory tracker- See Also:
-
addObject
Adds an actor to the world at the specified location- Parameters:
actor- The actor to add to the worldx- The x-coordinate of the actory- The y-coordinate of the actor- See Also:
-
removeObject
Removes an actor from the world- Parameters:
actor- The actor to remove from the world- See Also:
-
clear
public void clear()Clears all actors from the world and cleans up resources. -
addSound
Adds a sound to the world- Parameters:
sound- The sound to add to the world- See Also:
-
start
public void start()Starts the world timer- See Also:
-
stop
public void stop()Stops the world timer and cleans up resources- See Also:
-
pause
public void pause()Pauses the world timer- See Also:
-
resume
public void resume()Resumes the world timer- See Also:
-
showText
Shows text on the world at the specified location- Parameters:
x- The x-coordinate of the texty- The y-coordinate of the texttext- The text to display
-
paintComponent
Paints the world by drawing the background image, actors, and debug info Applies dynamic lighting if enabled- Overrides:
paintComponentin classJComponent- Parameters:
g- The graphics object to draw on- See Also:
-
calculateDistance
public float calculateDistance(int x1, int y1, int x2, int y2) Calculates the distance between two points, based on the Euclidean distance formula. This method is useful for determining how far apart two points are in the world. You're welcome to create your own distance calculation method, but this one is provided for convenience. Access via: `World.calculateDistance(int x1, int y1, int x2, int y2)`. If you do not have a set world, you can create a "empty" world with `new World(0, 0, 1)` and use this method.- Parameters:
x1-y1-x2-y2-- Returns:
- int Distance between two points as an Activerse unit relative
-
actionPerformed
Updates the world and repaints it- Specified by:
actionPerformedin interfaceActionListener
-
keyPressed
Handles key presses- Specified by:
keyPressedin interfaceKeyListener
-
keyReleased
Handles key releases- Specified by:
keyReleasedin interfaceKeyListener
-
keyTyped
-
getActors
-
getActorsByType
-
getActorsInRegion
-
getNearestActor
Finds the nearest actor of a specific type to a given position- Parameters:
x- X coordinatey- Y coordinatetype- The class type to search formaxDistance- Maximum search distance (0 for unlimited)- Returns:
- The nearest actor of the type, or null if none found
-
getPreferredSize
Returns the list of loaded images in the world- Overrides:
getPreferredSizein classJComponent- Returns:
- The list of loaded images in the world
-